Assignment 4: Queue
Code Implementation
Part – 1
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 5 // Define the maximum size of the queue
int queue[MAX_SIZE]; // Declare the queue array
int front = -1; // Track the front of the queue
int rear = -1; // Track the rear of the queue
// Function to check if the queue is empty
bool isEmpty() {
return (front == -1);
}
// Function to check if the queue is full
bool isFull() {
return (rear == MAX_SIZE - 1);
}
// Function to add an element to the queue
void enqueue(int value) {
if (isFull()) {
printf("Queue is full! Cannot enqueue %d\n", value);
return;
}
if (isEmpty()) {
front = 0; // Initialize front when the first element is added
}
rear++;
queue[rear] = value;
printf("%d enqueued to the queue\n", value);
}
// Function to remove an element from the queue
int dequeue() {
if (isEmpty()) {
printf("Queue is empty! Cannot dequeue\n");
return -1;
}
int dequeuedValue = queue[front];
printf("%d dequeued from the queue\n", dequeuedValue);
if (front == rear) {
// Reset queue if all elements are dequeued
front = rear = -1;
} else {
front++;
}
return dequeuedValue;
}
// Function to display the elements of the queue
void display() {
if (isEmpty()) {
printf("Queue is empty!\n");
return;
}
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
// Main function to demonstrate queue operations
int main() {
// 2a. Create an empty queue
// Queue is already initialized as empty with front = -1 and rear = -1.
// 2b. Enqueue some integer values into the queue
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
// 2c. Display the elements of the queue
display();
// 2d. Dequeue elements from the queue and print them
dequeue();
dequeue();
// Display the queue after dequeuing elements
display();
// 2e. Check if the queue is empty
if (isEmpty()) {
printf("Queue is empty!\n");
} else {
printf("Queue is not empty!\n");
}
// 2f. Enqueue additional elements into the queue
enqueue(60);
enqueue(70); // Should display "Queue is full"
// Display the updated queue
display();
return 0; }
Part – 2
1. Circular Queue
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 5 // Define the maximum size of the circular queue
int queue[MAX_SIZE];
int front = -1, rear = -1;
// Function to check if the circular queue is empty
bool isEmpty() {
return (front == -1);
}
// Function to check if the circular queue is full
bool isFull() {
return ((rear + 1) % MAX_SIZE == front);
}
// Function to add an element to the circular queue
void enqueue(int value) {
if (isFull()) {
printf("Circular Queue is full! Cannot enqueue %d\n", value);
return;
}
if (isEmpty()) {
front = 0;
}
rear = (rear + 1) % MAX_SIZE;
queue[rear] = value;
printf("%d enqueued to the circular queue\n", value);
}
// Function to remove an element from the circular queue
int dequeue() {
if (isEmpty()) {
printf("Circular Queue is empty! Cannot dequeue\n");
return -1;
}
int dequeuedValue = queue[front];
if (front == rear) {
// Reset the queue when all elements are dequeued
front = rear = -1;
} else {
front = (front + 1) % MAX_SIZE;
}
printf("%d dequeued from the circular queue\n", dequeuedValue);
return dequeuedValue;
}
// Function to display the elements of the circular queue
void display() {
if (isEmpty()) {
printf("Circular Queue is empty!\n");
return;
}
printf("Circular Queue elements: ");
int i = front;
do {
printf("%d ", queue[i]);
i = (i + 1) % MAX_SIZE;
} while (i != (rear + 1) % MAX_SIZE);
printf("\n");
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
display();
dequeue();
dequeue();
display();
enqueue(60);
enqueue(70);
display();
return 0;
}
2. Queue using Linked List
#include <stdio.h>
#include <stdlib.h>
// Structure for the node
struct Node {
int data;
struct Node *next;
};
// Pointers to front and rear of the queue
struct Node *front = NULL, *rear = NULL;
// Function to check if the linked list queue is empty
int isEmpty() {
return (front == NULL);
}
// Function to add an element to the linked list queue
void enqueue(int value) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (isEmpty()) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("%d enqueued to the linked list queue\n", value);
}
// Function to remove an element from the linked list queue
int dequeue() {
if (isEmpty()) {
printf("Linked list queue is empty! Cannot dequeue\n");
return -1;
}
int dequeuedValue = front->data;
struct Node *temp = front;
front = front->next;
free(temp);
if (front == NULL) {
rear = NULL;
}
printf("%d dequeued from the linked list queue\n", dequeuedValue);
return dequeuedValue;
}
// Function to display the linked list queue
void display() {
if (isEmpty()) {
printf("Linked list queue is empty!\n");
return;
}
struct Node *temp = front;
printf("Linked list queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
dequeue();
display();
enqueue(40);
enqueue(50);
display();
return 0;
}
3. Priority Queue
#include <stdio.h>
#include <stdlib.h>
// Structure for the node with priority
struct Node {
int data;
struct Node *next;
};
// Pointers to the front of the priority queue
struct Node *front = NULL;
// Function to add an element to the priority queue
void enqueue(int value) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
// If the queue is empty or new node has higher priority
if (front == NULL || front->data < value) {
newNode->next = front;
front = newNode;
} else {
struct Node *temp = front;
// Traverse to find the correct position based on priority
while (temp->next != NULL && temp->next->data >= value) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
printf("%d enqueued to the priority queue\n", value);
}
// Function to remove the highest priority element from the priority queue
int dequeue() {
if (front == NULL) {
printf("Priority queue is empty! Cannot dequeue\n");
return -1;
}
int dequeuedValue = front->data;
struct Node *temp = front;
front = front->next;
free(temp);
printf("%d dequeued from the priority queue\n", dequeuedValue);
return dequeuedValue;
}
// Function to display the elements of the priority queue
void display() {
if (front == NULL) {
printf("Priority queue is empty!\n");
return;
}
struct Node *temp = front;
printf("Priority queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
enqueue(30);
enqueue(20);
enqueue(50);
enqueue(40);
display();
dequeue();
dequeue();
display();
enqueue(60);
display();
return 0;
}
Sample and Significant Outputs
Part 1
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
Queue is not empty!
Queue is full! Cannot enqueue 60
Queue is full! Cannot enqueue 70
Queue elements: 30 40 50
Part 2
10 enqueued to the circular queue
20 enqueued to the circular queue
30 enqueued to the circular queue
40 enqueued to the circular queue
50 enqueued to the circular queue
Circular Queue elements: 10 20 30 40 50
10 dequeued from the circular queue
20 dequeued from the circular queue
Circular Queue elements: 30 40 50
60 enqueued to the circular queue
70 enqueued to the circular queue
Circular Queue elements: 30 40 50 60 70
10 enqueued to the linked list queue
20 enqueued to the linked list queue
30 enqueued to the linked list queue
Linked list queue elements: 10 20 30
10 dequeued from the linked list queue
20 dequeued from the linked list queue
Linked list queue elements: 30
40 enqueued to the linked list queue
50 enqueued to the linked list queue
Linked list queue elements: 30 40 50
30 enqueued to the priority queue
20 enqueued to the priority queue
50 enqueued to the priority queue
40 enqueued to the priority queue
Priority queue elements: 50 40 30 20
50 dequeued from the priority queue
40 dequeued from the priority queue
Priority queue elements: 30 20
60 enqueued to the priority queue
Priority queue elements: 60 30 20