Difference Between Stack and Queue

Stack and queue are very similar to each other. The main difference between stack and queue is that stack uses Last In First Out principle and queue uses First In First Out principle.

First, we will study about stack and queue and then make a table comparing both.

Stack

Stack is a linear data structure. It is based on Last In First Out (LIFO) principle. It access/deletes most recent value first.

For example, in a buffet or party, plates are on top of each other. If we want to eat, we take the top plate on the top, not from the middle. This is a stack.

Basic Operations:

  1. Push: inserts a value on top of the stack.
  2. Pop: deletes value on top of the stack.
  3. Top: prints value on top of the stack.
stack push pop operations
stack operation

Queue

Queue is a linear data structure. It is based on First In First Out (LIFO) principle. It access/deletes the oldest value first.

For example, in a mall, the cashier serves the customer in front of the line first. This is a queue.

Basic Operations:

  1. Enqueue: insert data at the end of the queue.
  2. Dequeue: deletes data in front of the queue.
  3. front: returns the data in front of the queue.
queue operations
Queue operations

Difference Between Stack and Queue

StackQueue
Stack is a linear data structure. It follows Last In First Out (LIFO) principle.Queue is a linear data structure. It follows First In First Out (FIFO) principle.
The item which is inserted most recently is accessed first.The item which was inserted first is accessed first.
Stack uses 1 pointer (top).Queue uses 2 pointers (front and rear).
Easy to implement.Complex to implement as compared to stack.
Array implementation of the stack performs all operations in O(1).Array implementation of the queue may involve moving of elements. Its complexity is O(N) where N is the size of the array.
A circular queue can be used to overcome this problem.
Push and Pop operations are performed on the same end.Dequeue and Enqueue operations are performed on different ends.
Stack can be used to convert recursion functions to iterative functions.Queue is used in operating systems and network algorithms to implement First In First Come functionality.
difference between stack and queue

References

https://en.wikiversity.org/wiki/Data_Structures_and_Algorithms/Stacks_and_Queues

Leave a Comment

Your email address will not be published. Required fields are marked *