list
list 模拟
#pragma once
#include
#include "ReverseIterator.h"
namespace sjy
{
template <typename T>
struct __list_node
{
__list_node(const T& val = T())
:_prev(nullptr)
, _next(nullptr)
, _val(val)
{}
__list_node<T>* _prev;
__list_node<T>* _next;
T _val;
};
template <typename T, typename Ref, typename Ptr>
struct __list_iterator
{
typedef __list_node<T> Node;
typedef __list_iterator<T, Ref, Ptr> self;
__list_iterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_val;
}
Ptr operator->()
{
return &(_node->_val);
}
self& operator++()
{
_node = _node->_next;
return *this;
}
self operator++(int)
{
self tmp(*this);
_node = _node->_next;
return tmp;
}
self& operator--()
{
_node = _node->_prev;
return *this;
}
self operator--(int)
{
self tmp(*this);
_node = _node->_prev;
return tmp;
}
bool operator!=(const self& other) const
{
return _node != other._node;
}
bool operator==(const self& other) const
{
return _node == other._node;
}
Node* _node;
};
template <typename T>
class list
{
typedef __list_node<T> Node;
public:
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
typedef Reverse_iterator<iterator, T&, T*> reverse_iteartor;
typedef Reverse_iterator<const_iterator, const T&, const T*> const_reverse_iteartor;
iterator begin()
{
return _head->_next;
}
iterator end()
{
return _head;
}
reverse_iteartor rbegin()
{
return reverse_iteartor(end());
}
Reverse_iterator<iterator, T&, T*> rend()
{
return reverse_iteartor(begin());
}
list()
:_size(0)
{
_head = new Node;
_head->_prev = _head;
_head->_next = _head;
}
~list()
{
clear();
delete _head;
}
list(const list<T>& other)
:_size(0)
{
_head = new Node;
_head->_prev = _head;
_head->_next = _head;
const_iterator it = other.begin();
while (it != other.end())
{
push_back(*it);
++it;
}
}
list<T>& operator=(list<T> other)
{
swap(other);
return *this;
}
void push_back(const T& x)
{
Node* newnode = new Node(x);
Node* tail = _head->_prev;
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;
_size++;
}
void push_front(const T& x)
{
insert(begin(), x);
}
iterator insert(iterator pos, const T& x)
{
Node* newnode = new Node(x);
Node* posptr = pos._node;
Node* posptrprev = posptr->_prev;
posptrprev->_next = newnode;
newnode->_prev = posptrprev;
newnode->_next = posptr;
posptr->_prev = newnode;
_size++;
return newnode;
}
void pop_back()
{
erase(--end());
}
void pop_front()
{
erase(begin());
}
iterator erase(iterator pos)
{
assert(pos != end());
Node* posptr = pos._node;
Node* posptrprev = posptr->_prev;
Node* posptrnext = posptr->_next;
posptrprev->_next = posptrnext;
posptrnext->_prev = posptrprev;
delete posptr;
_size--;
return posptrprev;
}
size_t size()
{
return _size;
}
void swap(list<T>& other)
{
std::swap(_head, other._head);
std::swap(_size, other._size);
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
private:
Node* _head;
size_t _size;
};
}
- 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
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205