CSC211 ยท 2078 ยท 10 marks
Define Queue. Write are different applications of queue? Explain queue operations with example.
Define Queue. Write are different applications of queue? Explain queue operations with example.[10]
Quick answer
A Queue is a linear data structure that follows the FIFO (First In First Out) principle, meaning the element inserted first is the element removed first. It is an ordered collection of elements in which insertion takes place at one end called the REAR and deletion takes place at the other end called the FRONT.
Queue: Definition, Applications, and Operations
1. Definition of Queue
A queue can be compared to a real-life queue of people standing in a line -- the person who joins first gets served first.
FRONT REAR
| |
v v
[ 10 | 20 | 30 | 40 | 50 ]
^ ^
(Deletion side) (Insertion side)
2. Applications of Queue
Queues are widely used in computer science and real-life scenarios:
| # | Application | Description |
|---|---|---|
| 1 | CPU Scheduling | Processes are scheduled for CPU execution in a queue (Round Robin, FCFS) |
| 2 | Printer Spooling | Print jobs are queued and processed one by one in order |
| 3 | Keyboard Buffer | Keystrokes are stored in a queue and processed in order |
| 4 | BFS (Breadth First Search) | Graph traversal uses a queue to visit nodes level by level |
| 5 | IO Buffers | Data transfer between processes uses queues as buffers |
| 6 | Simulation | Queues model real-world waiting lines (bank, ticket counter) |
| 7 | Network Packet Handling | Data packets are queued for transmission over a network |
| 8 | Operating System | Job scheduling, interrupt handling use queues |
3. Queue Operations
The primary operations performed on a queue are:
3.1 Basic Operations
| Operation | Description |
|---|---|
| Enqueue (Insert) | Add an element at the REAR of the queue |
| Dequeue (Delete) | Remove an element from the FRONT of the queue |
| IsEmpty | Check whether the queue is empty |
| IsFull | Check whether the queue is full |
| Peek / Front | Return the front element without removing it |
3.2 Queue Declaration (Array-based)
#define MAXSIZE 5
struct queue {
int items[MAXSIZE];
int front;
int rear;
};
typedef struct queue Q;
- Initially:
front = -1,rear = -1
3.3 IsEmpty Operation
int IsEmpty(Q *q) {
if (q->front == -1)
return 1; // Queue is empty
else
return 0;
}
3.4 IsFull Operation
int IsFull(Q *q) {
if (q->rear == MAXSIZE - 1)
return 1; // Queue is full
else
return 0;
}
3.5 Enqueue Operation
Algorithm:
- Check if queue is full. If full, print overflow and exit.
- If queue is empty, set
front = 0. - Increment
rearby 1. - Insert the new element at
items[rear].
void Enqueue(Q *q, int newitem) {
if (IsFull(q)) {
printf("Queue Overflow! Cannot insert.");
return;
}
if (IsEmpty(q))
q->front = 0;
q->rear = q->rear + 1;
q->items[q->rear] = newitem;
printf("%d inserted into queue.\n", newitem);
}
3.6 Dequeue Operation
Algorithm:
- Check if queue is empty. If empty, print underflow and exit.
- Store the front element.
- If
front == rear, reset both to-1(queue becomes empty). - Otherwise, increment
frontby 1. - Return the stored element.
int Dequeue(Q *q) {
int val;
if (IsEmpty(q)) {
printf("Queue Underflow! Cannot delete.");
return -1;
}
val = q->items[q->front];
if (q->front == q->rear)
q->front = q->rear = -1;
else
q->front = q->front + 1;
return val;
}
4. Worked Example
Perform the following operations on a queue of size 5:
Enqueue(10), Enqueue(20), Enqueue(30), Dequeue(), Enqueue(40), Dequeue()
Step-by-step Trace:
Initial State:
front = -1, rear = -1
Queue: [ | | | | ]
Step 1: Enqueue(10)
front = 0, rear = 0
Queue: [ 10 | | | | ]
Step 2: Enqueue(20)
front = 0, rear = 1
Queue: [ 10 | 20 | | | ]
Step 3: Enqueue(30)
front = 0, rear = 2
Queue: [ 10 | 20 | 30 | | ]
Step 4: Dequeue() --> returns 10
front = 1, rear = 2
Queue: [ | 20 | 30 | | ]
Step 5: Enqueue(40)
front = 1, rear = 3
Queue: [ | 20 | 30 | 40 | ]
Step 6: Dequeue() --> returns 20
front = 2, rear = 3
Queue: [ | | 30 | 40 | ]
Final State: front = 2, rear = 3, containing 30 and 40 in the queue.
Conclusion
The queue correctly follows FIFO order throughout this trace: the elements 10 and then 20 were removed in the same order they were inserted, and the two remaining elements, 30 and 40, stay in the array between the current front and rear indices, ready to be dequeued in that same order. This worked example, together with the array-based structure and the Enqueue/Dequeue/IsEmpty/IsFull operations defined above, completes the description of the queue data structure and its uses.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.