-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueueArray.c
87 lines (86 loc) · 1.47 KB
/
queueArray.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>
#include <stdlib.h>
int x, size = 100, queue[100], rear = -1, front = -1;
void enqueue(int x);
void dequeue();
void display();
int main()
{
int option;
printf("Choose from menu\n");
printf("1.enqueue\n");
printf("2.dequeue\n");
printf("3.display\n");
while (1)
{
printf("Choose option\n");
scanf("%d", &option);
switch (option)
{
case 1:
printf("Enter data\n");
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
default:
printf("EXIT\n");
exit(1);
}
}
return 0;
}
void enqueue(int x)
{
if (rear == size - 1)
{
printf("overflow\n");
}
else if (rear == -1 && front == -1)
{
front = 0;
rear = 0;
queue[rear] = x;
}
else
{
rear++;
queue[rear] = x;
}
}
void dequeue()
{
if (front == -1 && rear == -1)
{
printf("Underflow\n");
}
else if (rear == front)
{
front = rear = -1;
}
else
{
front++;
printf("Element deleted\n");
}
}
void display()
{
if (front == -1 && rear == -1)
{
printf("Queue is empty\n");
}
else
{
for (int i = front; i <= rear; i++)
{
printf("%d ", queue[i]);
}
printf("\n");
}
}