-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlist.h
333 lines (252 loc) · 6.84 KB
/
dlist.h
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// copy this file to "dlist.h" and then modify it where specified below
#ifndef __DLIST_H__
#define __DLIST_H__
#include <cstddef>
#include <cassert>
#include <iostream>
using namespace std;
/***************************************
* Do not modify the class declarations!
***************************************/
template <typename T>
class Dlist {
//OVERVIEW: a doubly-linked list
public:
//EFFECTS: returns true if the list is empty
bool isEmpty() const;
//MODIFIES: this
//EFFECTS: inserts v into the front of the list
void insertFront(const T &datum);
void print();
//MODIFIES: this
//EFFECTS: inserts v into the back of the list
void insertBack(const T &datum);
//REQUIRES: list is not empty
//MODIFIES: this
//EFFECTS: removes the item at the front of the list
T removeFront();
//REQUIRES: list is not empty
//MODIFIES: this
//EFFECTS: removes the item at the back of the list
T removeBack();
Dlist(); // ctor
Dlist(const Dlist &l); // copy ctor
Dlist &operator=(const Dlist &l); // assignment
~Dlist(); // dtor
private:
struct Node { // A private type
Node *next;
Node *prev;
T datum;
};
Node *first; // The pointer to the first node (0 if none)
Node *last; // The pointer to the last node (0 if none)
//MODIFIES: this
//EFFECTS: copies all nodes from l to this
void copyAll(const Dlist &l);
// //MODIFIES: this
// //EFFECTS: removes all nodes
void removeAll();
};
/**** DO NOT MODIFY ABOVE THIS LINE *****/
/***************************************
* Member function implementations here
***************************************/
template <typename T>
Dlist<T>::Dlist(){
first = 0;
last = 0;
}
template <typename T>
bool Dlist<T>::isEmpty() const{
return !first;
}
template <typename T>
void Dlist<T>::insertFront(const T &datum){
// Node *np = new Node;
// cout << np << endl;
// np->datum = datum;
// np->next = first;
// first = np;
// last = np;
if(isEmpty()){
//Make node
Node *np = new Node;
//set datum of np
np->datum = datum;
//set p/n to null
np->prev = 0;
np->next = 0;
//set first/last to node
first = np;
last = np;
}else{
//make temp point to first
Node *temp = first;
//create node to store datum
Node *np = new Node;
np->datum = datum;
//first points to np
first = np;
//set prev to null
np->prev = 0;
//set np next to temp
np->next = temp;
//set temp prev to np
temp->prev = np;
}
}
template <typename T>
void Dlist<T>::print(){
// cout << "------" << endl;
// Node *cur = first;
// while(cur){
// cout << "datum: " << cur->datum << endl;
// cur = cur->next;
// }
if(isEmpty()){
cout << "list is empty..." << endl;
}
cout << "--------" << endl;
for (Node *np=first; np != NULL; np=np->next){
cout << np->datum << " ";
}
cout << endl;
}
//MODIFIES: this
//EFFECTS: inserts v into the back of the list
template <typename T>
void Dlist<T>::insertBack(const T &datum){
if(isEmpty()){
//Make node
Node *np = new Node;
//set datum of np
np->datum = datum;
//set p/n to null
np->prev = 0;
np->next = 0;
//set first/last to node
first = np;
last = np;
}else{
//make temp point to last
Node *temp = last;
//create node to store datum
Node *np = new Node;
np->datum = datum;
//last points to np
last = np;
//set next to null
np->next = 0;
//set np next to temp
np->prev = temp;
//set temp prev to np
temp->next = np;
}
}
// //REQUIRES: list is not empty
// //MODIFIES: this
// //EFFECTS: removes the item at the front of the list
template <typename T>
T Dlist<T>::removeFront(){
//cout << "calling remove front" << endl;
if(isEmpty()){
return 0;
}
//make temp point to first
Node *temp = first;
//set first to point to next
first = first->next;
//hold data from temp in result
T result = temp->datum;
//delete what temp points to, set it = 0
delete temp; temp = 0;
//return result from above
return result;
/*
Node *victim = first;
first = first->next;
T result = victim->datum;
delete victim; victim=0;
return result;
*/
}
// //REQUIRES: list is not empty
// //MODIFIES: this
// //EFFECTS: removes the item at the back of the list
template <typename T>
T Dlist<T>::removeBack(){
if(isEmpty()){
return 0;
}
//make temp point to last
Node *temp = last;
//set last to point to prev
last = last->prev;
//hold data from temp in result
T result = temp->datum;
//delete what temp points to, set it = 0
delete temp; temp = 0;
//if last is not null, set it's next to 0
//this happens if more than 1 elem in list
if(last != 0){
last->next = 0;
}else{
first = 0;
}
//return result from above
return result;
}
//template <typename T>
//List<T>::List(const List &l)
//: first(0), last(0) {
// copyAll(l);
//}
// ctor
template <typename T>
Dlist<T>::Dlist(const Dlist &l)
: first(0), last(0) {
copyAll(l);
} // copy ctor
//template <typename T>
//List<T>& List<T>::operator= (const List &l) {
// if (this == &l) return *this;
// removeAll();
// copyAll(l);
// return *this;
//}
template <typename T>
Dlist<T>& Dlist<T>::operator= (const Dlist &l){
if(this == &l) return *this;
removeAll();
copyAll(l);
return *this;
}
//Dlist<T>::Dlist &operator=(const Dlist &l){
//
//} // assignment
//template <typename T>
//List<T>::~List() {
// removeAll();
//}
template <typename T>
Dlist<T>::~Dlist(){
//cout << "Calling destructor" << endl;
removeAll();
} // dtor
// //MODIFIES: this
// //EFFECTS: copies all nodes from l to this
template <typename T>
void Dlist<T>::copyAll(const Dlist &l){
for (Node *np=l.first; np; np=np->next)
insertBack(np->datum);
}
// //MODIFIES: this
// //EFFECTS: removes all nodes
template <typename T>
void Dlist<T>::removeAll(){
while (!isEmpty())
removeFront();
}
/* this must be at the end of the file */
#endif /* __DLIST_H__ */