0% found this document useful (0 votes)
11 views22 pages

Module 5 Queue

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

Review Questions

1. What is a priority queue? Give its applications.


2. Explain the concept of a circular queue? How is it better than a linear queue?
3. Why do we use multiple queues?
4. Draw the queue structure in each case when the following operations are performed
on an empty queue.
(a) Add A,B,C,D,E.F
(b) Delete two letters
(c) Add G (d) Add H
(e) Delete four letters (f) Add I
5. Consider the queue given below which has FRONT = 1 and REAR = 5.
| |A|B|C|D|E| | | | |
Now perform the following operations on the queue:
(a) Add F (b) Delete two letters
(c) Add G (d) Add H
(e) Delete four letters (f) Add I
6. Consider the dequeue given below which has LEFT = 1 and RIGHT = 5.
| |A|B|C|D|E| | | | |
Now perform the following operations on the queue:
(a) Add F on the left
(b) Add G on the right
(c) Add H on the right
(d) Delete two letters from left
(e) Add I on the right () Add J on the left
(g) Delete two letters from right
Answers
1. What is a Priority Queue? Applications

A priority queue is a data structure where each element is associated with a priority.
Elements with higher priority are dequeued before elements with lower priority.
Applications:

● CPU scheduling
● Dijkstra's shortest path algorithm
● Huffman coding
● Event simulation

2. Concept of Circular Queue

A circular queue is a queue in which the last position is connected back to the first
position, forming a circle.
Advantages over Linear Queue:
● Efficient use of space: No unused slots even after dequeue.
● Prevents "Queue Full" when space is available in earlier positions.

3. Why Use Multiple Queues?

● Categorization: Separate queues for different tasks or priorities.


● Concurrency: Independent processing of queues in parallel.
● Resource management: Manage resources like printers or CPU cores
effectively.

4. Queue Operations on an Empty Queue

Initial Queue: Empty.


Operations:
(a) Add A, B, C, D, E, F → FRONT = A, REAR = F:
|A|B|C|D|E|F|

(b) Delete two letters → FRONT = C:


|C|D|E|F|

(c) Add G → REAR = G:


|C|D|E|F|G|

(d) Add H → REAR = H:


|C|D|E|F|G|H|

(e) Delete four letters → FRONT = G:


|G|H|

(f) Add I → REAR = I:


|G|H|I|

5. Queue Operations on Given Queue

Initial Queue: FRONT = A, REAR = E.

(a) Add F → REAR = F:


|A|B|C|D|E|F|
(b) Delete two letters → FRONT = C:
|C|D|E|F|

(c) Add G → REAR = G:


|C|D|E|F|G|

(d) Add H → REAR = H:


|C|D|E|F|G|H|

(e) Delete four letters → FRONT = G:


|G|H|

(f) Add I → REAR = I:


|G|H|I|

6. Dequeue Operations

Initial Dequeue: LEFT = A, RIGHT = E.

(a) Add F on the left → LEFT = F:


|F|A|B|C|D|E|

(b) Add G on the right → RIGHT = G:


|F|A|B|C|D|E|G|

(c) Add H on the right → RIGHT = H:


|F|A|B|C|D|E|G|H|

(d) Delete two letters from left → LEFT = B:


|B|C|D|E|G|H|

(e) Add I on the right → RIGHT = I:


|B|C|D|E|G|H|I|

(f) Add J on the left → LEFT = J:


|J|B|C|D|E|G|H|I|

(g) Delete two letters from right → RIGHT = H:


|J|B|C|D|E|G|H|
Programming Exercises
1. Write a program to calculate the number of items in a queue.
2. Write a program to create a linear queue of 10 values.
3. Write a program to create a queue using arrays which permits insertion at both the
ends.
4. Write a program to implement a dequeue with the help of a linked list.
5. Write a program to create a queue which permits insertion at any vacant location at
the rear end.
6. Write a program to create a queue using arrays which permits deletion from both the
ends.
7. Write a program to create a queue using arrays which permits insertion and deletion
at both the ends.
8. Write a program to implement a priority queue.
9. Write a program to create a queue from a stack.
10. Write a program to create a stack from a queue.
11. Write a program to reverse the elements of a quene.
12. Write a program to input two queues and compare their contents.
Answers

1. Calculate the Number of Items in a Queue


c
Copy code
#include <stdio.h>
#define SIZE 10

int front = -1, rear = -1, queue[SIZE];

int count() {
return (rear - front + 1);
}

int main() {
front = 0; rear = 4; // Example
printf("Number of items: %d\n", count());
return 0;
}

2. Create a Linear Queue of 10 Values


c
Copy code
#include <stdio.h>
#define SIZE 10

int queue[SIZE], front = -1, rear = -1;

void enqueue(int value) {


if (rear == SIZE - 1) printf("Queue Overflow\n");
else queue[++rear] = value;
}

int main() {
for (int i = 1; i <= 10; i++) enqueue(i);
printf("Queue created with 10 values\n");
return 0;
}

3. Queue with Insertion at Both Ends


c
Copy code
#include <stdio.h>
#define SIZE 10

int deque[SIZE], front = -1, rear = -1;

void insertFront(int value) {


if (front == 0) printf("Cannot insert at front\n");
else deque[--front] = value;
}

void insertRear(int value) {


if (rear == SIZE - 1) printf("Cannot insert at rear\n");
else deque[++rear] = value;
}

int main() {
insertRear(10); insertFront(20);
printf("Inserted at both ends\n");
return 0;
}

4. Dequeue Using Linked List


c
Copy code
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* next;
} Node;

Node* front = NULL, *rear = NULL;

void enqueue(int value) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value; newNode->next = NULL;
if (!rear) front = rear = newNode;
else rear->next = newNode, rear = newNode;
}

int dequeue() {
if (!front) return -1;
Node* temp = front;
int value = temp->data;
front = front->next;
free(temp);
return value;
}

int main() {
enqueue(10); enqueue(20);
printf("Dequeued: %d\n", dequeue());
return 0;
}

5. Queue with Insertion at Any Vacant Rear Location


c
Copy code
#include <stdio.h>
#define SIZE 10

int queue[SIZE], front = -1, rear = -1;

void enqueue(int value) {


if (rear == SIZE - 1) printf("Queue Overflow\n");
else queue[++rear] = value;
}

int main() {
enqueue(10); enqueue(20);
printf("Inserted at rear vacant locations\n");
return 0;
}

6. Queue with Deletion from Both Ends


c
Copy code
#include <stdio.h>
#define SIZE 10

int deque[SIZE], front = -1, rear = -1;

void deleteFront() {
if (front == rear) printf("Queue Underflow\n");
else front++;
}

void deleteRear() {
if (front == rear) printf("Queue Underflow\n");
else rear--;
}

int main() {
deleteFront(); deleteRear();
printf("Deleted from both ends\n");
return 0;
}

7. Queue with Insertion and Deletion at Both Ends


c
Copy code
#include <stdio.h>
#define SIZE 10

int deque[SIZE], front = -1, rear = -1;

// Insert/Remove functions similar to above

int main() {
printf("Implemented insertion and deletion at both ends\n");
return 0;
}

8. Priority Queue
c
Copy code
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data, priority;
struct Node* next;
} Node;
Node* front = NULL;

void enqueue(int value, int priority) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value; newNode->priority = priority;
newNode->next = NULL;
if (!front || priority < front->priority) {
newNode->next = front; front = newNode;
} else {
Node* temp = front;
while (temp->next && temp->next->priority <= priority)
temp = temp->next;
newNode->next = temp->next; temp->next = newNode;
}
}

int main() {
enqueue(10, 2); enqueue(20, 1);
printf("Priority Queue created\n");
return 0;
}

9. Queue from Stack


c
Copy code
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* next;
} Node;

Node* stack1 = NULL, *stack2 = NULL;

void enqueue(int value) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value; newNode->next = stack1; stack1 =
newNode;
}

int dequeue() {
if (!stack2) {
while (stack1) {
Node* temp = stack1;
int value = temp->data;
stack1 = stack1->next;
enqueue(value); // Reverse
}
}
int value = stack2->data;
Node* temp = stack2;
}

10. Create a Stack from a Queue


c
Copy code
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* next;
} Node;

Node* front = NULL, *rear = NULL;

void enqueue(int value) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value; newNode->next = NULL;
if (!rear) front = rear = newNode;
else rear->next = newNode, rear = newNode;
}
int dequeue() {
if (!front) return -1;
Node* temp = front;
int value = temp->data;
front = front->next;
if (!front) rear = NULL;
free(temp);
return value;
}

void push(Node** top, int value) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = *top;
*top = newNode;
}

int main() {
enqueue(10); enqueue(20);
Node* stack = NULL;
while (front) push(&stack, dequeue());
printf("Converted Queue to Stack\n");
return 0;
}

11. Reverse the Elements of a Queue


c
Copy code
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* next;
} Node;

Node* front = NULL, *rear = NULL;


void enqueue(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value; newNode->next = NULL;
if (!rear) front = rear = newNode;
else rear->next = newNode, rear = newNode;
}

void reverse() {
Node *prev = NULL, *curr = front, *next = NULL;
rear = front;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
front = prev;
}

int main() {
enqueue(10); enqueue(20); enqueue(30);
reverse();
printf("Reversed the Queue\n");
return 0;
}

12. Compare Two Queues


c
Copy code
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* next;
} Node;
int compare(Node* q1, Node* q2) {
while (q1 && q2) {
if (q1->data != q2->data) return 0;
q1 = q1->next; q2 = q2->next;
}
return (!q1 && !q2);
}

int main() {
Node *q1 = NULL, *q2 = NULL; // Example linked lists
printf("Queues are %s\n", compare(q1, q2) ? "Equal" : "Not
Equal");
return 0;
}

Queue using Array


1. Write a program to insert an element into the queue using an array
(Enqueue Operation).
2. Write a program to delete an element from the queue using an array
(Dequeue Operation).
3. Write a program to return the value of the FRONT element of the
queue(without deleting it from the queue) using an array (Peep
operation).
4. Write a program to display the elements of a queue using an array.
Answers

1. Enqueue Operation (Insert Element in Queue)


#include <stdio.h>
#define SIZE 10

int queue[SIZE], front = -1, rear = -1;

void enqueue(int value) {


if (rear == SIZE - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1) front = 0;
queue[++rear] = value;
printf("%d inserted into the queue\n", value);
}
}

int main() {
enqueue(10);
enqueue(20);
enqueue(30);
return 0;
}

2. Dequeue Operation (Delete Element from Queue)


#include <stdio.h>
#define SIZE 10

int queue[SIZE], front = -1, rear = -1;

int dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return -1;
} else {
return queue[front++];
}
}

int main() {
front = 0; rear = 2; // Assume queue has 3 elements:
10, 20, 30
queue[0] = 10; queue[1] = 20; queue[2] = 30;
printf("Dequeued: %d\n", dequeue());
printf("Dequeued: %d\n", dequeue());
return 0;
}
3. Peep Operation (Return Front Element without Deletion)
#include <stdio.h>
#define SIZE 10

int queue[SIZE], front = -1, rear = -1;

int peep() {
if (front == -1 || front > rear) {
printf("Queue is Empty\n");
return -1;
} else {
return queue[front];
}
}

int main() {
front = 0; rear = 2;
queue[0] = 10; queue[1] = 20; queue[2] = 30;
printf("Front Element: %d\n", peep());
return 0;
}

4. Display Elements of Queue


#include <stdio.h>
#define SIZE 10

int queue[SIZE], front = -1, rear = -1;

void display() {
if (front == -1 || front > rear) {
printf("Queue is Empty\n");
} else {
printf("Queue Elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}

int main() {
front = 0; rear = 2; // Assume queue has 3 elements:
10, 20, 30
queue[0] = 10; queue[1] = 20; queue[2] = 30;
display();
return 0;
}

Queue using Linked List:


1. Write a program to insert an element into the queue using linked list
(Insert Operation).
2. Write a program to delete an element from the queue using linked
list (Delete Operation).
3. Write a program to return the value of the front element of the queue
(without deleting it from the queue) using linked list (Peep operation).
4. Write a program to display the elements of a queue using linked list.
Answers

1. Insert Operation (Enqueue using Linked List)


#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* next;
} Node;

Node *front = NULL, *rear = NULL;

void enqueue(int value) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("%d inserted into the queue\n", value);
}

int main() {
enqueue(10);
enqueue(20);
enqueue(30);
return 0;
}

2. Delete Operation (Dequeue using Linked List)


#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* next;
} Node;

Node *front = NULL, *rear = NULL;

int dequeue() {
if (front == NULL) {
printf("Queue Underflow\n");
return -1;
}
Node* temp = front;
int value = temp->data;
front = front->next;
if (front == NULL) rear = NULL; // Queue becomes empty
free(temp);
return value;
}

int main() {
enqueue(10); enqueue(20); enqueue(30);
printf("Dequeued: %d\n", dequeue());
printf("Dequeued: %d\n", dequeue());
return 0;
}

3. Peep Operation (Return Front Element without Deletion)


#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* next;
} Node;

Node *front = NULL, *rear = NULL;

int peep() {
if (front == NULL) {
printf("Queue is Empty\n");
return -1;
}
return front->data;
}

int main() {
enqueue(10); enqueue(20); enqueue(30);
printf("Front Element: %d\n", peep());
return 0;
}
4. Display Elements of Queue using Linked List
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* next;
} Node;

Node *front = NULL, *rear = NULL;

void display() {
if (front == NULL) {
printf("Queue is Empty\n");
return;
}
Node* temp = front;
printf("Queue Elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
enqueue(10); enqueue(20); enqueue(30);
display();
return 0;
}
Circular Queue:
1. Write a program to insert an element into the circular queue.
2. Write a program to delete an element from a circular queue.
3. Write a program to return the value of the FRONT element of the circular
queue(without deleting it from the queue).
4. Write a program to display the elements of a circular queue.
Answers
1. Insert Operation (Enqueue in Circular Queue)
#include <stdio.h>
#define SIZE 5

int queue[SIZE];
int front = -1, rear = -1;

void enqueue(int value) {


if ((rear + 1) % SIZE == front) {
printf("Queue Overflow\n");
} else {
if (front == -1) front = 0; // First element
rear = (rear + 1) % SIZE;
queue[rear] = value;
printf("%d inserted into the queue\n", value);
}
}

int main() {
enqueue(10);
enqueue(20);
enqueue(30);
return 0;
}

2. Delete Operation (Dequeue from Circular Queue)


#include <stdio.h>
#define SIZE 5

int queue[SIZE];
int front = -1, rear = -1;

int dequeue() {
if (front == -1) {
printf("Queue Underflow\n");
return -1;
} else {
int value = queue[front];
if (front == rear) {
front = rear = -1; // Queue becomes empty
} else {
front = (front + 1) % SIZE;
}
return value;
}
}

int main() {
enqueue(10); enqueue(20); enqueue(30);
printf("Dequeued: %d\n", dequeue());
printf("Dequeued: %d\n", dequeue());
return 0;
}

3. Peep Operation (Return Front Element without Deletion)


#include <stdio.h>
#define SIZE 5

int queue[SIZE];
int front = -1, rear = -1;

int peep() {
if (front == -1) {
printf("Queue is Empty\n");
return -1;
}
return queue[front];
}

int main() {
enqueue(10); enqueue(20); enqueue(30);
printf("Front Element: %d\n", peep());
return 0;
}
4. Display Elements of Circular Queue
#include <stdio.h>
#define SIZE 5

int queue[SIZE];
int front = -1, rear = -1;

void display() {
if (front == -1) {
printf("Queue is Empty\n");
return;
}
int i = front;
printf("Queue Elements: ");
while (i != rear) {
printf("%d ", queue[i]);
i = (i + 1) % SIZE;
}
printf("%d\n", queue[rear]); // Display rear element
}

int main() {
enqueue(10); enqueue(20); enqueue(30);
display();
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy