Java Collections - Difference between Stack and Queue Data Structure

Published April 11, 2022

Depending on the requirements, you can use stacks or queues to store objects while working with Java. Stacks are linear data structures in Java that are used to hold a collection of things using the Last-in-First-Out concept. The Stack class supports a variety of actions such as search, pop, and push. The Java Queue, on the other hand, is used to hold elements and process them in a First-In-First-Out sequence.

In this article, we are going to go through various differences between the Stack and Queue data structures in Java and the applications of each.

 

The Differences between Stack and Queue Data Structures in Java

Operating Principles

First of all, Stack operates on the  Last in First Out  (LIFO) principle, while on the other hand, Queue operates on the First In First Out (FIFO) principle.

 

Insertion

The Stack uses the Push Operation to insert elements, while the Queue uses the Enqueue operation to insert elements.

 

Deletion of elements

In Stack, elements are deleted using the Pop operation, while in Stack, the elements are deleted using the Dequeue operation.

 

Structure

In a stack, the insertion and deletion of elements take place from just one end only. Both the pop and Push operations take place from one end of a stack. While on the Queue, it uses the two ends to perform its operations. The Enqueue and Dequeue operation takes place at different ends of a queue. It uses two ends, rear, and front. The insertion takes place at the rear end and deletion at from end.

 

Variants

Stack does not support variants, while a Queue supports three types of variants, the priority queue, the double-ended Queue, and the circular Queue.

 

The Full Condition Examination

The stack is usually full if  top== max-1, while on the other hand, the Queue is full if the rear==max-1.

 

Visualization

Stacks are visualized as the vertical collection, while queues are visualized as the horizontal collection.

 

Pointers

Stack usually has one pointer, the top. This pointer indicates the address of the topmost element and the last inserted element of the stack.  On the other hand, the Queue uses the two pointers, the rear, and the front, to read and write data.  The rear shows the address of the last insert, while the pointer indicates the address of the first insert.

 

Application of the Stack and Queue Data structures

Application of Stack

You can apply the stack data structures in the following applications

These are used  to perform the expression evaluation

Used in the java virtual machine

Used in the parsing of strings  and string reversals

Used to match  the HTML tags in the web development

Used to call the recursive functions

 

Application of Queue

You can apply the Queue data structures in the following  applications

  • Ques are implemented  in the network switches and routers

  • Used in the media players to maintain the playlists

  • Used for handling the  interrupts in the operating  system

  • Used in the application of the round-robin scheduling algorithm

 

Examples of a Stack and a Queue

An Example of a Stack

import java.util.Stack;

public class StackSample {

            public static void main(String[] args) {

                        Stack<Integer> s = new Stack<Integer>();

                  s.push(50);

                  s.push(10);

                  s.push(65);

                  s.push(34);

                  s.push(88);

                  System.out.print("Elements in a stack include: " + s);

                  System.out.print("\nPop 88: ");

                  Integer num1 = (Integer) s.pop();

                  System.out.print(num1);

                  System.out.print("\n88 poped: " + s);

                  Integer pos = (Integer) s.search(10);

                  if(pos == -1)

                  System.out.print("\nSearch 10");

                  else

                  System.out.print("\nPosition 1" + pos + " in stack");

            }

}

 

An Example of a Queue

import java.util.LinkedList;

import java.util.Queue;

public class QueueSample {

            public static void main(String[] args) {

                        Queue<Integer> q = new LinkedList<>();

                for (int i=0; i<100; i++)

                q.add(i);

                System.out.println("Elements stored in a Queue:"+q);

                int removedele = q.remove();

                System.out.println("Elements removed:" + removedele);

                System.out.println(q);

                int head = q.peek();

                System.out.println("Head:" + head);

             

                int size = q.size();

                System.out.println("Size:" + size);

            }

}

 

Keywords: stack, Queue

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

203 Views