Assignment no.
Title: Queue
Part 1 and Part 2 are mandatory.
Part 3 is bonus. You can expect part 3 type ques ons in the end semester exam.
• Part 1
1. Queue Implementation:
a. Define a constant "MAX_SIZE" to represent the maximum size of the queue.
b. Define an integer array named "queue" with a size of "MAX_SIZE" to represent the queue.
c. Declare two integer variables named "front" and "rear" and initialize them to -1. These variables will track the front and rear of the queue,
respectively.
d. Create functions for the following queue operations:
• "enqueue" - adds an element to the rear of the queue.
• "dequeue" - removes an element from the front of the queue.
• "isEmpty" - checks if the queue is empty.
• "isFull" - checks if the queue is full.
• "display" - prints the elements of the queue.
e. Implement the functions mentioned above to perform the respective operations.
2. Queue Operations:
a. Create an empty queue using the defined queue implementation.
b. Enqueue some integer values into the queue.
c. Print the elements of the queue using the "display" function.
d. Dequeue elements from the queue and print the dequeued elements.
e. Check if the queue is empty and print the result.
f. Enqueue additional elements into the queue and print the updated queue.
• Part 2
1. Circular Queue: Implement a circular queue to optimize memory utilization. Modify the enqueue and dequeue functions to handle the circular behavior correctly.
2. Queue using Linked List: Implement a queue data structure using linked lists instead of an array. Create functions to enqueue, dequeue, and display elements in the linked list-based queue.
3. Priority Queue: Implement a priority queue, where each element has an associated priority (or assume it’s value as it’s priority). Elements with higher priority are dequeued before elements with
lower priority.
PART 1:
CODE:
#include <stdio.h>
#define MAX_SIZE 5 // Maximum size of the queue // Queue array and front, rear variables
int queue[MAX_SIZE];
int front = -1; int rear = -1;
// Func on to check if the queue is empty int isEmpty(){ return (front == -
1);
// Func on to check if the queue is full int isFull(){
return (rear == MAX_SIZE - 1);
// Func on to add an element to the queue (enqueue) void enqueue(int element){
if (isFull())
prin ("Queue is full! Cannot enqueue %d\n", element);
return;
if (isEmpty())
front = 0; // Ini alize front when the first element is enqueued
rear++;
queue[rear] = element;
prin ("%d enqueued to the queue.\n", element);
// Func on to remove an element from the queue (dequeue) int dequeue(){
if (isEmpty())
prin ("Queue is empty! Cannot dequeue.\n");
return -1;
int dequeuedElement = queue[front];
if (front == rear)
{
// Reset the queue if it's the last element front = rear = -1;
else
front++;
prin ("%d dequeued from the queue.\n", dequeuedElement); return dequeuedElement;
// Func on to display the elements of the queue void display()
{ if (isEmpty())
prin ("Queue is empty!\n");
return;
prin ("Queue elements: ");
for (int i = front; i <= rear; i++)
prin ("%d ", queue[i]);
prin ("\n");
// Main func on to demonstrate queue opera ons int main()
// Part 2: Queue Opera ons // Enqueue some elements into the queue
enqueue(10); enqueue(20); enqueue(30); enqueue(40); enqueue(50);
// Display the queue
display();
// Dequeue elements from the queue
dequeue(); dequeue();
// Display the queue a er dequeue opera ons
display();
// Check if the queue is empty if (isEmpty())
prin ("The queue is empty.\n");
}
else
prin ("The queue is not empty.\n");
// Enqueue addi onal elements into the queue enqueue(60); enqueue(70); // This should fail
since the queue is full
// Display the updated queue
display();
return 0;
OUTPUT:
10 enqueued to the queue.
20 enqueued to the queue.
30 enqueued to the queue.
40 enqueued to the queue.
50 enqueued to the queue. Queue elements: 10 20 30 40 50
10 dequeued from the queue.
20 dequeued from the queue.
Queue elements: 30 40 50 The queue is not empty.
Queue is full! Cannot enqueue 60
Queue is full! Cannot enqueue 70
Queue elements: 30 40 50
=== Code Execu on Successful ===
PART 2:
CODE:
#include <stdio.h> #define MAX_SIZE 5 int
queue[MAX_SIZE];
int front = -1; int rear = -1;
// Func on to check if the queue is empty int isEmpty()
return (front == -1);
}
// Func on to check if the queue is full int isFull()
return ((rear + 1) % MAX_SIZE == front);
// Func on to add an element to the circular queue (enqueue) void enqueue(int element)
{ if (isFull())
prin ("Queue is full! Cannot enqueue %d\n", element);
return;
if (isEmpty())
front = 0;
rear = (rear + 1) % MAX_SIZE; queue[rear] = element; prin ("%d enqueued to the
queue.\n", element);
// Func on to remove an element from the circular queue (dequeue) int dequeue()
if (isEmpty())
prin ("Queue is empty! Cannot dequeue.\n");
return -1;
int dequeuedElement = queue[front];
if (front == rear)
front = rear = -1;
else
front = (front + 1) % MAX_SIZE;
}
prin ("%d dequeued from the queue.\n", dequeuedElement); return dequeuedElement;
// Func on to display the elements of the circular queue void display()
{ if (isEmpty())
prin ("Queue is empty!\n");
return;
prin ("Queue elements: ");
int i = front; while (i != rear)
prin ("%d ", queue[i]); i = (i + 1) %
MAX_SIZE;
prin ("%d\n", queue[rear]);
int main()
enqueue(10); enqueue(20); enqueue(30);
enqueue(40); enqueue(50);
display();
dequeue(); dequeue(); display();
enqueue(60); enqueue(70);
display(); return 0;
OUTPUT:
10 enqueued to the queue.
20 enqueued to the queue.
30 enqueued to the queue.
40 enqueued to the queue.
50 enqueued to the queue. Queue elements: 10 20 30 40 50 10
dequeued from the queue.
20 dequeued from the queue.
Queue elements: 30 40 50
60 enqueued to the queue.
70 enqueued to the queue.
Queue elements: 30 40 50 60 70
=== Code Execu on Successful ===