-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircular Queue.cpp
185 lines (179 loc) · 4.24 KB
/
Circular 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//On progress...
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
template <class TYPE>
class MyCircularQueue{
int capacity,head,tail,Size;
TYPE *q;
public:
MyCircularQueue()
{
head=0;
Size=0;
tail=0;
capacity=5;
q=(TYPE*)malloc(capacity*sizeof(TYPE));
}
void push(TYPE x) //push back
{
if(head==(tail%capacity) && Size!=0){
printf("Queue is full!!!\n");
}
else{
Size++;
q[tail]=x;
tail++;
tail=tail%capacity;
}
//cout<<"#Push head: "<<head<<" tail: "<<tail<<" Size: "<<Size<<endl;
}
TYPE pop() //pop front
{
TYPE x;
if(head== tail && Size==0)
{
printf("Queue is empty!!!\n");
return -1;
}
else{
x=q[head];
head++;
head=head%capacity;
Size--;
}
return x;
//cout<<"$Pop head: "<<head<<" tail: "<<tail<<" Size: "<<Size<<endl;
}
TYPE front()
{
return q[head];
}
void display()
{
printf("\nQueue is:\n");
if(tail==head && Size==0) printf("Queue is empty!!\n");
else if(tail==head && Size!=0)
{
cout<<q[head]<<" ";
int i=head+1;
i=i%capacity;
for(;i!=tail;i++,i=i%capacity)
{
cout<<q[i]<<" ";
}
}
else if(tail>head)
{
for(int i=head; i<tail;i++)
{
cout<<q[i]<<" ";
}
}
else {
for(int i=head; i!=tail;i++)
{
i=i%capacity;
cout<<q[i]<<" ";
}
}
printf("\n");
}
int Search(TYPE x)
{
if(tail==head && Size==0) return -1;
else if(tail==head && Size!=0)
{
if(q[head]==x) {
return head;
}
int i=head+1;
i=i%capacity;
for(;i!=tail;i++,i=i%capacity)
{
if(q[i]==x){
return i;
}
}
}
else if(tail>head)
{
for(int i=head; i<tail;i++)
{
if(q[i]==x){
return i;
}
}
}
else {
for(int i=head; i!=tail;i++)
{
i=i%capacity;
if(q[i]==x){
return i;
}
}
}
return -1;
}
void CapIncrease()
{
capacity=capacity+capacity;
q=(TYPE*)realloc(q,capacity*sizeof(TYPE));
tail=Size;
}
~MyCircularQueue()
{
free(q);
}
};
int main()
{
MyCircularQueue<int> qq;
int x,ans;
printf("1. Push element at the bottom of the Queue\n");
printf("2. Pop element from the top of the Queue\n");
printf("3. Print the Queue\n");
printf("4. Show the peek element of the Queue\n");
printf("5. Search an element in the Queue and show its position\n");
printf("6. Increase the capacity of Queue\n");
printf("7. Quit\n");
while(1)
{
scanf("%d",&ans);
if(ans==1)
{
printf("Value: ");
scanf("%d",&x);
qq.push(x);
}
else if(ans==2)
{
x=qq.pop();
if(x!=-1)cout<<x<<" has popped out!!\n";
}
else if(ans==3)
{
qq.display();
}
else if(ans==4)
{
cout<<"Peek value is: "<<qq.front()<<endl;
}
else if(ans==5)
{
printf("Value: ");
scanf("%d",&x);
x=qq.Search(x);
if(x!=-1) printf("Position is: %d\n",x);
else printf("Not found!\n");
}
else if(ans==6)
{
qq.CapIncrease();
}
else if(ans==7) break;
}
return 0;
}