-
Notifications
You must be signed in to change notification settings - Fork 2
/
Queue.c
91 lines (81 loc) · 1.63 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
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 10
void enQueue();
void deQueue();
void display();
int front = -1, rear = -1;
int items[SIZE];
int main()
{
bool repeat = true;
do
{
int option;
printf("\nMenu\n 1.Add item\n 2.Remove item\n 3.Display item\n 4.Exit\nEnter option:");
scanf("%d", &option);
switch (option)
{
case 1:
enQueue();
break;
case 2:
deQueue();
break;
case 3:
display();
break;
case 4:
repeat = false;
printf("\nThank for using this program!");
break;
default:
printf("Enter correct option!");
break;
}
} while (repeat);
}
void enQueue()
{
if (rear == SIZE - 1)
printf("Queue is full.");
else
{
if (front == -1)
front = 0;
++rear;
printf("Enter value to be inserted: ");
scanf("%d", &items[rear]);
printf("value inserted -> %d\n", items[rear]);
}
return;
}
void deQueue()
{
if (front == -1)
printf("Queue is empty.");
else
{
printf("Removed -> %d\n", items[front]);
items[front] = NULL;
++front;
if (front > rear)
front = rear = -1;
}
return;
}
void display()
{
if (rear == -1)
printf("\n Queue is empty.");
else
{
printf("\nQueue elements are -> ");
for (int i = front; i <= rear; ++i)
{
printf("%d ", items[i]);
}
printf("\n");
}
}