C PROGRAMMINGDATA STRUCTURES

priority queue using linked list data structure

Linked list priority queue

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

struct node
{
int roll;
char name[20];
int priority;
struct node *link;
};

struct node *front=NULL;
struct node *rear=NULL;
struct node *cur=NULL;

// Queuee fuctions

void enque(int data,int pri, char name[]) //Enque function
{
struct node *temp;
/*
if(limit==count) // condition to check if queue is full.
{
printf("Queue is fulln");
return;
}*/

temp=(struct node *)malloc(sizeof(struct node));
temp->roll=data;
strcpy(temp->name,name);
temp->priority=pri;
temp->link=NULL;
if(rear==NULL)
{
front=temp;
rear=temp;
}
else
{
if( front == NULL || pri < front->priority )
{
temp->link = front;
front = temp;
}
else
{
cur = front;
while( cur->link != NULL && cur->priority <= pri )
cur=cur->link;
temp->link = cur->link;
cur->link = temp;
}
}
printf("%d %s enqued in Queue with priority %dnn",data,temp->name,pri);
//count++;
}


//deque
void deque()
{
struct node *cur=front;
if(front==NULL)
{
printf("n Queue is Emptynn ");
return;
}
else
{
front=front->link;
}
printf("n%d dequed from Queue with priority %d n",cur->roll,cur->priority);
free(cur);
//count--;
}

void display()
{
struct node *cur;
cur = front;
if(front == NULL)
{
printf("nnQueue is emptyn");
}
else
{
printf("Priorityt roll no.tNamenn");
while(cur != NULL)
{
printf("%d tt%dtt%snn",cur->priority,cur->roll,cur->name);
cur = cur->link;
}
}
}


void main()
{

int choice;
char name[20];
int data;
int pri;

do
{
printf("nn******** Various Operation on priority queuee using Linked List Data Structure********nn Main Menu:nn");
printf("n Enter 1 : To Enque ");
printf("n Enter 2 : To Deque ");
printf("n Enter 3 : To Display queue elements");
printf("n Enter Any other Key to Exitn");
printf("nEnter Your Choice: nn-------------------------------nn");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("nnEnter Roll no to add in the Queue :nn");
scanf("%d",&data);
printf("nnEnter a name to add in the Queue :nn");
scanf("%s",&name);
printf("nEnter a its priority :n");
scanf("%d",&pri);
enque(data,pri,name);

break;

case 2:
deque();
break;


case 3:
display();
break;
default:
exit(0);
}

} while(choice);
}

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button