-
Notifications
You must be signed in to change notification settings - Fork 0
/
prioQueue.c
166 lines (145 loc) · 2.59 KB
/
prioQueue.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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct Queue
{
int head;
int tail;
int size;
int a[51];
}Queue;
void create_Q(Queue *q)
{
q->head = q->tail = 0;
q->size = 50;
}
void enQ(Queue *q,int x)
{
if(q->tail < q->size)
q->a[++q->tail] = x;
if(q->head == 0)
q->head = 1;
}
/*void enQ_front(Queue *q,int x)
{
if(q->head == 1)
{
printf("Cannot enQ_front");
return;
}
if(q->head == 0)
{
q->a[1]=x;
q->head = q->tail = 1;
return;
}
q->a[--q->head]=x;
}*/
void enQ_priority(Queue* q, int x)
{
int i;
if(q->tail < q->size)
{
i = q->tail++;
while(i > 0 && q->a[i] > x)
q->a[i+1] = q->a[i--];
q->a[i+1] = x;
if (q->head == 0)
q->head = 1;
}
}
void deQ_priority(Queue* q, int x)
{
int i;
if (q->head==0)
{
printf("queue is empty\n");
return;
}
for(i=0;i<=q->tail;i++)
{
if (q->a[i]== x)
{
while(i<q->tail)
{
q->a[i]=q->a[i+1];
i++;
}
q->a[i+1]=-99;
q->tail--;
printf("Dequeued item\n");
if(q->tail==0)
q->head = 0;
return;
}
}
printf("element not present in queue\n");
return;
}
void deQ(Queue *q)
{
if(q->head < q->tail)
printf("Dequeued %d\n",q->a[q->head++]);
if(q->head == q->tail && q->head > 0)
{
printf("Dequeued %d\n",q->a[q->head]);
q->head = q->tail =0;
}
}
/*void deQ_rear(Queue *q)
{
if (q->head < q->tail)
{
printf("Dequeued %d\n",q->a[q->tail--]);
return;
}
if(q->head > 0)
{
printf("Dequeued %d\n",q->a[q->tail]);
q->head = q->tail = 0;
return;
}
}*/
void listQ(Queue *q)
{
int i;
if(q->head == 0)
printf("Queue is empty\n");
else
{
printf("The Queue is:\n");
for(i=q->head; i <= q->tail; i++)
printf("%d ", q->a[i]);
printf("\n");
}
}
bool isEmptyQ(Queue *q)
{
if(q->head == 0)
return true;
return false;
}
void main()
{
Queue q;
create_Q(&q);
enQ(&q,5);
enQ(&q,4);
enQ(&q,3);
enQ(&q,2);
enQ(&q,1);
listQ(&q);
deQ(&q);
deQ(&q);
listQ(&q);
enQ_front(&q,1);
listQ(&q);
deQ_rear(&q);
listQ(&q);
enQ(&q,7);
enQ_priority(&q,5);
listQ(&q);
deQ_priority(&q,1);
listQ(&q);
printf("Queue is empty? %d", isEmptyQ(&q));
}