-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue2.c
108 lines (91 loc) · 1.96 KB
/
queue2.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
#include <Stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
struct CircularQueue
{
int front, rear;
int Queue[MAX_SIZE];
};
void initilize(struct CircularQueue *queue)
{
queue->front = -1;
queue->rear = -1;
}
int isFull(struct CircularQueue *queue)
{
return (queue->front == (queue->rear + 1) % MAX_SIZE);
}
int isEmpty(struct CircularQueue *queue)
{
return (queue->front == -1);
}
void Enqueue(struct CircularQueue *queue, int value)
{
if (isFull(queue))
{
printf("Queue Overflow\n");
return;
}
else
{
if (isEmpty(queue))
{
queue->front = 0;
}
queue->rear = (queue->rear + 1) % MAX_SIZE;
queue->Queue[queue->rear] = value;
printf("%d Is Enqueued To The Queue\n", value);
}
}
void dequeue(struct CircularQueue *queue)
{
if (isEmpty(queue))
{
printf("Queue Underflow\n");
return;
}
else
{
int value = queue->Queue[queue->front];
printf("%d Is Dequeued From The Queue\n", value);
if (queue->front == queue->rear)
{
initilize(queue);
}
else
{
queue->front = (queue->front + 1) % MAX_SIZE;
}
}
}
void Display(struct CircularQueue *queue)
{
if (isEmpty(queue))
{
printf("Queue Underflow\n");
return;
}
else
{
printf("Queue Elements\n");
int i = queue->front;
do
{
/* code */
printf("%d ", queue->Queue[i]);
i = (i + 1) % MAX_SIZE;
} while (i != (queue->rear + 1) % MAX_SIZE);
printf("\n");
}
}
int main()
{
struct CircularQueue *myQueue;
initilize(&myQueue);
Enqueue(&myQueue, 10);
Enqueue(&myQueue, 20);
Enqueue(&myQueue, 30);
Display(&myQueue);
dequeue(&myQueue);
Display(&myQueue);
}