forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
86 lines (84 loc) · 1.51 KB
/
queue.cpp
File metadata and controls
86 lines (84 loc) · 1.51 KB
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
//
// Queue: the entities in the collection are kept in
// order and the principal (or only) operations on the
// collection are the addition of entities to the rear
// terminal position
//
// The All ▲lgorithms Project
//
// https://allalgorithms.com/data-scructures/
// https://github.com/allalgorithms/cpp
//
// Contributed by: ANUJ MODI
// Github: @descifrado
//
#include<stdio.h>
#define qsize 5
struct queue
{
int arr[qsize];
int f, r;
};
typedef struct queue Queue;
void
enqueue (Queue * q, int x)
{
if (q->r == qsize - 1)
printf ("QUEUE OVERFLOW\n");
else
q->arr[++q->r] = x;
}
int
dequeue (Queue * q)
{
if (q->f - q->r == 1)
printf ("Queue Underflow");
else
return (q->arr[q->f++]);
}
int
main ()
{
Queue q;
q.f = 0;
q.r = -1;
while (1)
{
printf("Enter 1 to Enqueue\nEnter 2 to Dequeue\nEnter 3 to Display All Elements\nEnter 0 to Exit\n");
int c;
scanf ("%d", &c);
switch (c)
{
case 0:
printf("Ended\n");
return 0;
case 1:
printf ("Enter Element to Enqueue\n");
int x;
scanf ("%d", &x);
enqueue (&q, x);
break;
case 2:
if (q.f - q.r == 1)
printf ("Queue Undeflow\n");
else
printf ("Dequeued Element is %d\n", dequeue (&q));
break;
case 3:
printf ("Elements of Queue Are\n");
if (q.f - q.r == 1)
{
printf ("Queue is Empty\n");
break;
}
int i;
for (i = q.f; i <= q.r; i++)
printf ("%d ", q.arr[i]);
printf ("\n");
break;
default:
printf ("Wrong Choice\n");
}
}
return 0;
}