Difference Between Stack and Queue Data Structures (original) (raw)
Last Updated : 6 Jun, 2026
Stack and Queue are linear data structures used to store and manage data efficiently. They differ in the way elements are inserted and removed, making them suitable for different programming scenarios and applications.
- Both store elements in a sequential manner.
- Stack follows the LIFO (Last In, First Out) principle.
- Queue follows the FIFO (First In, First Out) principle.
Stacks
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. It can be visualized as a pile of plates where you can only add or remove the top plate.
Operations on Stack:
The primary operations associated with a stack are:
- **Push: Adds an element to the top of the stack.
- **Pop: Removes and returns the top element of the stack.
- **Peek (or Top): Returns the top element of the stack without removing it.
- **IsEmpty: Checks if the stack is empty.
- **Size: Returns the number of elements in the stack.
Use Cases of Stack:
- Stacks are used in various applications, including:
- Function Call Management: The call stack in programming languages keeps track of function calls and returns.
- Expression Evaluation: Used in parsing expressions and evaluating postfix or prefix notations.
- Backtracking: Helps in algorithms that require exploring all possibilities, such as maze solving and depth-first search.
**Example:
Java `
import java.util.Stack;
public class StackExample { public static void main(String[] args) {
// Creating a Stack
Stack<String> stack = new Stack<>();
// Pushing elements into Stack
stack.push("Plate A");
stack.push("Plate B");
stack.push("Plate C");
// Display Stack
System.out.println("Stack: " + stack);
// Removing elements (LIFO order)
System.out.println("Removed: " + stack.pop());
System.out.println("Removed: " + stack.pop());
// Display remaining Stack
System.out.println("Final Stack: " + stack);
// Peek top element
System.out.println("Top Element: " + stack.peek());
}}
`
Output
Stack: [Plate A, Plate B, Plate C] Removed: Plate C Removed: Plate B Final Stack: [Plate A] Top Element: Plate A
**Explanation:
- **push(): adds elements to the top of the stack.
- **pop(): removes the top element (LIFO order).
- **peek(): returns the top element without removing it.
Queues
A queue is a linear data structure that follows the First In, First Out (FIFO) principle. This means that the first element added to the queue is the first one to be removed. It can be visualized as a line of people waiting for a service, where the first person in line is the first to be served.
Operations on Queue
The primary operations associated with a queue are:
- **Enqueue: Adds an element to the end (rear) of the queue.
- **Dequeue: Removes and returns the front element of the queue.
- **Front (or Peek): Returns the front element of the queue without removing it.
- **IsEmpty: Checks if the queue is empty.
- **Size: Returns the number of elements in the queue.
Use Cases of Queue:
Queues are used in various applications, including:
- **Task Scheduling: Operating systems use queues to manage tasks and processes.
- **Breadth-First Search (BFS): In graph traversal algorithms, queues help in exploring nodes level by level.
- **Buffering: Used in situations where data is transferred asynchronously, such as IO buffers and print spooling.
**Example:
Java `
import java.util.LinkedList; import java.util.Queue;
public class QueueExample { public static void main(String[] args) {
// Creating a Queue
Queue<String> queue = new LinkedList<>();
// Adding elements into Queue (Enqueue)
queue.add("Customer A");
queue.add("Customer B");
queue.add("Customer C");
// Display Queue
System.out.println("Queue: " + queue);
// Removing elements (Dequeue - FIFO order)
System.out.println("Removed: " + queue.remove());
System.out.println("Removed: " + queue.remove());
// Display remaining Queue
System.out.println("Final Queue: " + queue);
// Peek front element
System.out.println("Front Element: " + queue.peek());
}}
`
Output
Queue: [Customer A, Customer B, Customer C] Removed: Customer A Removed: Customer B Final Queue: [Customer C] Front Element: Customer C
**Explanation:
- **add(): inserts elements at the rear of the queue.
- **remove(): removes elements from the front (FIFO order).
- **peek(): returns the front element without removing it.
Stack vs Queue
| Feature | Stack | Queue |
|---|---|---|
| **Definition | A linear data structure that follows the Last In First Out (LIFO) principle. | A linear data structure that follows the First In First Out (FIFO) principle. |
| **Primary Operations | Push (add an item), Pop (remove an item), Peek (view the top item) | Enqueue (add an item), Dequeue (remove an item), Front (view the first item), Rear (view the last item) |
| **Insertion/Removal | Elements are added and removed from the same end (the top). | Elements are added at the rear and removed from the front. |
| **Use Cases | Function call management (call stack), expression evaluation and syntax parsing, undo mechanisms in text editors. | Scheduling processes in operating systems, managing requests in a printer queue, breadth-first search in graphs. |
| **Examples | Browser history (back button), reversing a word. | Customer service lines, CPU task scheduling. |
| **Real-World Analogy | A stack of plates: you add and remove plates from the top. | A queue at a ticket counter: the first person in line is the first to be served. |
| **Complexity (Amortized) | **Push: O(1), **Pop: O(1), Peek: O(1) | **Enqueue: O(1), **Dequeue: O(1), **Front: O(1), **Rear: O(1) |
| **Memory Structure | Typically uses a contiguous block of memory or linked list. | Typically uses a circular buffer or linked list. |
| **Implementation | Can be implemented using arrays or linked lists. | Can be implemented using arrays, linked lists, or circular buffers. |