-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
87 lines (75 loc) · 1.39 KB
/
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
#include <stdio.h>
#define MAX 5
int array[MAX];
int front = -1, rear = -1;
void enqueue(int *array, int item, int *pfront, int *prear)
{
if(*prear == MAX-1)
{
printf("Queue is full\n");
return;
}
else
{
printf("Enqueuing: %d\n", item);
(*prear)++;
array[*prear] = item;
if(*pfront == -1)
{
*pfront = 0;
}
}
}
int empty(int *pfront)
{
if(*pfront == -1)
{
printf("Queue is empty\n");
return 1;
}
else
{
return 0;
}
}
int dequeue(int *array, int *pfront, int *prear)
{
int data = array[*pfront];
printf("Dequeueing: %d\n", data);
array[*pfront] = 0;
if(*pfront == *prear)
{
*pfront = *prear = -1;
}
else
{
(*pfront)++;
}
return data;
}
int main()
{
int array[MAX];
int front = -1, rear = -1;
enqueue(array, 23, &front, &rear);
enqueue(array, 9, &front, &rear);
enqueue(array, 11, &front, &rear);
enqueue(array, -10, &front, &rear);
enqueue(array, 25, &front, &rear);
enqueue(array, 16, &front, &rear);
enqueue(array, 17, &front, &rear);
enqueue(array, 22, &front, &rear);
enqueue(array, 19, &front, &rear);
enqueue(array, 30, &front, &rear);
enqueue(array, 32, &front, &rear);
int i;
for(int index=0; index<MAX; index++)
{
if(!empty(&front))
{
i = dequeue(array, &front, &rear);
printf("Receieved Dequeued item: %d\n", i);
}
}
return 0;
}