forked from FluorineDog/2017aut-quest2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab_list.hpp
156 lines (146 loc) · 3.26 KB
/
lab_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
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
#ifndef LAB_LIST_HPP_
#define LAB_LIST_HPP_
namespace Lab {
template <typename Type> class list {
public:
list();
struct node {
Type data = 0;
node *pre = nullptr;
node *next = nullptr;
};
class iterator {
public:
iterator() {
beg = nullptr;
end = nullptr;
length = 0;
}
iterator(node *currentTmp) { current = currentTmp; }
// Type &operator*();
// assume zht asking for prefix increment
// make him unhappy
// iterator &operator++();
// iterator &operator--();
Type &operator*() { return current->data; }
iterator &operator++() {
current = current->next;
return *this;
}
iterator &operator--() {
current = current->pre;
return *this;
}
bool operator!=(const iterator &another) const {
return another.current != current;
}
node *current;
// add your data and function here
};
public:
void push_back(const Type &elem);
void push_front(const Type &elem);
iterator begin();
iterator end();
int size() const;
void pop_front();
void pop_back();
void erase(iterator iter);
void clear();
~list();
public:
node *beg;
node *en;
int length;
// add your data and function here
};
template <typename Type> list<Type>::list() {
beg = nullptr;
en = nullptr;
length = 0;
}
template <typename Type> typename list<Type>::iterator list<Type>::begin() {
return iterator(beg);
}
template <typename Type> typename list<Type>::iterator list<Type>::end() {
return iterator(en);
}
template <typename Type> void list<Type>::push_back(const Type &elem) {
node *newNode = new node;
newNode->data = elem;
newNode->pre = en;
newNode->next = nullptr;
if (en) {
en->next = newNode;
}
en = newNode;
if (!length) {
beg = newNode;
}
length++;
}
template <typename Type> void list<Type>::push_front(const Type &elem) {
node *newNode = new node;
newNode->data = elem;
newNode->next = beg;
newNode->pre = nullptr;
if (beg) {
beg->pre = newNode;
}
beg = newNode;
if (!length)
en = newNode;
length++;
}
template <typename Type> int list<Type>::size() const { return length; }
template <typename Type> void list<Type>::pop_front() {
node *newNode = new node;
newNode = beg;
beg = beg->next;
if (beg)
beg->pre = nullptr;
length--;
delete newNode;
}
template <typename Type> void list<Type>::pop_back() {
node *newNode = new node;
length--;
newNode = en;
en = en->pre;
if (en)
en->next = nullptr;
delete newNode;
}
template <typename Type> void list<Type>::erase(iterator iter) {
// node *newNode = iter.current->pre;
if (iter.current->pre)
iter.current->pre->next = iter.current->next;
if (iter.current->next)
iter.current->next->pre = iter.current->pre;
delete iter.current;
length--;
}
template <typename Type> void list<Type>::clear() {
while (beg != en) {
node *newNode = beg->next;
delete beg;
beg = newNode;
length--;
}
delete beg;
beg = en = nullptr;
length = 0;
}
template <typename Type> list<Type>::~list() {
/*while (beg.next != nullptr) {
node *newNode = beg->next;
delete beg;
beg = newNode;
}
delete beg;*/
list::clear();
}
} // namespace Lab
#endif
// (.*) \(\*(.*)\)\(.*?desc[,]?(.*)\)
// (.*)\(\*(.*)\)\(.*?iter[,]?(.*)\)