-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.cpp
More file actions
43 lines (34 loc) · 899 Bytes
/
list.cpp
File metadata and controls
43 lines (34 loc) · 899 Bytes
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
#include "list.h"
#include <cstdio>
Node::Node(tuple* t, Node* n = nullptr) : mytuple{t}, next{n} {}
// ------------------------------------------------------------------
Node* List::getRoot() const { return start; }
int64_t List::getLen() const { return len; }
void List::append(tuple* t) {
if (len == 0)
start = end = new Node(t);
else {
// always maintain a pointer that points to the last node, so we can
// immediately insert
end->next = new Node(t);
end = end->next;
}
len++;
}
bool List::find(tuple& t) {
Node* traverse = start;
while (traverse) {
if (traverse->mytuple == &t) return true;
traverse = traverse->next;
}
return false;
}
List::List() : start{nullptr}, end{nullptr}, len{} {}
List::~List() {
Node* traverse = start;
for (int i = 0; i < len; i++) {
Node* t = traverse;
traverse = traverse->next;
delete t;
}
}