-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueArrRe.cpp
More file actions
80 lines (69 loc) · 1.36 KB
/
queueArrRe.cpp
File metadata and controls
80 lines (69 loc) · 1.36 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
#include<bits/stdc++.h>
using namespace std;
/***************************************ORIGINAL aUTHOR*******************************
*************************************************************************************
*************************************************************************************
*************astrainL3gi0N**********************************************************/
class queueArr
{
int *Q;
int N;
int front = -1, rear = -1;
public:
queueArr(int max_size){
Q = new int[max_size];
N = max_size;
}
void enqueue(int item){
if(front == -1){
Q[++front] = item;
rear = front;
}
else if(rear >= N-1){
//resize
resize(N*2);
Q[++rear] = item;
}
else Q[++rear] = item;
}
int dequeue(){
int m;
if(front == -1){
cout<<"underflow\n";
m = -1;
}
else if(front == rear){
m = Q[front];
front = rear = -1;
}
else
m = Q[front++];
return m;
}
void resize(int n){
int *arr = new int[N];
for(int i = 0; i < N; ++i) arr[i] = Q[i];
Q = new int[2*N];
for(int i = 0; i < N; ++i) Q[i] = arr[i];
this->N = n;
}
void printArr(){
cout<<'\n';
for(int i = 0; i < N; ++i)
cout<<Q[i]<<' ';
cout<<'\n';
}
};
int main()
{
int n; cin>>n;
queueArr que(n);
for(int i = 0; i <= n; ++i){
int k; cin>>k;
que.enqueue(k);
}
for(int i = 0; i <= n; ++i){
cout<<que.dequeue()<<' ';
}
return 0;
}