-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular queue.c
103 lines (102 loc) · 1.41 KB
/
circular 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
//Implement push & pop algorithm on circular queue.
//CODE:-
#include<stdio.h>
#define max 5
void push(int []);
void pop(int []);
void display(int []);
int f = -1, r = -1;
void main()
{
int S[max],choice;
do
{
printf("\nEnter Your Choice :");
printf("\n(1) Push\n(2) Pop\n(3) Display\n(4) Exit\nEnter Choice From given above :");
scanf("%d",&choice);
switch(choice)
{
case 1 : push(S);
break;
case 2 : pop(S);
break;
case 3 : display(S);
break;
default: exit(0);
}
}while(choice>0&&choice<4);
}
void push(int s[])
{
int x;
if((r==max-1 && f==0) || (r==f-1))
{
printf("Queue is Overflow!!!.\n");
}
else
{
if(f==-1 && r==-1)
{
f++;
r++;
}
else if(r==max-1 && f!=0)
{
r = 0;
}
else
{
r++;
}
printf("\nEnter Value to Be Pushed :");
scanf("%d",&x);
s[r] = x;
}
}
void pop(int s[])
{
if(f==-1)
{
printf("\nQueue is Underflow!!!");
}
else
{
printf("%d is Popped.",s[f]);
if(f==r)
{
f = -1;
r = -1;
}
else if(f == max-1)
{
f = 0;
}
else
{
f++;
}
}
}
void display(int s[])
{
int i;
if(f < r)
{
for(i=f;i<=r;i++)
{
printf("\nS[%d] = %d",i,s[i]);
}
}
else
{
for(i=f;i<max;i++)
{
printf("\nS[%d] = %d",i,s[i]);
}
for(i=0;i<=r;i++)
{
printf("\nS[%d] = %d",i,s[i]);
}
}
printf("\n");
}