forked from akshitagupta15june/100code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue_using_linklist.c
More file actions
104 lines (99 loc) · 1.97 KB
/
Copy pathpriority_queue_using_linklist.c
File metadata and controls
104 lines (99 loc) · 1.97 KB
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
99
100
101
102
103
104
#include <stdio.h>
#include<malloc.h>
struct node
{
int data;
int priority;
struct node *next;
};
struct node *front=NULL;
struct node *enqueue(int dat,int pri);
struct node *dequeue();
struct node *display();
struct node *enqueue(int dat,int pri)
{
struct node *newnode;
struct node *q;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=dat;
newnode->priority=pri;
if(front==NULL || pri<front->priority)
{
newnode->next=front;
front=newnode;
}
else
{
q=front;
while(q->next!=NULL && pri>=q->next->priority)
{
q=q->next;
}
newnode->next=q->next;
q->next=newnode;
}
}
struct node *dequeue()
{
struct node *temp;
temp=front;
if(front==NULL)
{
printf("\n QUEUE UNDERFLOW\n");
}
else
{
printf("\n The item which is deleted is %d ",temp->data);
front=front->next;
free(temp);
}
}
struct node *display()
{
struct node *temp;
temp=front;
if(front==NULL)
{
printf("\n QUEUE Empty");
}
else
{
printf("Queue is :\n");
printf("Priority Item\n");
while(temp!= NULL)
{
printf("%5d %5d\n",temp->priority,temp->data);
temp = temp->next;
}
}
}
int main()
{
int ch,dat,pri;
do
{
printf("\nEnter following keys\n1:ENQUEUE\n2:DEQUEUE\n3:display\n4:(0) to exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Input the item value to be added in the queue : ");
scanf("%d",&dat);
printf("Enter its priority : ");
scanf("%d",&pri);
enqueue(dat,pri);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
default:
printf("wrong choice");
}
}
while(ch!=0);
ch=getchar();
return 0;
}