Queues follow the First-In-First-Out (FIFO) principle. Elements are added at the rear and removed from the front. Think of it like a line of people waiting - the first person to join is the first to be served.
FIFO: First In, First Out - Elements enter from rear and exit from front
First element added is the first to be removed
Constant time enqueue and dequeue operations
Ensures fair order of processing elements
Perfect for managing data flow and buffering
Basic FIFO queue with front and rear pointers
Rear connects to front, efficient memory usage
Elements served based on priority, not order
Double-ended queue, insertion/deletion at both ends
Add an element to the rear of the queue
Remove and return element from the front
View the front element without removing it
Check if the queue has no elements
Operating system process scheduling
Breadth-First Search in graphs and trees
Managing print jobs in order
Handling data between fast and slow devices
Operation | Time Complexity | Description |
---|---|---|
Enqueue | O(1) | Add element to rear |
Dequeue | O(1) | Remove element from front |
Front | O(1) | Access front element |
IsEmpty | O(1) | Check if queue is empty |
Space Complexity: O(n) where n is the number of elements