-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityque.cpp
More file actions
104 lines (100 loc) · 1.54 KB
/
priorityque.cpp
File metadata and controls
104 lines (100 loc) · 1.54 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
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
int priority;
struct node *next;
}node;
typedef struct
{
node *first;
}pq;
void create(pq *q)
{
int ans,d,p;
while(1)
{
printf("\nPress 1 if you want to continue entering else any other key.\n");
scanf("%d",&ans);
if(ans!=1)
break;
node *nd,*temp=q->first;
nd=(node*)malloc(sizeof(node));
nd->next=NULL;
printf("\nenter data and priority:");
scanf("%d%d",&d,&p);
nd->priority=p;
nd->data=d;
if(q->first==NULL)
q->first=nd;
else if(q->first->priority>p)
{
nd->next=q->first;
q->first=nd;
}
else
{
while(temp->next!=NULL && temp->next->priority<p)
temp=temp->next;
if(temp->next==NULL)
{
temp->next=nd;
}
else
{
nd->next=temp->next;
temp->next=nd;
}
}
}
}
void display(pq *q)
{
node *t=q->first;
if(t==NULL)
{
printf("\nlist is empty.\n");
exit(3);
}
printf("\nDATA\tPRIORITY\n");
while(t!=NULL)
{
printf("%d\t%d\n",t->data,t->priority);
t=t->next;
}
}
int delq(pq *q)
{
if(q->first==NULL)
{
printf("\nlist is empty.\n");
exit(5);
}
node *t=q->first;
int d=t->data;
q->first=t->next;
free(t);
return d;
}
int main()
{
pq q;
q.first=NULL;
int ch;
while(1)
{
printf("\n\n1.Create Priority Que.\n2.Display.\n3.Delete\n4.Exit.\n\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:create(&q);
break;
case 2:display(&q);
break;
case 3: printf("%d is deleted",delq(&q));
break;
case 4: exit(2);
}
}
}