-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsteroidList.cpp
182 lines (158 loc) · 4.71 KB
/
AsteroidList.cpp
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
#include <galaxy-explorer/AsteroidList.hpp>
//default constructor
AsteroidListItem::AsteroidListItem() {
this->next = nullptr;
this->data = nullptr;
}
//constructor with Asteroid passed in
AsteroidListItem::AsteroidListItem(Asteroid a) {
this->next = nullptr;
this->data = new Asteroid(a);
}
AsteroidListItem::~AsteroidListItem() {
//destructor
}
AsteroidList::AsteroidList()
{
//empty list
}
AsteroidList::AsteroidList(const AsteroidList& src)
{
// The functions in this class are listed in a suggested order of implementation,
// except for this one and the destructor (because you should put all your constructors together).
//create an independent copy of 'src' for AsteroidList
const AsteroidListItem* ptr = src.begin();
AsteroidListItem* last = NULL;
AsteroidListItem* nptr = NULL;
head.setNext(NULL);
while (ptr != NULL) {
nptr = new AsteroidListItem(ptr->getData());
if (last == NULL)
head.setNext(nptr);
else
last -> setNext(nptr);
ptr = ptr-> getNext();
last = nptr;
}
}
AsteroidList::~AsteroidList() {
// The functions in this class are listed in a suggested order of implementation,
// except for this one and the copy constructor (because you should put all your constructors together).
//Free all memory associated with AsteroidList
this -> clear();
}
void AsteroidList::pushFront(Asteroid e) {
//create a new AsteroidListItem and put it at the front of the AsteroidList
AsteroidListItem* nptr = new AsteroidListItem(e);
nptr -> setNext(begin());
head.setNext(nptr);
}
Asteroid& AsteroidList::front() {
return (head.getNext() -> getData());
}
const Asteroid& AsteroidList::front() const {
return (head.getNext() -> getData());
}
bool AsteroidList::isEmpty() const {
//return true if the list is empty
if (head.getNext() == NULL)
return true;
else
return false;
}
int AsteroidList::size() const {
int count = 0;
const AsteroidListItem* tptr = head.getNext();
while (tptr != NULL) {
count++;
tptr = tptr -> getNext();
}
return count;
}
AsteroidListItem* AsteroidList::beforeBegin() {
//return the address of head
return &head;
}
const AsteroidListItem* AsteroidList::beforeBegin() const {
//return the address of head
return &head;
}
AsteroidListItem* AsteroidList::begin() {
return head.getNext();
}
const AsteroidListItem* AsteroidList::begin() const {
return head.getNext();
}
AsteroidListItem* AsteroidList::beforeEnd() {
//if list is empty, return beforeBegin
if (isEmpty()) {
return beforeBegin();
}
AsteroidListItem* tptr = head.getNext();
while(tptr -> getNext() != NULL) {
tptr = tptr -> getNext();
}
return tptr;
}
const AsteroidListItem* AsteroidList::beforeEnd() const {
//if list is empty, return beforeBegin
if (isEmpty()) {
return beforeBegin();
}
const AsteroidListItem* tptr = head.getNext();
while(tptr -> getNext() != NULL) {
tptr = tptr -> getNext();
}
return tptr;
}
AsteroidListItem* AsteroidList::end() {
//returns null, because it is the last thing in the list
return nullptr;
}
const AsteroidListItem* AsteroidList::end() const {
//returns null, because it is the last thing in the list
return nullptr;
}
AsteroidListItem* AsteroidList::insertAfter(AsteroidListItem* prev, Asteroid e) {
//insert a new Asteroid after prev in the list
AsteroidListItem* nptr = new AsteroidListItem(e);
AsteroidListItem* temp = prev->getNext();
prev->setNext(nptr);
nptr->setNext(temp);
return nptr;
}
AsteroidListItem* AsteroidList::insertAfter(AsteroidListItem* prev, const AsteroidList& others) {
//insert a new AsteroidList after prev in the list
const AsteroidListItem* ptr = others.begin();
while (ptr != NULL) {
prev = this -> insertAfter(prev, ptr->getData());
ptr = ptr -> getNext();
}
return prev;
}
AsteroidListItem* AsteroidList::eraseAfter(AsteroidListItem* prev) {
//delete the asteroid following prev in the list
if (prev->getNext() != NULL) {
AsteroidListItem* ptr = prev->getNext();
AsteroidListItem* temp = ptr->getNext();
delete ptr;
prev->setNext(temp);
}
}
void AsteroidList::clear() {
//Makes the list empty, frees all nodes
AsteroidListItem* ptr;
while (head.getNext() != NULL) {
ptr = head.getNext();
head.setNext(ptr -> getNext());
delete ptr;
}
}
AsteroidList& AsteroidList::operator=(const AsteroidList& src) {
//makes 'this' list an independent copy of 'src'
if (this == &src)
return (*this);
this -> clear();
this -> insertAfter(&head, src);
return(*this);
}