forked from codehouseindia/Everything
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular_Queue.c
More file actions
111 lines (105 loc) · 1.9 KB
/
Circular_Queue.c
File metadata and controls
111 lines (105 loc) · 1.9 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
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 size;
struct queue
{
int a[30];
int front;
int rear;
};
void enqueue(struct queue *p)
{ int item;
printf("Enter the item:");
scanf("%d",&item);
if(p->rear==-1)
{
p->rear++;
p->front++;
p->a[p->rear]=item;
}
else
{
int next;
next=(p->rear+1)%size;
p->a[++p->rear]=item;
if(next!=p->front)
{
p->rear=next;
p->a[p->rear]=item;
}
else
{
printf("Queue is full");
}
}
}
void dequeue(struct queue *p)
{
if(p->front==-1)
{
printf("Queue is empty");
}
else
{
int item;
item=p->a[p->front];
printf("Deleted item=%d",item);
if(p->rear==p->front)
{
p->front=-1;
p->rear=-1;
}
else
{
p->front=(p->front+1)%size;
}
}
}
void display(struct queue *p)
{
int i;
printf("Circular queue");
if(p->front>p->rear)
{
for(i=p->front;i<=size-1;i++)
printf("%d",p->a[i]);
for(i=0;i<p->rear;i++)
printf("%d",p->a[i]);
}
else
{
for(i=p->front; i<=p->rear; i++)
{
printf("%d ",p->a[i]);
}
printf("\n");
}
}
void main()
{
int ch,t=1;
struct queue q;
q.front=-1;
q.rear=-1;
printf("Enter the size:");
scanf("%d",&size);
while(t==1)
{
printf("Enter the choice\n 1.ENQUEUE\n 2.DEQUEUE\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqueue(&q);
display(&q);
break;
case 2:
dequeue(&q);
display(&q);
break;
default:
printf("Invalid choice\n");
}
printf("\nDo you want to continue \n1.yes 2.no\n");
scanf("%d",&t);
}
}