-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Priority_Queue_using_Linked_List.c
196 lines (161 loc) · 4.05 KB
/
Priority_Queue_using_Linked_List.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
AIM :: To implement Priority Queue (PQ) using Linked List (LL).
WHAT IS PRIORITY QUEUE ?
A priority queue is an abstract data type that behaves similarly to the normal queue except that each element has some priority,
i.e., the element with the highest priority would come first in a priority queue.
The priority of the elements in a priority queue will determine the order in which elements are removed from the priority queue.
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
int data; //for storing data
int prior; // for storing priority of the data
struct node *next; // for storing next node's address
} * front;
typedef struct node PQ;
//protorypes
void enq(); // for insertion
void deq(); // for deletion
void display(); // for display
int main()
{
int choice;
printf("\n\t\tPRIORITY QUEUE IMPLEMENTION USING LINKED LIST\n\n");
while (1)
{
printf("\nMAIN MENU\n");
printf("1. ENQ\n2. DEQ\n3. DISPLAY\n4. EXIT\n");
printf("Enter your choice :: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
enq();
break;
case 2:
deq();
break;
case 3:
display();
break;
case 4:
printf("\nexiting...\n");
exit(0);
break;
default:
printf("\nINVALID CHOICE :(\n");
break;
}
}
return 0;
}
void enq()
{
// creating new node
PQ *new_node = (PQ *)malloc(sizeof(PQ));
new_node->next = NULL;
//take data and priority of that data form the user
printf("\nenter data: ");
scanf("%d", &new_node->data);
printf("\nenter priority of %d: ", new_node->data);
scanf("%d", &new_node->prior);
// if queue is full then insert the new node at the beginning
if (front == NULL || new_node->prior < front->prior)
{ // insertion at beginning
new_node->next = front;
front = new_node;
}
else
{
PQ *temp = front;
//finding the appropriate position and insert the new node there
while (temp->next != NULL && temp->next->prior <= new_node->prior)
temp = temp->next;
new_node->next = temp->next;
temp->next = new_node;
}
display();
}
void deq()
{
//if queue is already empty then display error message
if (front == NULL)
printf("\nPRIORITY QUEUE IS EMPTY :(\n");
else
{
PQ *temp = front;
//every time deleting the first
front = front->next;
temp->next = NULL;
free(temp);
display();
}
}
void display()
{
if (front == NULL)
printf("\npriority queue is empty :(\n");
else
{
//temp_data to store and display the data of queue while traversing
PQ *temp_data = front;
//temp_priority to store and display the priority of data that are being displayed
PQ *temp_priority = front;
// displaying data in queue
printf("\nQUEUE :: ");
while (temp_data != NULL)
{
printf("%d ", temp_data->data);
temp_data = temp_data->next;
}
// displaying priority of all the data in the queue
printf("\nPRIORIOTY :: ");
while (temp_priority != NULL)
{
printf("%d ", temp_priority->prior);
temp_priority = temp_priority->next;
}
printf("\n");
}
}
/*
TEST CASE
PRIORITY QUEUE IMPLEMENTION USING LINKED LIST
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 1
Enter the data :: 30
Enter priority of 30 :: 2
DATA :: 30
PRIORITY :: 2
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 1
Enter the data :: 10
Enter priority of 10 :: 1
DATA :: 10 30
PRIORITY :: 1 2
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 2
DATA :: 30
PRIORITY :: 2
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 4
Exiting...
TIME COMPLEXITY
INSERTION :: O(n)
DELETION :: O(n)
DISPLAY :: O(n)
*/