0% found this document useful (0 votes)
20 views4 pages

Exp 5

Uploaded by

2403fqcjkvyash
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)
20 views4 pages

Exp 5

Uploaded by

2403fqcjkvyash
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/ 4

Code :

#include <stdio.h>

#define MAX_QUEUE_SIZE 10

typedef struct {

int data[MAX_QUEUE_SIZE];

int priority[MAX_QUEUE_SIZE];

int rear;

} PriorityQueue;

// Function to initialize the priority queue

void initializeQueue(PriorityQueue* queue) {

queue->rear = -1;

// Function to check if the priority queue is empty

int isEmpty(PriorityQueue* queue) {

return queue->rear == -1;

// Function to check if the priority queue is full

int isFull(PriorityQueue* queue) {

return queue->rear == MAX_QUEUE_SIZE - 1;

// Function to add an element to the priority queue

void enqueue(PriorityQueue* queue, int value, int priority) {

if (isFull(queue)) {

printf("Queue is full. Cannot enqueue %d with priority %d.\n", value,

priority);

return;

int i;
for (i = queue->rear; i >= 0; i--) {

if (priority > queue->priority[i]) {

queue->data[i + 1] = queue->data[i];

queue->priority[i + 1] = queue->priority[i];

} else {

break;

queue->data[i + 1] = value;

queue->priority[i + 1] = priority;

queue->rear++;

printf("Enqueued %d with priority %d to the queue.\n", value, priority);

// Function to remove an element from the front of the priority queue

int dequeue(PriorityQueue* queue) {

if (isEmpty(queue)) {

printf("Queue is empty. Cannot dequeue.\n");

return -1;

int value = queue->data[queue->rear];

int priority = queue->priority[queue->rear];

queue->rear--;

printf("Dequeued %d with priority %d from the queue.\n", value, priority);

return value;

// Function to display the elements in the priority queue

void displayQueue(PriorityQueue* queue) {

if (isEmpty(queue)) {

printf("Queue is empty.\n");

return;
}

printf("Priority Queue elements:\n");

for (int i = queue->rear; i >= 0; i--) {

printf("Value: %d, Priority: %d\n", queue->data[i], queue->priority[i]);

int main() {

PriorityQueue queue;

initializeQueue(&queue);

enqueue(&queue, 10, 2);

enqueue(&queue, 20, 1);

enqueue(&queue, 30, 3);

enqueue(&queue, 40, 2);

enqueue(&queue, 50, 4);

displayQueue(&queue);

int dequeuedElement = dequeue(&queue);

printf("Dequeued element: %d\n", dequeuedElement);

displayQueue(&queue);

return 0;

OUTPUT :

Enqueued 10 with priority 2 to the queue.

Enqueued 20 with priority 1 to the queue.

Enqueued 30 with priority 3 to the queue.

Enqueued 40 with priority 2 to the queue.

Enqueued 50 with priority 4 to the queue.

Priority Queue elements:

Value: 50, Priority: 4

Value: 30, Priority: 3


Value: 10, Priority: 2

Value: 40, Priority: 2

Value: 20, Priority: 1

Dequeued 50 with priority 4 from the queue.

Priority Queue elements:

Value: 30, Priority: 3

Value: 10, Priority: 2

Value: 40, Priority: 2

Value: 20, Priority: 1

You might also like