4 Queue
4 Queue
Title: Queue
• Part 1
1. Queue Implementation:
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. Dequeue elements from the queue and print the dequeued elements.
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];
1);
if (isFull())
return;
if (isEmpty())
rear++;
queue[rear] = element;
if (isEmpty())
return -1;
if (front == rear)
{
// Reset the queue if it's the last element front = rear = -1;
else
front++;
{ if (isEmpty())
return;
prin ("\n");
// Part 2: Queue Opera ons // Enqueue some elements into the queue
display();
dequeue(); dequeue();
display();
}
else
// Enqueue addi onal elements into the queue enqueue(60); enqueue(70); // This should fail
display();
return 0;
OUTPUT:
Queue elements: 30 40 50
PART 2:
CODE:
queue[MAX_SIZE];
// Func on to add an element to the circular queue (enqueue) void enqueue(int element)
{ if (isFull())
return;
if (isEmpty())
front = 0;
queue.\n", element);
// Func on to remove an element from the circular queue (dequeue) int dequeue()
if (isEmpty())
return -1;
if (front == rear)
else
}
prin ("%d dequeued from the queue.\n", dequeuedElement); return dequeuedElement;
{ if (isEmpty())
return;
MAX_SIZE;
int main()
enqueue(40); enqueue(50);
display();
enqueue(60); enqueue(70);
display(); return 0;
OUTPUT:
Queue elements: 30 40 50
60 enqueued to the queue.
Queue elements: 30 40 50 60 70