forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Priority_Queue_using_LinkedList.cpp
195 lines (159 loc) · 4.19 KB
/
Priority_Queue_using_LinkedList.cpp
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
/*
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 <iostream>
#include <stdlib.h> // for using malloc (DMA)
using namespace std;
struct node
{
int data; // to store data
int prior; // to store priority of data
struct node *next; // to store next node's pointer
} * front; // front pointer of PQ
typedef struct node PQ;
// prototypes
void enq(); // for insertion
void deq(); // for deletion
void display(); // for display
int main()
{
int choice; //for storing the choice of user after viewing the options
cout << "\n\t\tPQ IMPLEMENTATION" << endl;
while (true)
{
// give the menu to user
cout << "\n1. ENQ\n2. DEQ\n3. DISPLAY\n4. EXIT" << endl;
cout << "Enter your choice :: ";
cin >> choice;
switch (choice)
{
case 1:
enq();
break;
case 2:
deq();
break;
case 3:
display();
break;
case 4:
cout << "Exiting...";
exit(0);
break;
default:
cout << "Invalid choice :(" << endl;
}
}
return 0;
}
void enq()
{
//creating new node
PQ *new_node = (PQ *)malloc(sizeof(PQ));
new_node->next = NULL;
//ask user to enter the data and priority of that data
cout << "Enter the data :: ";
cin >> new_node->data; // storing data into data field of newly created node
cout << "Enter priority of " << new_node->data << " :: ";
cin >> new_node->prior; // storing priority of that data
// if linked list is empty or priority is less than first position
if (front == NULL || new_node->prior <= front->prior)
{ // then insert at begin
new_node->next = front;
front = new_node;
}
else
{
PQ *temp = front;
//traverse till the appropriate position
while (temp->next != NULL && temp->next->prior <= new_node->prior)
temp = temp->next;
//inseting node at it's appropriate position
new_node->next = temp->next;
temp->next = new_node;
}
display(); // display the result
}
void deq()
{
if (front == NULL) // means queue is empty
cout << "QUEUE UNDERFLOW" << endl;
else
{
PQ *temp = front;
//every time deleting the first
front = front->next;
temp->next = NULL;
free(temp);
display();
}
}
void display()
{
if (front == NULL) // means queue is empty
cout << "QUEUE UNDERFLOW" << endl;
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
cout << "\nDATA :: ";
while (temp_data != NULL)
{
cout << temp_data->data << " ";
temp_data = temp_data->next;
}
// displaying priority of all the data in the queue
cout << "\nPRIORITY :: ";
while (temp_priority != NULL)
{
cout << temp_priority->prior << " ";
temp_priority = temp_priority->next;
}
}
}
/*
TEST CASE
PQ IMPLEMENTATION
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)
*/