forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.cpp
61 lines (61 loc) · 1.05 KB
/
Queue.cpp
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
#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;
}