-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_queue.c
110 lines (91 loc) · 1.73 KB
/
priority_queue.c
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
105
106
107
108
109
110
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int info;
int priority;
struct node *next;
}node;
node *front=NULL;
node *rear=NULL;
void enqueue(int ele, int priority){
node *ptr= (node *)malloc(sizeof(node));
ptr->info=ele;
ptr->priority=priority;
ptr->next=NULL;
if(front==NULL){
front=ptr;
}
else{
if(priority>front->priority)
{
ptr->next=front;
front=ptr;
}
else{
node *temp;
temp=front;
while((temp->next)->priority>=priority && temp->next!=NULL){
temp=temp->next;
}
//printf("temp is %d\n",temp->info);
ptr->next=(temp->next);
temp->next=ptr;
}
}
}
void dequeue(){
if(front==NULL){
printf("queue underflow\n");
}
else{
front=front->next;
}
}
void display(){
node *temp;
temp=front;
printf("ELEMENTS:-\n");
while((temp->next)!=NULL){
printf("%d ",temp->info);
temp=temp->next;
}
printf("%d ",temp->info);
printf("\n");
temp=front;
printf("AND THEIR PRIORITY:-\n");
while((temp->next)!=NULL){
printf("%d ",temp->priority);
temp=temp->next;
}
printf("%d ",temp->priority);
printf("\n");
}
void main(){
while(1){
printf("enter 1 to enqueue\n");
printf("enter 2 to dequeue\n");
printf("enter 3 to display\n");
printf("enter 4 to exit\n");
int choice;
scanf("%d",&choice);
switch(choice){
case 1:
printf("enter the element to be enqueued\n");
int ele;
scanf("%d",&ele);
printf("enter its priority\n");
int priority;
scanf("%d",&priority);
enqueue(ele,priority);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(1);
}
}
}