forked from MorganBauer/COP-6726-Databases-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoWayList.cc
278 lines (215 loc) · 5.7 KB
/
TwoWayList.cc
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
#ifndef _TWO_WAY_LIST_C
#define _TWO_WAY_LIST_C
#include "TwoWayList.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
// create an alias of the given TwoWayList
template <class Type>
TwoWayList <Type> :: TwoWayList (TwoWayList &me) {
list = new (std::nothrow) Header;
if (list == NULL)
{
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
list->first = me.list->first;
list->last = me.list->last;
list->current = me.list->current;
list->leftSize = me.list->leftSize;
list->rightSize = me.list->rightSize;
}
// basic constructor function
template <class Type>
TwoWayList <Type> :: TwoWayList () : list(NULL)
{
// allocate space for the header
list = new (std::nothrow) Header;
if (list == NULL)
{
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
// set up the initial values for an empty list
list->first = new (std::nothrow) Node;
if (list->first == NULL)
{
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
list->last = new (std::nothrow) Node;
if (list->last == NULL)
{
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
list->current = list->first;
list->leftSize = 0;
list->rightSize = 0;
list->first->next = list->last;
list->last->previous = list->first;
}
// basic deconstructor function
template <class Type>
TwoWayList <Type> :: ~TwoWayList ()
{
// remove everything
MoveToStart ();
while (RightLength ()>0) {
Type temp;
Remove (&temp);
}
// kill all the nodes
for (int i = 0; i <= list->leftSize + list->rightSize; i++) {
list->first = list->first->next;
delete list->first->previous;
}
delete list->first;
// kill the header
delete list;
}
// swap operator
template <class Type> void
TwoWayList <Type> :: operator &= (TwoWayList & List)
{
Header *temp = List.list;
List.list = list;
list = temp;
}
// make the first node the current node
template <class Type> void
TwoWayList <Type> :: MoveToStart ()
{
list->current = list->first;
list->rightSize += list->leftSize;
list->leftSize = 0;
}
// make the first node the current node
template <class Type> void
TwoWayList <Type> :: MoveToFinish ()
{
list->current = list->last->previous;
list->leftSize += list->rightSize;
list->rightSize = 0;
}
// determine the number of items to the left of the current node
template <class Type> int
TwoWayList <Type> :: LeftLength ()
{
return (list->leftSize);
}
// determine the number of items to the right of the current node
template <class Type> int
TwoWayList <Type> :: RightLength ()
{
return (list->rightSize);
}
// swap the right sides of two lists
template <class Type> void
TwoWayList <Type> :: SwapRights (TwoWayList & List)
{
// swap out everything after the current nodes
Node *left_1 = list->current;
Node *right_1 = list->current->next;
Node *left_2 = List.list->current;
Node *right_2 = List.list->current->next;
left_1->next = right_2;
right_2->previous = left_1;
left_2->next = right_1;
right_1->previous = left_2;
// set the new endpoints
Node *temp = list->last;
list->last = List.list->last;
List.list->last = temp;
int tempint = List.list->rightSize;
List.list->rightSize = list->rightSize;
list->rightSize = tempint;
}
// swap the leftt sides of the two lists
template <class Type> void
TwoWayList <Type> :: SwapLefts (TwoWayList & List)
{
// swap out everything after the current nodes
Node *left_1 = list->current;
Node *right_1 = list->current->next;
Node *left_2 = List.list->current;
Node *right_2 = List.list->current->next;
left_1->next = right_2;
right_2->previous = left_1;
left_2->next = right_1;
right_1->previous = left_2;
// set the new frontpoints
Node *temp = list->first;
list->first = List.list->first;
List.list->first = temp;
// set the new current nodes
temp = list->current;
list->current = List.list->current;
List.list->current = temp;
int tempint = List.list->leftSize;
List.list->leftSize = list->leftSize;
list->leftSize = tempint;
}
// move forwards through the list
template <class Type> void
TwoWayList <Type> :: Advance ()
{
(list->rightSize)--;
(list->leftSize)++;
list->current = list->current->next;
}
// move backwards through the list
template <class Type> void
TwoWayList <Type> :: Retreat ()
{
(list->rightSize)++;
(list->leftSize)--;
list->current = list->current->previous;
}
// insert an item at the current poition
template <class Type> void
TwoWayList <Type> :: Insert (Type *Item)
{
Node *temp = new (std::nothrow) Node;
if (temp == NULL)
{
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
Node *left = list->current;
Node *right = list->current->next;
left->next = temp;
temp->previous = left;
temp->next = right;
temp->data = new (std::nothrow) Type;
if (temp->data == NULL)
{
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
right->previous = temp;
temp->data->Consume (Item); // this is Record specific, templating doesn't help here...
list->rightSize += 1;
}
// get a reference to the currentitemin the list
template <class Type> Type*
TwoWayList <Type> :: Current (int offset)
{
Node *temp = list->current->next;
for (int i = 0; i < offset; i++) {
temp = temp->next;
}
return temp->data;
}
// remove an item from the current poition
template <class Type> void
TwoWayList <Type> :: Remove (Type *Item)
{
Node *temp = list->current->next;
list->current->next = temp->next;
temp->next->previous = list->current;
Item->Consume(temp->data);
delete temp;
(list->rightSize)--;
}
#endif