-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
104 lines (100 loc) · 1.56 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
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
#ifndef _LIST_H_
#define _LIST_H_
template <typename T>
class List
{
public:
struct list{
list *next ;
list *last;
T elem;
};
typedef list* iterator;
List()
{
head = new list;
tail = new list;
head->next = tail;
tail->last = head;
head->last = tail->next = 0;
}
void push_back(const T& e)
{
list *cur = new list;
cur->elem = e;
tail->last->next = cur;
cur->last = tail->last;
cur->next = tail;
tail->last = cur;
}
void push_back(iterator &it)
{
iterator tnext = it->next;
iterator tlast = it->last;
it->next = tail;
it->last = tail->last;
tail->last->next = it;
tail->last = it;
tlast->next = tnext;
tnext->last = tlast;
it = tnext;
}
void push_back(List &l)
{
if (l.head->next == l.tail)
return;
tail->last->next = l.head->next;
l.head->next->last = tail->last;
tail->last = l.tail->last;
l.tail->last->next = tail;
l.head->next = l.tail;
l.tail->last = l.head;
}
void erase(iterator &it)
{
if (it == head || it == tail)
return;
iterator tnext = it->next;
iterator tlast = it->last;
delete it;
tlast->next = tnext;
tnext->last = tlast;
it = tnext;
}
bool isempty() const
{
return head->next == tail;
}
iterator begin()
{
return head->next;
}
iterator end()
{
return tail;
}
bool check() const
{
iterator it = head->next;
while (it != tail)
{
if (it == NULL)
return false;
it = it->next;
}
return true;
}
~List()
{
iterator tmp;
while (head)
{
tmp = head->next;
delete head;
head = tmp;
}
}
private:
iterator head, tail;
};
#endif