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 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.
- Push: inserts a value on top of the stack.
- Pop: deletes value on top of the stack.
- Top: prints value on top of the stack.
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.
- Enqueue: insert data at the end of the queue.
- Dequeue: deletes data in front of the queue.
- front: returns the data in front of the queue.
Difference Between Stack and Queue
|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.|