Skip to main content

Queue

SpaceTime
AccessLookupInsertionDeletion
O(n)O(n)O(n)O(1)O(1)

Definition​

Queue is a data structure that follows the First-In-First-Out (FIFO) principle, where elements are added at the end and removed from the front.

Simplified

You're in a candy store with your friends, and you all want to buy your favorite candies. But there's a rule: you can't all rush to the counter at once; you have to form a line. The first one in the line gets their candy first, then the second, and so on until everyone has their candy.

This is just like a queue in a shop.

Practice​

AspectPseudo Code
Enqueue
enqueue(item):
queue.add_to_end(item)
Peak
peek():
queue.show_first()

Dequeue
dequeue():
queue.remove_first()