Introduction to Queue Data Structure
Last Updated :
13 Dec, 2024
Queue is a linear data structure that follows FIFO (First In First Out) Principle, so the first element inserted is the first to be popped out.
FIFO Principle in Queue:
FIFO Principle states that the first element added to the Queue will be the first one to be removed or processed. So, Queue is like a line of people waiting to purchase tickets, where the first person in line is the first person served. (i.e. First Come First Serve).
Basic Terminologies of Queue
- Front: Position of the entry in a queue ready to be served, that is, the first entry that will be removed from the queue, is called the front of the queue. It is also referred as the head of the queue.
- Rear: Position of the last entry in the queue, that is, the one most recently added, is called the rear of the queue. It is also referred as the tail of the queue.
- Size: Size refers to the current number of elements in the queue.
- Capacity: Capacity refers to the maximum number of elements the queue can hold.
Representation of Queue
Operations on Queue
1. Enqueue:
Enqueue operation adds (or stores) an element to the end of the queue.
Steps:
- Check if the queue is full. If so, return an overflow error and exit.
- If the queue is not full, increment the rear pointer to the next available position.
- Insert the element at the rear.
2. Dequeue:
Dequeue operation removes the element at the front of the queue. The following steps are taken to perform the dequeue operation:
- Check if the queue is empty. If so, return an underflow error.
- Remove the element at the front.
- Increment the front pointer to the next element.
3. Peek or Front Operation:
This operation returns the element at the front end without removing it.
4. Size Operation:
This operation returns the numbers of elements present in the queue.
5. isEmpty Operation:
This operation returns a boolean value that indicates whether the queue is empty or not.
6. isFull Operation:
This operation returns a boolean value that indicates whether the queue is full or not.
Implementation of Queue Data Structure
Queue can be implemented using following data structures:
Complexity Analysis of Operations on Queue
| Operations |
Time Complexity |
Space Complexity
|
| enqueue |
O(1) |
O(1)
|
| dequeue |
O(1) |
O(1)
|
|
front
|
O(1)
|
O(1)
|
| size |
O(1) |
O(1)
|
| isEmpty |
O(1) |
O(1)
|
|
isFull
|
O(1)
|
O(1)
|
Types of Queues
Queue data structure can be classified into 4 types:
- Simple Queue: Simple Queue simply follows FIFO Structure. We can only insert the element at the back and remove the element from the front of the queue.
- Double-Ended Queue (Deque): In a double-ended queue the insertion and deletion operations, both can be performed from both ends. They are of two types:
- Input Restricted Queue: This is a simple queue. In this type of queue, the input can be taken from only one end but deletion can be done from any of the ends.
- Output Restricted Queue: This is also a simple queue. In this type of queue, the input can be taken from both ends but deletion can be done from only one end.
- Circular Queue: This is a special type of queue where the last position is connected back to the first position. Here also the operations are performed in FIFO order.
- Priority Queue: A priority queue is a special queue where the elements are accessed based on the priority assigned to them. They are of two types:
- Ascending Priority Queue: In Ascending Priority Queue, the elements are arranged in increasing order of their priority values. Element with smallest priority value is popped first.
- Descending Priority Queue: In Descending Priority Queue, the elements are arranged in decreasing order of their priority values. Element with largest priority is popped first.
Applications of Queue Data Structure
Application of queue is common. In a computer system, there may be queues of tasks waiting for the printer, for access to disk storage, or even in a time-sharing system, for use of the CPU. Within a single program, there may be multiple requests to be kept in a queue, or one task may create other tasks, which must be done in turn by keeping them in a queue.
- A Queue is always used as a buffer when we have a speed mismatch between a producer and consumer. For example keyboard and CPU.
- Queue can be used where we have a single resource and multiple consumers like a single CPU and multiple processes.
- In a network, a queue is used in devices such as a router/switch and mail queue.
- Queue can be used in various algorithm techniques like Breadth First Search, Topological Sort, etc.
Please refer Applications of Queue for more details.
FAQs (Frequently asked questions) on Queue Data Structure:
1. Which principle is followed by Queue data structure?
Queue follows FIFO (First-In-First-Out) principle, so the element which is inserted first will be deleted first.
2. In data structures, what is a double-ended queue?
In a double-ended queue, elements can be inserted and removed at both ends.
3. What are the four types of Queue data structure?
The four types of Queue are: Simple Queue, Double-ended queue, Circular Queue and Priority Queue.
4. What are some real-life applications of Queue data structure?
Real-life applications of queue include: Cashier Line in Stores, CPU Scheduling, Disk Scheduling, Serving requests on a shared resource like Printer.
5. What are some limitations of Queue data structure?
Some of the limitations of Queue are: deletion of some middle element is not possible until all the elements before it are not deleted. Also random access of any element takes linear time complexity.
6. What are the different ways to implement a Queue?
Queue data structure can be implemented by using Arrays or by using Linked List. The Array implementation requires the size of the Queue to be specified at the time of declaration.
7. What are some common operations in Queue data structure?
Some common operations on Queue are: Insertion or enqueue, Deletion or dequeue, front, rear, isFull, isEmpty, etc.
Related articles: