⬅ Previous Topic
Space Complexity of Stack OperationsNext Topic ⮕
Queue Operations⬅ Previous Topic
Space Complexity of Stack OperationsNext Topic ⮕
Queue OperationsQueues are a fundamental linear data structure that follow the First-In First-Out (FIFO) principle. This means the first element added to the queue will be the first one to be removed. Queues are widely used in operating systems, network buffers, printers, and scheduling systems.
A queue is a linear structure where elements are inserted at one end called the rear and removed from the other end called the front. This order ensures that the element which has been in the queue the longest is processed first — just like a line of people waiting at a ticket counter.
front
rear
Queues follow First-In First-Out order. The earliest inserted element is always the first one to be removed. This behavior is ideal for handling tasks in the order they arrive.
Queues support two main operations: enqueue()
to add an element at the rear and dequeue()
to remove the element from the front.
Enqueue
Dequeue
Queues give access to two pointers — front and rear — representing the ends from which data is removed or added. You cannot directly access elements in between without dequeuing the earlier ones.
Queues are perfect for problems requiring ordered processing — like scheduling print jobs or handling requests in the order they arrive. Each operation happens in constant time, making queues highly efficient.
Besides the basic queue, there are many variants like:
Operating systems use queues to schedule tasks based on their arrival time. Each process is placed in a queue and executed in FIFO order unless priorities override it.
When multiple print jobs are sent to a printer, they are queued up. The printer processes each job in the order it was received using a queue.
BFS in graphs or trees uses queues to explore nodes level by level. Nodes are enqueued as they are visited and dequeued for processing.
Data packets arriving at a network port are queued before being processed. This ensures packets are handled in the same order they arrive.
In graphical interfaces, events like mouse clicks and key presses are added to an event queue and processed sequentially by the event loop.
⬅ Previous Topic
Space Complexity of Stack OperationsNext Topic ⮕
Queue OperationsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.