-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
70 lines (58 loc) · 1.32 KB
/
list.h
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
#ifndef LIST_H
#define LIST_H
#include <vector>
using namespace std;
template <class Node>
class List {
public:
void Append(Node* node) {
if (tail_)
tail_->after_ = node;
else
head_ = node;
tail_ = node;
node->after_ = nullptr;
++size_;
}
void Append(List* list) {
if (tail_)
tail_->after_ = list->head_;
else
head_ = list->head_;
tail_ = list->tail_;
size_ += list->size_;
list->Clear();
}
vector<Node*> ToVector() {
vector<Node*> nodes;
for (auto* node : *this) nodes.push_back(node);
Clear();
return nodes;
}
int empty() const { return size_ == 0; }
int size() const { return size_; }
class Iterator {
public:
Iterator(Node* node) : node_(node), after_(node ? node->after_ : nullptr) {}
void operator++() {
node_ = after_;
after_ = (node_ ? node_->after_ : nullptr);
}
bool operator!=(const Iterator& it) const { return node_ != it.node_; }
Node* operator*() const { return node_; }
private:
Node* node_;
Node* after_;
};
const Iterator begin() const { return Iterator(head_); }
const Iterator end() const { return Iterator(nullptr); }
private:
void Clear() {
head_ = tail_ = nullptr;
size_ = 0;
}
Node* head_ = nullptr;
Node* tail_ = nullptr;
int size_ = 0;
};
#endif