VOOZH about

URL: https://www.geeksforgeeks.org/dsa/difference-between-stack-and-queue-data-structures/

⇱ Difference Between Stack and Queue Data Structures - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Difference Between Stack and Queue Data Structures

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:


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:


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

FeatureStackQueue
DefinitionA 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 OperationsPush (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/RemovalElements are added and removed from the same end (the top).Elements are added at the rear and removed from the front.
Use CasesFunction 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.
ExamplesBrowser history (back button), reversing a word.Customer service lines, CPU task scheduling.
Real-World AnalogyA 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 StructureTypically uses a contiguous block of memory or linked list.Typically uses a circular buffer or linked list.
ImplementationCan be implemented using arrays or linked lists.Can be implemented using arrays, linked lists, or circular buffers.
Comment
Article Tags:
Article Tags: