-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathqueue.c
111 lines (100 loc) · 2.04 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
int front = -1;
int rear = -1;
int n;
int loop = 0;
void enqueue(int a[])
{
if (rear == -1 && front ==-1)
{
front = front +1;
rear = rear +1;
printf("Enter element \t");
scanf("%d",&a[rear]);
}
else if (rear == n-1)
{
printf("Queue overflow");
}
else
{
rear = rear +1;
printf("Enter element \t");
scanf("%d",&a[rear]);
}
}
void dequeue(int a[])
{
if (front == -1 && rear == -1)
{
printf("Queue underflow");
}
else if( front > rear || front > n-1)
{
printf("Queue is empty");
}
else
{
printf("Element removed is %d", a[front]);
front ++;
}
}
void size()
{
int size = rear-front +1;
printf("Size of the queue is %d",size);
}
void display(int a[])
{
if(front == -1 && rear == -1)
{
printf("Queue is empty");
}
else
{
printf("Elements in the queue are: ");
for(int i = front; i <= rear; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
}
void exit_queue()
{
loop = 1;
}
void main()
{
printf("Enter size of the queue \t");
scanf("%d",&n);
int a[n];
int choice;
while (loop == 0)
{
printf("\n");
printf("Menu \n 1.Enqueue \n 2.Dequeue \n 3.Size \n 4.Display \n 5.Exit \n Enter your choice (1-5): \t");
scanf("%d",&choice);
switch (choice)
{
case (1):
enqueue(a);
break;
case (2):
dequeue(a);
break;
case (3):
size();
break;
case (4):
display(a);
break;
case (5):
exit_queue();
break;
default:
printf("Invalid choice ");
break;
}
}
}