-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLAB4_Q3.cpp
112 lines (79 loc) · 1.67 KB
/
LAB4_Q3.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
#include <iostream>
using namespace std;
class QueueArr{
public:
int a[100];
int t = 0;
int f = 1;
void enqueue(int value){
if(t<=100){
a[t+1]=value;
t++;
}
else{
cout<<"Array size is limited"<<endl;
}
}
void dequeue(){
if (t>0){
a[f]=0;
f=f+1;
}
else
cout<<"Queue has no element."<<endl;
}
int siz(){
return t-f+1;
}
bool isEmpty(){
if(t-f+1==0) return true;
return false;
}
int top(){
return a[t];
}
int btm(){
return a[f];
}
void display(){
for(int i=f;i<=t;i++){
cout<<a[i];
}
cout<<endl;
}
void pop(){
QueueArr q2;
int n = siz();
for(int i=0; i<n-1; i++){
int temp =btm();
q2.enqueue(temp);
dequeue();
}
dequeue();
for(int i=0; i<n-1; i++){
int temp = q2.btm();
enqueue(temp);
q2.dequeue();
}
display();
}
};
int main(){
QueueArr q1;
for(int i=1; i<10;i++){
q1.enqueue(i);
q1.display();
}
cout<<"Size: "<<q1.siz()<<endl;
cout<<"isEmpty: "<<q1.isEmpty()<<endl;
cout<<"Top element: "<<q1.top()<<endl;
cout<<endl;
q1.pop();
for(int i=1; i<10;i++){
q1.pop();
}
cout<<"Size: "<<q1.siz()<<endl;
cout<<"isEmpty: "<<q1.isEmpty()<<endl;
cout<<"Top element: "<<q1.top()<<endl;
return 0;
}