-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
81 lines (79 loc) · 1.14 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
typedef struct
{
int a[MAX];
int front;
int rear;
}cque;
void initque(cque *q)
{
q->front=0;
q->rear=0;
}
void insert(cque *q,int d)
{
if((q->rear+1)%MAX==q->front)
{
printf("queue is full");
exit(0);
}
q->rear=(q->rear+1)%MAX;
q->a[q->rear]=d;
}
int delt(cque *q)
{
if(q->front==q->rear)
{
printf("queue is empty");
exit(1);
}
q->front=(q->front+1)%MAX;
return q->a[q->front];
}
int peep(cque *q)
{
if(q->front==q->rear)
{
printf("queue is empty");
exit(1);
}
return q->a[q->rear];
}
void printque(cque *q)
{
int f;
f=q->front;
while(f!=q->rear)
{
f=(f+1)%MAX;
printf("%d\n",q->a[f]);
}
}
int main()
{
int ch,d;
cque q;
initque(&q);
while(1)
{
printf("\n\n1.Insert\n2.Delete\n3.Peep\n4.Print\n\n5.Exit\n");
printf("enter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nenter data:");
scanf("%d",&d);
insert(&q,d);
break;
case 2:printf("the deleted element is %d",delt(&q));
break;
case 3: printf("the peeped element is %d",peep(&q));
break;
case 4: printque(&q);
break;
case 5: exit(2);
}
}
}