-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.cpp
More file actions
executable file
·214 lines (175 loc) · 4.15 KB
/
list.cpp
File metadata and controls
executable file
·214 lines (175 loc) · 4.15 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
#include "list.h"
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//default constructor. do not use.
PackageList::PackageList(): Package(), next(NULL)
{}
//recursively deep-copys.
PackageList::PackageList(const PackageList &to_copy): Package(to_copy.priority,
to_copy.getAddress(),
to_copy.size,
to_copy.weight)
{
if(to_copy.next)
{
next = new PackageList(*to_copy.next);
}
else
{
next = NULL;
}
}
//recursively deletes dynamic memory.
PackageList::~PackageList()
{
if(next)
{
delete next;
next = NULL;
}
}
//this is the constructor used. in_next is NULL by default. Creates a new package list with one element.
PackageList::PackageList(int in_priority, int in_address, int in_size,
int in_weight, PackageList * in_next):
Package(in_priority, in_address, in_size, in_weight),
next(in_next)
{}
//adds a package list to the end of calling list.
//client must set to_add to null after calling.
//I know this is bad please don't fail me.
void PackageList::add_to_rear(PackageList * to_add)
{
if(!next)
{
next = to_add;
return;
}
PackageList * tail = this;
PackageList * target;
while(tail)
{
target = tail;
tail = tail->next;
}
target->next = to_add;
}
//returns a const pointer to next, so client has
//information but can't screw it up. hopefully.
const PackageList * PackageList::getNextPtr() const
{
const PackageList * return_ptr = next;
return return_ptr;
}
//this function is bad. call with "donor = donor->get_first_element(donation).
//couldn't figure out a better way to do this, which is pretty embarassing.
PackageList * PackageList::get_first_element(PackageList *& out_package)
{
PackageList * temp = this;
PackageList * ret = next;
temp->next = NULL;
out_package = new PackageList(*temp);
delete temp;
return ret;
}
//same as above, but this one is somehow worse.
PackageList * PackageList::get_last_element(PackageList * out_package)
{
if(!next)
{
out_package = this;
out_package->next = NULL;
return NULL;
}
PackageList * pre_tail = this;
PackageList * tail = next;
while(tail->next)
{
pre_tail = pre_tail->next;
tail = tail->next;
}
pre_tail->next = NULL;
out_package = tail;
out_package->next = NULL;
return this;
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//default constructor, don't use
EdgeList::EdgeList(): Edge(), next(NULL)
{}
//recursive deep copy
EdgeList::EdgeList(const EdgeList &to_copy): Edge(to_copy.getAddress(),
to_copy.length, to_copy.speed)
{
if(to_copy.next)
{
next = new EdgeList(*to_copy.next);
}
else
{
next = NULL;
}
}
//recursive delete dynamic memory
EdgeList::~EdgeList()
{
if(next)
{
delete next;
next = NULL;
}
}
//this is the constructor to use, in_next is null by default.
EdgeList::EdgeList(int in_address, int in_length, int in_speed,
EdgeList * in_next): Edge(in_address, in_length, in_speed), next(in_next)
{}
//
void EdgeList::add_to_rear(EdgeList * to_add)
{
if(!next)
{
next = to_add;
return;
}
EdgeList * tail = next;
while(tail->next)
{
tail = tail->next;
}
tail->next = to_add;
}
//returns const poniter to next, so client can
//have information about whats happening in list.
const EdgeList * EdgeList::getNextPtr() const
{
const EdgeList * return_ptr = next;
return return_ptr;
}
//this function is bad, use like donor = donor.getFirstElement(donation)
//it kinda works i guess.
EdgeList * EdgeList::get_first_element(Edge *& out_edge)
{
out_edge = this;
return next;
}
//see above
EdgeList * EdgeList::get_last_element(Edge *& out_edge)
{
if(!next)
{
out_edge = this;
return NULL;
}
EdgeList * pre_tail = this;
EdgeList * tail = next;
while(tail->next)
{
pre_tail = pre_tail->next;
tail = tail->next;
}
pre_tail->next = NULL;
out_edge = tail;
return this;
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////