forked from keshavsingh4522/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circular Queue.c
147 lines (142 loc) · 3 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include<stdio.h>
#include<stdlib.h>
#define n MAX_SIZE
int MAX_SIZE=10;
int cq[n];
int first=-1;
int last=-1;
void second(void)
{
system("cls"); //For Windows
// clrscr(); //For Mac/Linux
printf("\n=====================================================================================================================================");
printf("\n\t\t\t\t __ ___ __ __ __ __ __ ___ __ __ __ __ ___");
printf("\n\t\t\t\t|_/|__)| | ||_/ /\\ / _ /\\ |__)| | /\\ | /| (__\\ |__)|__) | / (_ / \\ /__ /");
printf("\n\t\t\t\t| \\| \\ | | || \\ /--\\\\__)/--\\| \\ |/\\|/--\\|__ | __/ |__)|__) | \\____) \\__/ \\__) / ");
printf("\n=====================================================================================================================================\n");
}
void ins()
{
int x,item;
if((first==0 && last==n-1) || (first==last+1))
{
printf("\n\t\t\t\tQueue Overflow!!\n");
return;
}
printf("\n\t\t\t\tHow many elements do you want to insert now? Note: Max Size Available is %d",n);
scanf("%d",&x);
for(int i=1;i<=x;i++)
{
printf("\n\t\t\t\tInput the element for insertion in queue at pos %d : ",i);
scanf("%d", &item);
if(first==-1)
{
first=0;
last=0;
}
else
{
if(last==n-1)
last=0;
else
last+=1;
}
cq[last]=item;
}
}
void del()
{
int x;
if(first==-1)
{
printf("\n\t\t\t\tQueue Underflow!!\n");
return ;
}
printf("\n\t\t\t\tHow many elements do you want to delete?: ");
scanf("%d",&x);
printf("\n\t\t\t\tElement deleted from queue : ");
for(int i=1;i<=x;i++)
{
printf(" %d ",cq[first]);
if(first==last)
{
first=-1;
last=-1;
}
else
{
if(first==n-1)
first=0;
else
first+=1;
}
}
}
void disp()
{
int first_pos=first,last_pos=last;
if(first==-1)
{
printf("\n\t\t\t\tQueue is empty!!\n");
return;
}
printf("\n\t\t\t\tQueue elements:: ");
if(first_pos<=last_pos)
{
while(first_pos<=last_pos)
{
printf(" %d ",cq[first_pos]);
first_pos++;
}
}
else
{
while(first_pos<=n-1)
{
printf(" %d ",cq[first_pos]);
first_pos++;
}
first_pos=0;
while(first_pos<=last_pos)
{
printf(" %d ",cq[first_pos]);
first_pos++;
}
}
printf("\n");
}
void main()
{
int choice,a;
second();
do
{
printf("\n\t\t\t\t===============Circular Queue Operation===============\n\n");
printf("\n\t\t\t\tEnter Max Size of Queue: ");
scanf("%d",&a);
n=a;
printf("\n\t\t\t\tMax Size Alloted to queue: %d",n);
printf("\n\t\t\t\t1.Insert");
printf("\n\t\t\t\t2.Delete");
printf("\n\t\t\t\t3.Display");
printf("\n\t\t\t\t4.Quit");
printf("\n\t\t\t\tEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
ins();
break;
case 2 :
del();
break;
case 3:
disp();
break;
case 4:
exit(0);
default:
printf("\n\t\t\t\tInvalid choice!!\n");
}
}while(choice!=4);
}