0% found this document useful (0 votes)
32 views5 pages

Ex 5

The document implements a priority queue using a linked list data structure in C. It defines a Process struct with process number and priority fields. Functions are declared to enqueue, dequeue, and print the queue. The main function displays a menu and calls the respective functions based on user input. Enqueue inserts nodes by comparing priority and dequeues removes the start node. Print queue traverses the list printing process numbers and priorities.

Uploaded by

CHINTAN UNDHAD
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
32 views5 pages

Ex 5

The document implements a priority queue using a linked list data structure in C. It defines a Process struct with process number and priority fields. Functions are declared to enqueue, dequeue, and print the queue. The main function displays a menu and calls the respective functions based on user input. Enqueue inserts nodes by comparing priority and dequeues removes the start node. Print queue traverses the list printing process numbers and priorities.

Uploaded by

CHINTAN UNDHAD
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Chintan Undhad

91900133035
DSA

#include<stdio.h>

#include<malloc.h>

//Creating Process Structure

struct Process

int processNo;

int priority;

struct Process *next;

};

struct Process *start=NULL, *head=NULL;

//Declaration of Functions

void displayMenu();

void Enqueue(); //Insert Node in Queue

void Dequeue();

void printQueue();

int main()

int op,val;

do

displayMenu();
printf("\n Select Operation : ");

scanf("%d",&op);

switch(op)

case 1:

Enqueue();

break;

case 2:

Dequeue();

break;

case 3:

printQueue();

break;

}while(op!=4);

return 0;

void displayMenu()

printf("\n=======================");

printf("\n Priority Queue Menu");

printf("\n=======================");

printf("\n 1 - Enqueue");

printf("\n 2 - Dequeue ");

printf("\n 3 - Print Queue ");

printf("\n 4 - Exit ");

printf("\n=======================");

void Enqueue()
{

struct Process *temp=NULL;

//New Process is Created

temp=(struct Process *)malloc(sizeof(struct Process)*1);

printf("\n Enter Process No :");

scanf("%d",&(temp->processNo));

printf("\n Enter Priority for Process :");

scanf("%d",&(temp->priority));

temp->next=NULL;

if(start==NULL)

start=temp;

else

// If new Process Priority is > first Process Priority

if(temp->priority < start->priority)

temp->next=start;

start = temp;

else

head=start;

while(head->next!=NULL && head->next->priority < temp->priority)

head=head->next;

temp->next=head->next;
head->next=temp;

void Dequeue()

struct Process *temp=NULL;

if(start==NULL)

printf("Queue is Empty");

else

temp=start;

start = start->next;

free(temp);

void printQueue()

if(start==NULL)

printf("\n Queue is Empty");

else

head = start;

while(head!=NULL)

printf("| %d | %d | -> ",head->processNo,head->priority);

head = head->next;
}

You might also like