-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority
98 lines (95 loc) · 1.51 KB
/
priority
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int priority;
int info;
struct node*link;
};
struct node*front=NULL;
void insert();
void delete();
void display();
int main()
{
int choice;
while(1)
{
printf("\nENTER YOUR CHOICE: \n1.INSERT \n2.DISPLAY \n3.DELETE \n4.EXIT\n");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: display();
break;
case 3: delete();
break;
case 4: exit(0);
default: printf("INVALID CHOICE!!!!");
}
}
return(0);
}
void insert()
{
struct node*tmp;
struct node*q;
int added_item, item_priority;
tmp=(struct node*)malloc(sizeof(struct node));
printf("ENTER THE ITEM VALUE TO BE ADDED:");
scanf("%d",&added_item);
printf("ENTER ITS PRIORITY?:");
scanf("%d",&item_priority);
tmp->info=added_item;
tmp->priority=item_priority;
if(front==NULL || item_priority < front->priority)
{
tmp->link=front;
front=tmp;
}
else
{
q=front;
while(q->link!=NULL && q->link->priority <= item_priority)
{
q=q->link;
}
tmp->link=q->link;
q->link=tmp;
}
}
void delete()
{
struct node*tmp;
if(front==NULL)
{
printf("!!!QUEUE UNDERFLOW!!!\n");
}
else
{
tmp=front;
printf("Deleted item is %d \n",tmp->info);
front=front->link;
free(tmp);
}
}
void display()
{
struct node*ptr;
ptr=front;
if(front==NULL)
{
printf("!!!QUEUE IS EMPLY!!!");
}
else
{
printf(" ITEM WITH ITS PRIORITY IS :\n");
while(ptr!=NULL)
{
printf("%5d %5d \n",ptr->priority,ptr->info);
ptr=ptr->link;
}
}
}