Download as pdf or txt
Download as pdf or txt
You are on page 1of 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