-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.hpp
63 lines (44 loc) · 1.19 KB
/
List.hpp
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
// Buğra Ekuklu, 150120016
#ifndef ListDeclaration
#define ListDeclaration
template <typename T>
class List {
public:
T operator [](unsigned int index) {
// Complementary operation
index = _length - index - 1;
Node *cursor = _head;
for (unsigned int i = 0; i < index; ++i) {
cursor = cursor->next;
}
return cursor->data;
}
void append(const T& object) {
// Append to first node
Node *incoming = new Node(object);
incoming->data = object;
incoming->next = _head;
_head = incoming;
_length += 1;
}
unsigned int count() const {
return _length;
}
~List() {
Node *cursor = _head;
for (unsigned int i = 0; i < _length; ++i) {
Node *next = cursor->next;
delete cursor;
cursor = next;
}
}
private:
struct Node {
Node(const T &data) : data(data) { }
T data;
Node *next;
};
Node *_head;
unsigned int _length = 0;
};
#endif /* ListDeclaration */