/*program to implement priority queue*/
#include<stdio.h>
struct node
{
int data,prio;
struct node *link;
}*nw,*p,*temp,*front=NULL;
void insert()
{
struct node *temp;
nw=malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&nw->data);
printf("enter the priority of the item");
scanf("%d",&nw->prio);
nw->link=NULL;
if(front==NULL||nw->prio<front->prio)
{
nw->link=front;
front=nw;
}
else
{
temp=front;
while(temp->link!=NULL && temp->link->prio<=nw->prio)
temp=temp->link;
nw->link=temp->link;
temp->link=nw;
}
}
void disp()
{
p=front;
if(front==NULL)
printf("queue underflow");
else
{
printf("the data with its priority ");
while(p!=NULL)
{
printf("%d-%d->",p->data,p->prio);
p=p->link;
}
}
}
void del()
{
if(front==NULL)
printf("queue underflow");
else
{
temp=front;
front=front->link;
printf("the deleted element is %d",temp->data);
free(temp);
}
}
void main()
{
int ch,s;
do
{
printf("select your choice\n");
printf("1.insert\n");
printf("2.delete\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();break;
case 2: del();break;
case 3: disp();break;
case 4: exit(0);
default: printf("invalid choice");
}
}while(ch!=4);
}
................................OUTPUT....................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice1
enter the data9
enter the priority of the item1
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice1
enter the data5
enter the priority of the item3
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice8
invalid choice
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice1
enter the data4
enter the priority of the item2
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice3
the data with its priority 9-1->4-2->5-3->
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice2
the deleted element is 9
#include<stdio.h>
struct node
{
int data,prio;
struct node *link;
}*nw,*p,*temp,*front=NULL;
void insert()
{
struct node *temp;
nw=malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&nw->data);
printf("enter the priority of the item");
scanf("%d",&nw->prio);
nw->link=NULL;
if(front==NULL||nw->prio<front->prio)
{
nw->link=front;
front=nw;
}
else
{
temp=front;
while(temp->link!=NULL && temp->link->prio<=nw->prio)
temp=temp->link;
nw->link=temp->link;
temp->link=nw;
}
}
void disp()
{
p=front;
if(front==NULL)
printf("queue underflow");
else
{
printf("the data with its priority ");
while(p!=NULL)
{
printf("%d-%d->",p->data,p->prio);
p=p->link;
}
}
}
void del()
{
if(front==NULL)
printf("queue underflow");
else
{
temp=front;
front=front->link;
printf("the deleted element is %d",temp->data);
free(temp);
}
}
void main()
{
int ch,s;
do
{
printf("select your choice\n");
printf("1.insert\n");
printf("2.delete\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();break;
case 2: del();break;
case 3: disp();break;
case 4: exit(0);
default: printf("invalid choice");
}
}while(ch!=4);
}
................................OUTPUT....................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice1
enter the data9
enter the priority of the item1
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice1
enter the data5
enter the priority of the item3
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice8
invalid choice
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice1
enter the data4
enter the priority of the item2
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice3
the data with its priority 9-1->4-2->5-3->
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice2
the deleted element is 9
No comments:
Post a Comment