0% found this document useful (0 votes)
15 views8 pages

Queues

The document provides an overview of various types of queues, including circular queues, linear queues, double-ended queues, and priority queues, along with algorithms and C programs for their implementation. It explains the insertion and deletion processes, the significance of front and rear pointers, and compares circular queues with double-ended queues. Additionally, it discusses the disadvantages of linear queues and provides examples to illustrate the working of priority queues.

Uploaded by

ramesham683
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Queues

The document provides an overview of various types of queues, including circular queues, linear queues, double-ended queues, and priority queues, along with algorithms and C programs for their implementation. It explains the insertion and deletion processes, the significance of front and rear pointers, and compares circular queues with double-ended queues. Additionally, it discusses the disadvantages of linear queues and provides examples to illustrate the working of priority queues.

Uploaded by

ramesham683
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Queues

1. Explain the procedure to insert an element in a circular queue with

an algorithm.(5 marks)

Algorithm InsertCircularQueue(Element)

If ((rear + 1) % size == front) Then

Print "Queue is full"

Exit

End If

rear = (rear + 1) % size

queue[rear] = Element

End Algorithm

2. Write a program to implement a linear queue using arrays.(5 marks)

#include <stdio.h>

#define MAX 5

int queue[MAX];

int front = -1, rear = -1;

void enqueue(int value) {

if (rear == MAX - 1) {

printf("Queue is full\n");

} else {

if (front == -1) {

front = 0;

rear++;

queue[rear] = value;

printf("%d inserted into queue\n", value);

void dequeue() {

if (front == -1 || front > rear) {

printf("Queue is empty\n");
} else {

printf("%d dequeued from queue\n", queue[front]);

front++;

void display() {

if (front == -1 || front > rear) {

printf("Queue is empty\n");

} else {

printf("Queue elements are: ");

for (int i = front; i <= rear; i++) {

printf("%d ", queue[i]);

printf("\n");

int main() {

enqueue(10);

enqueue(20);

enqueue(30);

dequeue();

display();

return 0;

3. What is a double ended queue?(2 marks)

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.

4. Explain priority queue with an example.( 2 marks)

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).

5. What is the disadvantage of a linear queue? .( 2 marks)

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.

6. Write a C function to insert an element at the front end of the

double ended queue. .(5 marks)

void insertFront(int value) {

if (front == 0) {

printf("Queue is full\n");

} else {

if (front == -1) { // If queue is empty

front = rear = 0;

} else {

front--;

queue[front] = value;

printf("%d inserted at the front\n", value);

7. Write a C function to insert an element at the rear end of the double

ended queue. .(5 marks)

void insertRear(int value) {

if (rear == MAX - 1) {

printf("Queue is full\n");

} else {

if (front == -1) { // If queue is empty

front = rear = 0;

} else {

rear++;
}

queue[rear] = value;

printf("%d inserted at the rear\n", value);

8. Write a C function to delete an element at the rear end of the double

ended queue. .(5 marks)

void deleteRear() {

if (front == -1) {

printf("Queue is empty\n");

} else {

printf("%d deleted from the rear\n", queue[rear]);

if (front == rear) {

front = rear = -1; // Queue is empty

} else {

rear--;

9. Write a C function to delete an element at the front end of the

double ended queue. .(5 marks)

void deleteFront() {

if (front == -1) {

printf("Queue is empty\n");

} else {

printf("%d deleted from the front\n", queue[front]);

if (front == rear) {

front = rear = -1; // Queue is empty

} else {

front++;

10. Write an algorithm to delete an element at the front end of the


circular queue. .(5 marks)

Algorithm DeleteFrontCircularQueue

If (front == -1) Then

Print "Queue is empty"

Exit

End If

Print queue[front] "deleted from the front"

If (front == rear) Then

front = rear = -1

Else

front = (front + 1) % size

End If

End Algorithm

11. Write an algorithm to insert an element at the rear end of the

circular queue. .(5 marks)

Algorithm InsertRearCircularQueue(Element)

If ((rear + 1) % size == front) Then

Print "Queue is full"

Exit

End If

rear = (rear + 1) % size

queue[rear] = Element

End Algorithm

12. Explain the process and algorithm for deleting an element from the

linear queue. .(5 marks)

To delete an element from a linear queue:


Check if the queue is empty by verifying if front == -1.
If the queue is not empty, remove the element at the front.
Update the front pointer, and if it becomes greater than rear, reset both to -1 (queue is empty).
Algorithm Dequeue

If (front == -1) Then

Print "Queue is empty"

Exit

End If
Print queue[front] "deleted"

front++

If (front > rear) Then

front = rear = -1

End If

End Algorithm

13. Compare the circular queue and double ended queue. .(5 marks)

Feature Circular Queue Double Ended Queue

Can insert and delete only at


Insertion/Deletion Can insert and delete at both ends
rear/front

Memory Can be more complex to implement due to both


More efficient (reuses space)
Utilization ends being active

Fixed size but allows more flexible use of both


Size Fixed size with circular behavior
ends

Useful for buffering and circular


Applications Useful for both stack and queue applications
data structures

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.

Significance of Front Pointer:

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.

Significance of Rear Pointer:

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.

You might also like