-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathAscendingPQ.c
186 lines (147 loc) · 3 KB
/
AscendingPQ.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
/*
AIM ::TO IMPLEMENT ASCENDING PRIORITY QUEUE
WHAT IS ASCENDING PRIORITY QUEUE ?
In ascending order priority queue, a lower priority number is given as a higher priority.
For example, 1 to 5 arranged in an ascending order like,
then, the smallest number, i.e., 1 is given as the highest priority in a Ascending priority queue.
*/
#include <stdio.h>
#include <stdlib.h>
#define size 5
//Front for insertion an Rear for deletion
int Front = -1;
int Rear = -1;
int Queue[size];
//for checking whether Priority Queue is full or not
int q_full()
{
if (Rear == size - 1)
return 1;
return 0;
}
//for checking whether Priority Queue is empty or not
int q_empty()
{
if (Front == -1 || Front > Rear)
return 1;
return 0;
}
//to display the data in Priority Queue
void display()
{
if (q_empty())
printf("\nqueue is empty\n");
else
{
printf("\n");
for (int i = Front; i <= Rear; i++)
printf("%d ", Queue[i]);
printf("\n");
}
}
//for performing insertion
void enq()
{
if (q_full())
printf("\nQUEUE OVERFLOW");
else
{
int element, i;
printf("\nenter the element: ");
scanf("%d", &element);
//if inserting for firs time then do increment front also
if (Front == -1)
Front++;
//find appropriate place for the element and insert
for (i = Rear; i >= 0 && element < Queue[i]; i--)
Queue[i + 1] = Queue[i];
Queue[i + 1] = element;
Rear++;
display();
}
}
//for performing deletion
void deq()
{
if (q_empty())
printf("\nQUEUE UNDERFLOW\n");
else
{
int element = Queue[Front];
//if front and rear is equal means there was only one element left
if (Front == Rear)
Front = Rear = -1;
else
Front++;
display();
}
}
int main()
{
int choice;
printf("\n\n\tPRIORITY QUEUE IMPLEMENTION\n");
while (1)
{
printf("\nMAIN MENU\n");
printf("1. ENQ\n2. DEQ\n3. DISPLAY\n4. EXIT");
printf("\nenter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
enq();
break;
case 2:
deq();
break;
case 3:
display();
break;
case 4:
printf("\nexiting...\n\n");
exit(0);
break;
default:
printf("\ninvalid choice :(\n");
break;
}
}
return 0;
}
/*
TEST CASE
PRIORITY QUEUE IMPLEMENTION
MAIN MENU
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
enter your choice: 1
enter the element: 20
20
MAIN MENU
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
enter your choice: 1
enter the element: 10
10 20
MAIN MENU
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
enter your choice: 1
enter the element: 5
5 10 20
MAIN MENU
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
enter your choice: 4
exiting...
TIME COMPLEXITY :: O(n)
SPACE COMPLEXITY :: O(1)
*/