Queue using Array
What is a Queue?
A Queue is a linear data structure that follows the First In First Out (FIFO) principle.
-
First In First Out (FIFO) means:
➔ The element added first will be removed first.
Think of a queue in a bank or queue at a ticket counter:
-
The first person to enter the queue is the first to get served.
Basic Queue Operations:
Operation | Meaning |
---|---|
enqueue(x) | Add element x to the rear (end) of the queue. |
dequeue() | Remove the front element from the queue. |
peek() | View the element at the front without removing it. |
isEmpty() | Check if the queue is empty. |
Important Queue Terminologies
Term | Meaning |
---|---|
Front | Points to the first element in the queue. |
Rear | Points to the last element in the queue. |
Size | Total number of elements currently in the queue. |
Algorithms for Basic Queue Operations
We assume:
-
queue[MAX]
is an array to hold queue elements. -
front
andrear
are integers.(-1 initially) -
MAX
is the maximum size.
Algorithm for Enqueue (Insert element)
Algorithm for Dequeue (Remove element)
-
Queue Overflow occurs when trying to add to a full queue.
-
Queue Underflow occurs when trying to remove from an empty queue.
-
In linear queue, after a lot of enqueues and dequeues, space at the front is wasted.
➔ To solve this, we can use a circular queue
🚀 Time Complexity of Queue Operations
Operation Description Time Complexity
(for fixed-size array queues)
🎯 5 Important Applications of Queues
Process Scheduling — Managing processes waiting for CPU time in operating systems.
-
Printer Queue Management — Handling multiple print jobs in order.
-
Handling Requests in Web Servers — Managing incoming client requests.
-
Breadth-First Search (BFS) — Exploring graphs level by level.
-
Network Packet Scheduling — Queuing data packets in routers before transmission.
Comments
Post a Comment