ADT Queue and Types of Queues
ADT Queue and Types of Queues
DATA STRUCTURES AND ALGORITHMS USING C (CST-003)
B.Tech. 2nd Year Lecture Notes
1. Introduction to Queue
A Queue is a linear data structure in which insertion of an element takes place at one end and deletion takes place at the other end.
A queue follows the principle of:
FIFO — First In, First Out
The element that enters the queue first is the first element to leave the queue.
A real-life example is a queue of people waiting at a ticket counter. The person who arrives first is generally served first.
2. Queue as an Abstract Data Type (ADT)
A Queue ADT defines the behavior and operations of a queue without specifying how the queue must be implemented.
The implementation may use:
Array
Linked list
The important operations of Queue ADT are:
| Operation | Description |
|---|---|
enqueue() | Inserts an element into the queue |
dequeue() | Removes an element from the queue |
front() / peek() | Returns the front element |
rear() | Returns the last element |
isEmpty() | Checks whether queue is empty |
isFull() | Checks whether queue is full |
The detailed algorithms and C implementations of these operations will be discussed in Topic 4.
3. Basic Terminology of Queue
A queue mainly has two ends:
Front
The front indicates the position from where an element is removed.
Rear
The rear indicates the position where a new element is inserted.
For example, consider:
10, 20, 30, 40
Here:
10→ Front element40→ Rear element
4. FIFO Principle
The fundamental property of a normal queue is FIFO.
Suppose the following elements are inserted:
10 → 20 → 30 → 40
The removal order will be:
10 → 20 → 30 → 40
Thus:
Example
If four students enter a laboratory queue in the following order:
Student A
Student B
Student C
Student D
Then the service order will normally be:
Student A
Student B
Student C
Student D
5. Queue Representation
A queue can primarily be represented using:
1. Array
A fixed-size array is used to store queue elements.
2. Linked List
Dynamic memory allocation is used to create queue nodes.
The choice depends on the application.
| Feature | Array Queue | Linked-List Queue |
|---|---|---|
| Memory | Fixed | Dynamic |
| Size | Usually fixed | Can grow dynamically |
| Implementation | Simple | Relatively complex |
| Memory utilization | May be inefficient | Generally better |
| Overflow | When array becomes full | When memory is unavailable |
| Access | Array indexing | Pointer traversal |
6. Types of Queues
The important types of queues are:
Simple Queue / Linear Queue
Circular Queue
Priority Queue
Another commonly discussed variant is the Deque (Double-Ended Queue), in which insertion and deletion can be performed from both ends.
For the present syllabus, the major focus is on:
Simple Queue, Circular Queue and Priority Queue
7. Simple Queue / Linear Queue
A Simple Queue, also called a Linear Queue, is the basic implementation of a queue in which:
insertion takes place at the rear
deletion takes place at the front
FIFO principle is followed
Example
Suppose a queue initially contains:
10 20 30 40
If 50 is inserted:
10 20 30 40 50
If one element is deleted:
20 30 40 50
The element 10 is removed because it was inserted first.
8. Structure of a Linear Queue
In an array implementation, two variables are generally maintained:
frontrear
For example:
Queue = [10, 20, 30, 40, 50]
↑ ↑
front rearConceptually:
frontpoints to the first available element.rearpoints to the last inserted element.
9. Problem with Linear Queue
One important limitation of a simple linear queue implemented using an array is unused space.
Consider an array of size 5:
[10][20][30][40][50]
↑ ↑
front rearNow remove three elements:
[ ][ ][ ][40][50]
↑ ↑
front rearAlthough the first three positions are empty, the queue may report that it is full if rear has already reached the last array position.
This is called false overflow or space wastage in a linear array queue.
This limitation motivates the use of a Circular Queue.
10. Circular Queue
A Circular Queue is a queue in which the last position of the array is logically connected to the first position.
When the rear reaches the last position, it can move back to the beginning if free space is available.
The circular arrangement allows previously unused positions to be reused.
11. Need for Circular Queue
Consider an array of size 5.
Initially:
10 20 30 40 50
After deleting three elements:
_ _ _ 40 50
A linear queue may not use the empty locations at the beginning.
A circular queue can reuse these locations.
For example, when new elements 60, 70, and 80 are inserted:
60 70 80 40 50
Thus, circular queues provide better utilization of the available array space.
12. Circular Movement Using Modulo
Circular queues commonly use the modulo operator %.
For an array of size N:
Similarly:
For example, if:
and:
then:
Therefore, rear moves from position 4 back to position 0.
13. Advantages of Circular Queue
Efficient utilization of array memory.
Avoids unnecessary space wastage.
Supports continuous insertion and deletion.
Useful in systems requiring repeated processing.
Suitable for buffers and scheduling applications.
Applications
CPU scheduling
Printer buffering
Keyboard buffering
Network packet buffering
Streaming systems
Traffic management
Round-robin scheduling
14. Priority Queue
A Priority Queue is a queue in which every element is associated with a priority.
Unlike a simple FIFO queue, deletion is generally based on priority rather than merely on arrival order.
For example:
| Element | Priority |
|---|---|
| A | 3 |
| B | 1 |
| C | 2 |
If a smaller numerical value represents higher priority, the removal order can be:
B → C → A
15. Priority Queue Principle
The fundamental idea is:
The element having the highest priority is served before an element having lower priority.
If two elements have the same priority, their relative order may be maintained according to the implementation, often following FIFO order.
Example
Suppose:
| Patient | Priority |
|---|---|
| P1 | 3 |
| P2 | 1 |
| P3 | 2 |
| P4 | 1 |
If priority 1 is highest, the queue may process:
P2 → P4 → P3 → P1
Here, P2 and P4 have the same priority, so their arrival order can be preserved.
16. Types of Priority Queue
Priority queues are commonly categorized as:
1. Ascending Priority Queue
The element with the smallest priority value is served first.
Example:
1 → 2 → 3 → 4
2. Descending Priority Queue
The element with the largest priority value is served first.
Example:
4 → 3 → 2 → 1
The exact meaning of "high priority" depends on the convention adopted by the application.
17. Simple Queue vs Circular Queue vs Priority Queue
| Feature | Simple Queue | Circular Queue | Priority Queue |
|---|---|---|---|
| Principle | FIFO | FIFO | Priority-based |
| Insertion | Rear | Rear | According to implementation |
| Deletion | Front | Front | Highest-priority element |
| Array utilization | May waste space | Efficient | Depends on implementation |
| Wrap-around | No | Yes | Not essential |
| Priority considered | No | No | Yes |
| Typical use | Basic waiting line | Buffers/scheduling | Scheduling/emergency systems |
18. Queue Operations — Overview
Although the detailed algorithms are covered in the next topic, it is important to understand the basic operations.
18.1 Enqueue
Enqueue means inserting an element into a queue.
For a normal queue:
Insertion occurs at the rear.
Example:
Before:
10 20 30
Enqueue 40:
10 20 30 40
18.2 Dequeue
Dequeue means removing an element from a queue.
For a normal queue:
Deletion occurs from the front.
Before:
10 20 30 40
After dequeue:
20 30 40
Element removed = 10.
18.3 Front / Peek
The front() or peek() operation returns the element at the front without removing it.
Example:
Queue:
10 20 30
front() returns:
10
Queue remains unchanged.
18.4 Rear
The rear() operation identifies the last element in the queue.
For:
10 20 30
the rear element is:
30
18.5 isEmpty()
Checks whether the queue contains no elements.
Conceptually:
if (front == -1)
printf("Queue is Empty");The exact condition depends on the chosen queue implementation.
18.6 isFull()
For an array-based linear queue, it generally checks whether:
rear == MAX - 1For a circular queue, the full condition is different and depends on the implementation.
A common condition is:
(rear + 1) % MAX == frontwhen one array position is intentionally kept empty.
19. Queue Overflow and Underflow
Queue Overflow
Overflow occurs when an insertion is attempted into a queue that has no available space.
Example:
Queue capacity = 5
Current elements = 5Attempting another insertion can cause overflow.
Queue Underflow
Underflow occurs when deletion is attempted from an empty queue.
Example:
Queue = EmptyAttempting dequeue() causes underflow.
20. Queue Using Linked List
A queue can also be implemented using a linked list.
Typically, two pointers are maintained:
frontrear
The structure consists of dynamically allocated nodes.
The advantage is that the queue does not require a fixed array size.
For a linked-list queue:
insertion is normally performed at
reardeletion is normally performed at
front
21. Applications of Queue
Queues are extensively used in computer science.
1. CPU Scheduling
Processes waiting for CPU execution can be maintained in queues.
2. Printer Spooling
Print jobs wait in a queue before being processed.
3. Network Communication
Packets may be temporarily stored in queues before transmission.
4. Operating Systems
Various processes and resources can be managed using queues.
5. Breadth-First Search
BFS of a graph uses a queue.
6. Buffering
Queues are used for temporary storage of data between components operating at different speeds.
7. Simulation
Queues are used to model:
Bank queues
Traffic systems
Customer service systems
Call centers
8. Round-Robin Scheduling
Circular queues are particularly suitable for round-robin CPU scheduling.
22. Queue and Stack — Important Difference
Students frequently confuse stacks and queues.
| Property | Stack | Queue |
|---|---|---|
| Principle | LIFO | FIFO |
| Full form | Last In First Out | First In First Out |
| Insertion | Top | Rear |
| Deletion | Top | Front |
| Main operations | PUSH, POP | ENQUEUE, DEQUEUE |
| Example | Stack of plates | People waiting in a line |
23. Important Conceptual Example
Suppose the following elements are inserted into a queue:
A, B, C, D
The queue becomes:
A → B → C → D
Now perform two dequeue operations.
First deletion:
A is removed.
Queue:
B → C → D
Second deletion:
B is removed.
Queue:
C → D
Now insert E:
C → D → E
Therefore, the order of removal is:
A → B → C → D → E
This demonstrates the FIFO principle.
24. Comparison of Queue Types
Simple Queue
Best understood as a straightforward FIFO structure.
Circular Queue
Best suited where the storage area is reused continuously.
Priority Queue
Best suited when elements must be processed according to importance or priority.
The appropriate type depends on the application requirements.
25. Complexity Overview
For appropriate implementations, the fundamental queue operations can generally be performed efficiently.
| Queue Type | Enqueue | Dequeue | Front/Peek |
|---|---|---|---|
| Linear Queue | O(1) | O(1) | O(1) |
| Circular Queue | O(1) | O(1) | O(1) |
| Linked-List Queue | O(1)* | O(1) | O(1) |
| Priority Queue | Depends on implementation | Depends on implementation | Depends on implementation |
*Assuming both front and rear pointers are maintained.
For priority queues, complexity depends on the underlying implementation, such as:
Array
Linked list
Binary heap
26. Key Points for Examination
Students should remember the following:
Queue follows FIFO.
Insertion occurs at rear.
Deletion occurs at front.
enqueue()inserts an element.dequeue()removes an element.peek()returns the front element without deleting it.Linear queues can suffer from unused spaces after deletions.
Circular queues solve the space-reuse problem through wrap-around.
Circular queues commonly use the modulo operator
%.Priority queues process elements according to priority.
Queues can be implemented using arrays or linked lists.
Queue is used in BFS, scheduling, buffering and resource management.
27. Short-Answer Questions
Define a queue.
What is FIFO?
What are
frontandrear?Define enqueue and dequeue.
What is queue overflow?
What is queue underflow?
What is a linear queue?
What is a circular queue?
Why is a circular queue required?
What is a priority queue?
Differentiate between FIFO and priority-based processing.
Give two applications of queues.
Differentiate between stack and queue.
What is the purpose of the modulo operator in a circular queue?
How can a queue be implemented using a linked list?
28. Long-Answer / Descriptive Questions
Explain Queue ADT and its basic operations with suitable examples.
Explain the FIFO principle of a queue.
Explain the representation of a linear queue using an array.
Discuss the limitations of a linear queue.
Explain circular queue with a suitable example.
How does a circular queue overcome the limitations of a linear queue?
Explain priority queue and its types.
Compare simple queue, circular queue and priority queue.
Explain array and linked-list implementations of queues.
Discuss various applications of queues in computer science.
29. Programming-Oriented Questions
Write a C program to implement a queue using an array.
Write a C program to perform enqueue and dequeue operations.
Write a C program to implement a circular queue.
Write a C program to implement a queue using a linked list.
Write a C program to check whether a queue is empty or full.
Implement a priority queue using an array.
Display all elements of a queue.
Implement
front()andrear()operations.
Topic Summary
A Queue is a fundamental linear data structure based on the FIFO principle. The two important ends are front and rear. A Simple Queue provides basic FIFO processing but may suffer from space wastage in array implementation. A Circular Queue allows the unused positions of an array to be reused. A Priority Queue processes elements according to their priority rather than simply according to their arrival order.
Topic 3 in one line:
Simple Queue → FIFO, Circular Queue → FIFO + efficient space reuse, Priority Queue → priority-based processing.
Next topic: Topic 4 – Operations on Different Types of Queues, covering detailed Enqueue, Dequeue, Front, Rear, algorithms, C programs, circular queue implementation, priority queue implementation, and complexity analysis.