-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeque.cpp
More file actions
243 lines (214 loc) · 8.7 KB
/
Deque.cpp
File metadata and controls
243 lines (214 loc) · 8.7 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <iostream>
#include <vector>
#include <array>
#include <string>
namespace csci2100{
template <typename ItemType>
class Deque{
private:
std::vector<ItemType> _dector;
int _back{0},_size{0},_capacity{4},_front{0};
void resize(){ //resize function used throughout to resize the vector when _size ==_capacity
if(_size != 0){
std::vector<ItemType> temp; //temp vector used to store _dector data
temp.resize(_capacity);//temp is made the size of the current capacity
for(int i = 0; i< _size; i++){ // for loop that copies the dector to the temp in order so _front ends up at index 0
temp[i] = _dector[_front];
if (_front == _capacity - 1){
_front = 0;
}
else{_front++;}
}
_capacity = _capacity*2; // _capacity is then mulitplied
_dector = temp;// the organized data is coppied into _dector
_dector.resize(_capacity);// dector's capacity is now twice that it was before
_front = 0;//_front is now the beginning of the dector
_back = _size-1;// _back is size -1
}}
public:
void push_front(ItemType item){
if(_dector.size()!=_capacity){ //used to make sure that the actual vector length matches the capacity
_dector.resize(_capacity);}
if (_size == _capacity){resize();} //if size has reached the limit, the resize function is used
if (_size == 0){ //if there are no items, then there is no need to change the _front location
_dector[_front] = item;
_size++;
}else if (_front == 0 && _size>0){ // if there is at least one item in the vector and _front == 0, _front will change the position to be _capacity -1
_front = _capacity - 1;
_dector[_front] = item;
_size++;
}
else if (_front >0){ //every other instance when _front is not 0, it will be going backwards in the vector
_front--;
_dector[_front] = item;
_size++;
}
}
void push_back(ItemType item){
if(_dector.size()!=_capacity){//same as push_front^
_dector.resize(_capacity);}
if (_size == _capacity){resize();}//same as push_front^
if(_size == 0){//if there is no other item, no need to change _back
_dector[_back] = item;
_size++;
}
if((_back == 0 && _size == 1)|| _back!=_capacity){ //either instances will require you to add one to _back
_back++;
_dector[_back]=item;
_size++;
}
}
void pop_front(){
if (_size > 0){//code will not run if there is no items in the vector
if (_size == _capacity){resize();}//same as push_front^^
if(_front == _capacity-1){ //if _front is _capacity -1, then _front needs to go back to the front of the vector
_front = 0;
_size--;
}else if(_front == _back){ //this only occurs when both front and back are equal to zero and that there is only one item in the list, when this happens, the vector will be cleared
_dector.clear();
_dector.resize(_capacity);
_size--;
}
else{_front++;_size--;} //every other instance will require _front to be increased
}
}
void pop_back(){
if(_size>0){//code wont run if there aren't any items
if (_size == _capacity){resize();}//^^
if(_back == 0 && _front > 0){//if back is equal to zero and _front is larger then back, then _back needs to go to the back of the vector
_back = _capacity-1;
_size--;
}else if(_back >0){ //in this case, _back needs to go down the vector
_back--;
_size--;
}if(_back == _front){ // same as pop_front^
_dector.clear();
_dector.resize(_capacity);
_size--;
}
}
}
void peek_front(){ //shows the front value only if there are items in the deque
if(_size!=0){
std::cout<< _dector[_front];}
std::cout << std::endl;
}
void peek_back(){//shows the back value only if there are items in the deque
if(_size!=0){
std::cout<< _dector[_back];}
std::cout << std::endl;
}
bool isEmpty(){
if (_size == 0){
return true;
}
return false;
}
int getLength(){
return _size;
}
void getDeque(){ //prints the deque
if (_size!=0){
int tempFront = _front;
for(int i = 0; i<_size;i++){
std::cout << _dector[_front] << " ";
if (_front == _capacity-1){_front = 0;}
else{_front++;}
}
std::cout << std:: endl;
_front = tempFront;}}
//MEANT FOR VISUALIZATION FOR TESTS
void getVector(){ //used to show how the deque is implemented in the tests
for (int i=0;i!=_capacity;i++){
std::cout << _dector.at(i) << " ";
}
std::cout << std::endl;
}
};
}
namespace csci2100{}
int main() {
csci2100::Deque<int> test;
test.push_front(8);
test.push_front(7);
test.push_back(6);
test.push_back(5);
std::cout << "TEST WITH INTEGERS" << std::endl;
std::cout << std::endl;
std::cout << "With push_front(8), push_front(7), push_back(6), push_back(5), the current Deque is: ";
test.getDeque();
std::cout << "Here is the front value (should be 7): "; test.peek_front();
std::cout << "Here is the back value (should be 5): "; test.peek_back();
std::cout << "Here is the current size (should be 4): " << test.getLength() << std::endl;
std::cout << "Here is the VECTOR without the organization: ";
test.getVector();
test.push_back(4);
std::cout << "When doing push_back(4) to the deque, the vector will resize and organize itself in a total of 8 spaces. Here is the VECTOR: ";
test.getVector();
std::cout << "Here is the Deque: ";
test.getDeque();
test.pop_back();
std::cout << "After pop_back(), the current deque is (should be 7,8,6,5): ";
test.getDeque();
test.pop_front();
std::cout << "After pop_front(), the current deque is (should be 8,6,5): ";
test.getDeque();
std::cout << "Here is the current size (should be 3): " << test.getLength() << std::endl;
std::cout << "Athough the deque is changed, the vector still has the values, but they will never be accessed again: ";test.getVector();
test.push_front(44);
test.push_back(99);
test.push_front(12);
std::cout << "After push_front(44), push_back(99), and push_front(12), the deque is: ";
test.getDeque();
std::cout << "The vector is: ";
test.getVector();
std::cout << "Is the deque empty?: "<< test.isEmpty() << std::endl;
test.pop_front();test.pop_front();test.pop_front();test.pop_back();test.pop_back();test.pop_back();
std::cout << "After we pop_front 3 items and pop_back() 3 items, this is the deque (should be nothing): "<<std::endl;
test.getDeque();
std::cout << "Is the deque empty?: "<< test.isEmpty() << std::endl;
std::cout << "Here is the front value (should be nothing): ";test.peek_front();
std::cout << "Here is the back value (should be nothing): "; test.peek_back();
std::cout << "Here is the current size (should be 0): " << test.getLength() << std::endl;
std::cout << std::endl;
std::cout << "TEST WITH CHARACTERS" << std::endl;
std::cout << std::endl;
csci2100::Deque<char> test2;
test2.push_front('A');
test2.push_back('B');
test2.push_front('C');
std::cout << "After push_front(A), push_back(B), push_front(C), the deque is: ";
test2.getDeque();
std::cout << "Here is the front value (should be C): ";test2.peek_front();
std::cout << "Here is the back value (should be B): ";test2.peek_back();
std::cout << "Here is the current size (should be 3): " << test2.getLength() << std::endl;
std::cout << "Here is the VECTOR without the organization: ";
test2.getVector();
test2.push_back('D');
test2.push_front('E');
std::cout << "When doing push_back(D) and push_front(E) to the deque, the vector will resize and organize itself in a total of 8 spaces. Here is the VECTOR: ";
test2.getVector();
std::cout << "Here is the Deque: ";
test2.getDeque();
test2.pop_back();
test2.pop_back();
std::cout << "After doing pop_back() twice, the current deque is (should be E,C,A): ";
test2.getDeque();
std::cout << "Here is the current size (should be 3): " << test2.getLength() << std::endl;
std::cout << "Athough the deque is changed, the vector still has the values, but they will never be accessed again: ";test2.getVector();
test2.push_front('F');
test2.push_back('G');
test2.push_front('H');
std::cout << "After push_front(F), push_back(G), and push_front(H), the deque is: ";
test2.getDeque();
std::cout << "The vector is: ";
test2.getVector();
std::cout << "Is the deque empty?: "<< test2.isEmpty() << std::endl;
test2.pop_front();test2.pop_front();test2.pop_front();test2.pop_back();test2.pop_back();test2.pop_back();
std::cout << "After we pop_front 3 items and pop_back() 3 items, this is the deque (should be nothing): "<< std::endl;
test2.getDeque();
std::cout << "Is the deque empty?: "<< test2.isEmpty() << std::endl;
std::cout << "Here is the front value (should be nothing): ";test2.peek_front();
std::cout << "Here is the back value (should be nothing): "; test2.peek_back();
std::cout << "Here is the current size (should be 0): " << test2.getLength() << std::endl;
}