-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
147 lines (116 loc) · 2.9 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
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
// File: list.h
// Author: Viktor Slavkovic
// Date: May 2016
#ifndef _STL_LIST_H_
#define _STL_LIST_H_
#include <assert.h>
template <class T>
class list {
public:
typedef T value_type;
typedef unsigned size_type;
list() : head_(0), tail_(0), size_(0u) {}
~list() { clear(); }
value_type& front() {
assert(size_);
return head_->val;
}
const value_type& front() const {
assert(size_);
return head_->val;
}
value_type& back() {
assert(size_);
return tail_->val;
}
const value_type& back() const {
assert(size_);
return tail_->val;
}
size_type size() const { return size_; }
int empty() const { return !size_; }
void clear() {
while (head_) {
pnode temp = head_;
head_ = head_->next;
delete temp;
}
tail_ = 0;
size_ = 0;
}
void push_back(const value_type& val) {
pnode temp = new node(val);
if (!size_++)
head_ = tail_ = temp;
else {
tail_->next = temp;
tail_ = temp;
}
}
void push_front(const value_type& val) {
head_ = new node(val, head_);
if (!size_++) tail_ = head_;
}
// O(size)
void pop_back() {
if (!size_) return;
pnode* p = &head_;
while ((*p)->next) p = &((*p)->next);
delete (*p);
*p = 0;
if (!--size_) head_ = tail_ = 0;
}
void pop_front() {
if (!size_) return;
pnode temp = head_;
head_ = head_->next;
delete temp;
if (!--size_) head_ = tail_ = 0;
}
int operator==(const list& rhs) const {
if (size_ != rhs.size_) return 0;
for (pnode p1 = head_, p2 = rhs.head_; p1; p1 = p1->next, p2 = p2->next)
if (p1->val != p2->val) return 0;
return 1;
}
int operator!=(const list& rhs) const { return !(*this == rhs); }
private:
struct node {
node(const value_type& val, node* next = 0) : val(val), next(next) {}
value_type val;
node* next;
};
typedef node* pnode;
pnode head_, tail_;
unsigned size_;
public:
class iterator {
public:
iterator() : owner(0), p(0) {}
void operator++() {
if (p) p = p->next;
}
void operator++(int k) {
if (p) p = p->next;
}
value_type& operator*() {
assert(p);
return p->val;
}
int operator==(const iterator& rhs) const {
if (!owner) return 0;
if (owner != rhs.owner) return 0;
return (p == rhs.p);
}
int operator!=(const iterator& rhs) const { return !(*this == rhs); }
private:
typedef list<value_type>::node* pnode;
pnode p;
list<value_type>* owner;
iterator(list<value_type>* owner, const pnode& p) : owner(owner), p(p) {}
friend class list<value_type>;
};
iterator begin() { return iterator(this, head_); }
iterator end() { return iterator(this, (pnode)0); }
};
#endif // _STL_LIST_H_