DSA Repository

Stacks And Queues Comparison

Main Rule

StructureRule
StackLast In, First Out
QueueFirst In, First Out
Priority QueueHighest priority first

Operation Comparison

StructureInsertDeletePeek
Stackpush at toppop from toptop element
Simple Queueenqueue at reardequeue from frontfront element
Circular Queueenqueue at rear with wrap-arounddequeue from front with wrap-aroundfront element
Priority Queueinsert by priority ruleremove highest-priority itemhighest-priority item

Time Complexity Table

StructureInsertDeletePeek
StackO(1)O(1)O(1)
Simple QueueO(1)O(1)O(1)
Circular QueueO(1)O(1)O(1)
Priority Queue (unsorted array)O(1)O(n)O(n)
Priority Queue (heap)O(log n)O(log n)O(1)

When To Use What

Choose Stack if:

  • last inserted item should come out first
  • you are dealing with recursion, undo, brackets, or expressions

Choose Simple Queue if:

  • first entered item should leave first
  • array waste is not a problem

Choose Circular Queue if:

  • you want efficient reuse of array space

Choose Priority Queue if:

  • some items must be handled before others