Queues
Queues
an algorithm.(5 marks)
Algorithm InsertCircularQueue(Element)
Exit
End If
queue[rear] = Element
End Algorithm
#include <stdio.h>
#define MAX 5
int queue[MAX];
if (rear == MAX - 1) {
printf("Queue is full\n");
} else {
if (front == -1) {
front = 0;
rear++;
queue[rear] = value;
void dequeue() {
printf("Queue is empty\n");
} else {
front++;
void display() {
printf("Queue is empty\n");
} else {
printf("\n");
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
dequeue();
display();
return 0;
A Double Ended Queue (Deque) is a linear data structure that allows insertion and deletion of elements from both
ends (front and rear). Unlike a normal queue, where elements can only be added at the rear and removed from the
front, in a deque, elements can be added or removed from both ends.
A Priority Queue is a type of queue in which each element is assigned a priority. Elements are dequeued in
the order of their priority, with the highest priority element being dequeued first. If two elements have the
same priority, they are dequeued in the order they were added (FIFO).
Example:
Consider a priority queue with elements:
(5, "Task A"), (2, "Task B"), (8, "Task C"), (3, "Task D").
The elements will be dequeued in the order: "Task C" (priority 8), "Task A" (priority 5), "Task D" (priority
3), "Task B" (priority 2).
The main disadvantage of a linear queue is that it has fixed size and does not reuse the empty spaces after dequeue
operations. This leads to inefficient memory usage, especially when elements are frequently dequeued from the
front. If there is space at the front but the queue is full at the rear, new elements cannot be inserted until the queue
is completely emptied.
if (front == 0) {
printf("Queue is full\n");
} else {
front = rear = 0;
} else {
front--;
queue[front] = value;
if (rear == MAX - 1) {
printf("Queue is full\n");
} else {
front = rear = 0;
} else {
rear++;
}
queue[rear] = value;
void deleteRear() {
if (front == -1) {
printf("Queue is empty\n");
} else {
if (front == rear) {
} else {
rear--;
void deleteFront() {
if (front == -1) {
printf("Queue is empty\n");
} else {
if (front == rear) {
} else {
front++;
Algorithm DeleteFrontCircularQueue
Exit
End If
front = rear = -1
Else
End If
End Algorithm
Algorithm InsertRearCircularQueue(Element)
Exit
End If
queue[rear] = Element
End Algorithm
12. Explain the process and algorithm for deleting an element from the
Exit
End If
Print queue[front] "deleted"
front++
front = rear = -1
End If
End Algorithm
13. Compare the circular queue and double ended queue. .(5 marks)
14. Explain the working of the priority queue with an example. .(5 marks)
A Priority Queue is a type of data structure in which each element is assigned a priority, and elements are
served based on their priority rather than their arrival order. The key feature of a priority queue is that
elements with higher priority are dequeued before those with lower priority. If two elements have the same
priority, they are dequeued in the order they were inserted (FIFO).
In a priority queue, there are two main operations:
Insert (Enqueue): Insert an element with an associated priority.
Delete (Dequeue): Remove and return the element with the highest priority.
How it Works:
Insertion: When an element is inserted into a priority queue, it is placed according to its priority. If the
priority queue is implemented using a heap (min-heap or max-heap), the heap property ensures that the
element with the highest priority (or lowest, depending on implementation) is always at the front.
Deletion: When an element is dequeued, the element with the highest priority is removed. After the
removal, the queue is rearranged to maintain the order of priority.
Example:
Consider a priority queue where tasks are represented by a tuple containing the priority and the task
description. The elements in the priority queue are inserted with the following priority values:
Task A: Priority 3 (Urgent)
Task B: Priority 1 (Low priority)
Task C: Priority 2 (Medium priority)
Insertion Process:
Task A (Priority 3) is inserted first.
Queue: [(3, "Task A")]
Task B (Priority 1) is inserted next.
Queue: [(3, "Task A"), (1, "Task B")]
Task C (Priority 2) is inserted last.
Queue: [(3, "Task A"), (1, "Task B"), (2, "Task C")]
Deletion Process:
First Dequeue: Task A (priority 3) is dequeued as it has the highest priority.
Queue after dequeue: [(2, "Task C"), (1, "Task B")]
Second Dequeue: Task C (priority 2) is dequeued next.
Queue after dequeue: [(1, "Task B")]
Third Dequeue: Task B (priority 1) is dequeued last.
Queue after dequeue: []
15. What are front and rear pointers and explain their significance. .(5 marks)
1. Front Pointer:
The front pointer always points to the element that is at the front of the queue, i.e., the element that is about to be
dequeued or removed.
Dequeue Operation: The front pointer is used to identify the element that should be removed first. It helps in
ensuring that the element at the front of the queue is dequeued first (FIFO principle).
Empty Queue Check: The front pointer helps in determining if the queue is empty. If front == -1 (for empty
queues) or if front > rear, it indicates that the queue is empty.
Queue Status Management: The front pointer keeps track of the position of the first element to be processed,
ensuring the queue operates in a structured manner.
2. Rear Pointer:
The rear pointer always points to the last element that has been inserted into the queue or the element that is at
the rear of the queue.
Enqueue Operation: The rear pointer is used to insert elements at the rear (end) of the queue. The rear pointer
moves forward each time a new element is added.
Full Queue Check: In some implementations (especially in a fixed-size queue), the rear pointer can be used to
determine if the queue is full. For example, in a circular queue, if rear == (front - 1) % size, the queue is
considered full.
Queue Management: The rear pointer helps in ensuring that elements are inserted in the correct order and that the
queue maintains its integrity by managing the addition of new elements.