This repository was archived by the owner on Apr 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueu_array_resize.cpp
More file actions
137 lines (132 loc) · 1.73 KB
/
Copy pathqueu_array_resize.cpp
File metadata and controls
137 lines (132 loc) · 1.73 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
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
#include <iostream>
using namespace std;
class queue
{
int *Array;
int top, bottom, size, capasity;
public:
queue(int);
void push(int);
bool pop(int &);
int Size();
void resize(int );
bool isEmpty();
void print();
~queue();
};
queue::queue(int Capasity = 5)
{
capasity = Capasity;
Array = new int[capasity];
top = bottom = size = 0;
}
queue::~queue()
{
top = bottom = size = 0;
delete [] Array;
}
void queue::push(int item)
{
if (size == capasity)
resize(capasity * 2);
Array[top++] = item;
size++;
top = top % capasity;
}
bool queue::pop(int &out)
{
if (isEmpty())
return false;
if (size < capasity / 4)
resize(capasity / 2);
out = Array[bottom++];
size--;
bottom = bottom % capasity;
return true;
}
void queue::resize(int newCapasity)
{
int *newArray = new int[newCapasity];
int f, s, p = 0;
if (top <= bottom)
{
f = capasity;
s = top;
}
else
{
f = top;
s = 0;
}
for (int i = bottom; i < f; i++)
{
newArray[p] = Array[i];
p++;
}
for (int j = 0; j < s; j++)
{
newArray[p] = Array[j];
p++;
}
capasity = newCapasity;
bottom = 0;
top = size;
delete Array;
Array = newArray;
}
bool queue::isEmpty()
{
if (size == 0)
return true;
return false;
}
int queue::Size()
{
return size;
}
void queue::print()
{
int i = bottom;
while (i != top && i != capasity)
{
cout << Array[i] << " ";
i++;
}
if (i == capasity)
{
for (int j = 0; j < top; j++)
cout << Array[j] << " ";
}
cout << endl;
}
void pps(queue &q)
{
int a = 1;
cin >> a;
while (a != 0)
{
q.push(a);
cin >> a;
}
q.print();
}
void ppo(queue &q)
{
int a = 1;
cin >> a;
while (a != 1)
{
q.pop(a);
cout << "> " << a << endl;
cin >> a;
}
q.print();
}
int main()
{
queue Qu;
pps(Qu);
ppo(Qu);
pps(Qu);
return 0;
}