-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue1.c
163 lines (136 loc) · 3.11 KB
/
queue1.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Queue
{
int front, rear, size;
unsigned capacity;
int *array;
};
struct Queue *createQueue(unsigned capacity)
{
struct Queue *queue = (struct Queue *)malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1;
queue->array = (int *)malloc(queue->capacity * sizeof(int));
return queue;
}
bool isEmpty(struct Queue *queue)
{
return queue->size == 0;
}
bool isFull(struct Queue *queue)
{
return queue->size == queue->capacity;
}
void Enqueue(struct Queue *queue, int value)
{
if (isFull(queue))
{
printf("Queue Overflow\n");
return;
}
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear] = value;
queue->size = queue->size + 1;
printf("%d enqueued to queue\n", value);
}
int Dequeue(struct Queue *queue)
{
if (isEmpty(queue))
{
printf("Queue Underflow\n");
return -1;
}
int value = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size = queue->size - 1;
return value;
}
int peek(struct Queue *queue)
{
if (isEmpty(queue))
{
printf("Stack Underflow\n");
return -1;
}
return queue->array[queue->front];
}
int rear(struct Queue *queue)
{
if (queue == NULL)
{
printf("Queue Underflow\n");
return -1;
}
return queue->array[queue->rear];
}
void display(struct Queue *queue)
{
printf("\nQueue Opertion\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Rear\n");
printf("4. peek\n");
printf("5. isEmpty\n");
printf("6. isFull\n");
printf("7. Exit\n");
printf("Enter Your Choice ? ");
}
int main()
{
int choice, value;
struct Queue *queue = createQueue(10);
while (1)
{
display(queue);
scanf("%d", &choice);
switch (choice)
{
case 1:
{
printf("Enter The Value ? ");
scanf("%d", &value);
Enqueue(queue, value);
break;
}
case 2:
{
printf("%d deequeued To Queue\n", Dequeue(queue));
break;
}
case 3:
{
printf("%d Is Peek Of Queue\n", rear(queue));
break;
}
case 4:
{
printf("%d Is Rear Of Queue\n", peek(queue));
break;
}
case 5:
{
printf("Is Queue Is Empty ? %s\n", isEmpty(queue) ? "Yes" : "No");
break;
}
case 6:
{
printf("Is Queue Is Full ? %s\n", isFull(queue) ? "Yes" : "No");
break;
}
case 7:
{
printf("Exiting Program.\n");
exit(0);
}
default:
{
printf("Invalid Input\n");
break;
}
}
}
return 0;
}