1. 什么是list
list是一个顺序表,底层采用双向带头循环链表实现,支持常量时间的插入删除操作,不同于vector无法随机访问。
2. 模拟实现
反向迭代器可作为适配器,是正向迭代器的封装:
#pragma once
namespace lzh {
template<class Iterator, class Ref, class Ptr>
struct ReverseIterator {
typedef ReverseIterator<Iterator, Ref, Ptr> self;
ReverseIterator(Iterator it) :_it(it) { }
self& operator++() {
--_it;
return *this;
}
self& operator--() {
++_it;
return *this;
}
Ref& operator*() {
Iterator temp = _it;
return *--temp;
}
Ptr operator->() {
return &(operator*());
//return _it.operator->();
}
bool operator!=(const self& it) {
return _it != it._it;
}
Iterator _it;
};
}
list模拟实现:
#pragma once
#include <iostream>
#include <algorithm>
#include <algorithm>
#include <utility>
#include <assert.h>
using namespace std;
namespace lzh {
template<class T>
struct list_node {
list_node(const T& val = T()) :_val(val), _next(nullptr), _prev(nullptr)
{ }
T _val;
list_node<T>* _next;
list_node<T>* _prev;
};
template<class T, class Ref, class Ptr>
struct list_iterator {
typedef list_node<T> list_node;
typedef list_iterator<T, T&, T*> iterator;
typedef list_iterator<T, const T&, const T*> const_iterator;
typedef list_iterator<T, Ref, Ptr> self;
list_iterator(list_node* ptr) :_ptr(ptr)
{ }
list_iterator(const iterator& it) :_ptr(it._ptr)
{ }
self& operator++() {
_ptr = _ptr->_next;
return *this;
}
self operator++(int) {
self temp = *this;
_ptr = _ptr->_next;
return temp;
}
self& operator--() {
_ptr = _ptr->_prev;
return *this;
}
self operator--(int) {
self temp = *this;
_ptr = _ptr->_prev;
return temp;
}
bool operator!=(const self& p) const {
return _ptr != p._ptr;
}
Ref operator*() {
return _ptr->_val;
}
Ptr operator->() {
return &_ptr->_val;
}
list_node* _ptr;
};
template<class T>
class list {
private:
typedef list_node<T> list_node;
void empty_init() {
_node = new list_node;
_node->_next = _node->_prev = _node;
_size = 0;
}
public:
typedef list_iterator<T, T&, T*> iterator;
typedef list_iterator<T, const T&, const T*> const_iterator;
typedef list_reverse_iterator<iterator, T&, T*> reverse_iterator;
typedef list_reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;
list() {
empty_init();
}
list(size_t n, const T& val) {
empty_init();
for (size_t i = 0; i < n; ++i) {
push_back(val);
}
}
list(int n, const T& val) {
empty_init();
for (int i = 0; i < n; ++i) {
push_back(val);
}
}
// list(const list& lt)
// 在类中可以直接写类名,但是不推荐
// 为了可读性最好加上模板参数
list(const list<T>& lt) {
empty_init();
for (auto& l : lt) {
push_back(l);
}
}
template <class Iterator>
list(Iterator first, Iterator last) {
empty_init();
while (first != last) {
push_back(*first);
++first;
}
}
void swap(list<T>& lt) {
std::swap(_node, lt._node);
std::swap(_size, lt._size);
}
list<T>& operator=(list<T> lt) {
swap(lt);
return *this;
}
~list() {
clear();
delete _node;
_node = nullptr;
}
void clear() {
iterator it = begin();
while (it != end()) {
it = erase(it);
}
_size = 0;
}
iterator begin() {
// iterator(_node->_next)
return _node->_next;
}
iterator end() {
return _node;
}
const_iterator begin() const {
return _node->_next;
}
const_iterator end() const {
return _node;
}
reverse_iterator rbegin() {
return end();
}
reverse_iterator rend() {
return begin();
}
const_reverse_iterator rbegin() const {
return end();
}
const_reverse_iterator rend() const {
return begin();
}
void push_back(const T& val) {
/*list_node* newNode = get_node(val);
list_node* tail = _node->_prev;
newNode->_next = _node;
_node->_prev = newNode;
newNode->_prev = tail;
tail->_next = newNode;*/
insert(end(), val);
}
void push_front(const T& val) {
/*list_node* newNode = get_node(val);
list_node* head = _node->_next;
_node->_next = newNode;
newNode->_prev = _node;
newNode->_next = head;
head->_prev = newNode;*/
insert(begin(), val);
}
iterator insert(iterator pos, const T& val) {
list_node* newNode = new list_node(val);
list_node* prev = pos._ptr->_prev;
newNode->_next = pos._ptr;
pos._ptr->_prev = newNode;
prev->_next = newNode;
newNode->_prev = prev;
++_size;
return pos;
}
void pop_back() {
/*assert(_node->_prev != _node);
list_node* tail = _node->_prev;
tail->_prev->_next = _node;
_node->_prev = tail->_prev;
delete tail;*/
erase(--end());
}
void pop_front() {
/*assert(_node->_next != _node);
list_node* head = _node->_next;
_node->_next = head->_next;
head->_next->_prev = _node;
delete head;*/
erase(begin());
}
iterator erase(iterator pos) {
//assert(pos._ptr != _node);
assert(pos != end());
list_node* prev = pos._ptr->_prev;
list_node* next = pos._ptr->_next;
prev->_next = next;
next->_prev = prev;
delete pos._ptr;
--_size;
return next;
}
// List Access
T& front() {
assert(!empty());
return _node->_next->_val;
}
const T& front() const {
assert(!empty());
return _node->_next->_val;
}
T& back() {
assert(!empty());
return _node->_prev->_val;
}
const T& back() const {
assert(!empty());
return _node->_prev->_val;
}
bool empty() const {
return _size == 0;
}
size_t size() const {
return _size;
}
private:
list_node* _node;
size_t _size;
};
}