这篇文章讲述常用容器list的使用和一些重要部分的简单模拟实现,仅仅只是了解一些实现方法。内容丰富,干货多多。
namespace User
{
template<class T>
struct ListNode
{
ListNode*<T> _next;
ListNode*<T> _prev;
T _data;
//...
};
template<class T>
class list
{
typedef ListNode<T> Node;
//...
private:
Node* _head;
};
}
默认构造函数构造一个只含有哨兵位结点的链表,并且哨兵位结点的的两个指针指向自身。我们暂时使用库里的list,先创建一个int类型的list容器,调用的是默认构造函数,之后尾插几个整数。遍历整个链表一般使用迭代器,还可以使用范围for。因为物理结构是不连续的,不支持使用下标方括号遍历链表。
list();//默认构造函数
void test_list1()
{
std::list<int> lt1;
lt1.push_back(1);
lt1.push_back(2);
lt1.push_back(3);
lt1.push_back(4);
list<int>::iterator it1 = lt1.begin();
while (it1 != lt1.end())
{
cout << *it1 << " ";
++it1;
}
cout << endl;
for (const auto& e : lt1)
{
cout << e << " ";
}
cout << endl;
}
运行结果如下:
template<class T>
struct ListNode
{
ListNode*<T> _next;
ListNode*<T> _prev;
T _data;
ListNode(const T& data = T())
:_next(nullptr)
,_prev(nullptr)
,_data(data)
{}
};
template<class T>
class list
{
typedef ListNode<T> Node;
public:
list()
{
_head = new Node(T());
_head->_next = _head;
_head->_prev = _head;
}
void push_back(const T& x)
{
Node* newnode = new Node(x);
Node* tail = _head->_prev;
// head tail newnode
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;
}
private:
Node* _head;
};
template<class T>
struct ListIterator
{
typedef ListNode<T> Node;
typedef ListIterator<T> Self;
Node* _node;
//...
};
ListIterator(Node* node)
:_node(node)
{}
template<class T>
class list
{
typedef ListNode<T> Node;
public:
typedef ListIterator<T> iterator;
iterator begin()
{
//iterator it(_head->_next);
//return it
//下面是隐士类型转换调用
return iterator(_head->_next);
}
iterator end()
{
return iterator(_head);
}
};
// ++it 前置
Self& operator++()
{
_node = _node->_next;
return *this;
}
// it++ 后置
Self operator++(int)
{
Self tmp(*this);
_node = _node->_next;
return tmp;
}
// --it 前置
Self& operator--()
{
_node = _node->_prev;
return *this;
}
T& operator*()
{
return _node->_data;
}
bool operator!=(const Self& it)
{
return _node != it._node;
}
bool operator==(const Self& it)
{
return _node == it._node;
}
T* operator->()
{
return &_node->_data;
}
虽然operator->看起来比较奇特,但是也有它的应用场景。
struct Pos
{
int _row;
int _col;
Pos(int row = 0, int col = 0)
:_row(row)
, _col(col)
{}
};
void test_list2()
{
list<Pos> lt1;
lt1.push_back(Pos(100, 100));
lt1.push_back(Pos(200, 100));
lt1.push_back(Pos(300, 100));
//临时对象可以调用非静态成员函数,但是不能引用定义
list<Pos>::iterator it = lt1.begin();
while (it != lt1.end())
{
//cout<< *it <<endl;//error,这样写无法运行成功。
//可以这样写但是太别扭了
//cout << (*it)._row << ":" << (*it)._col << " ";
//为了可读性,省略了一个->
cout << it->_row << ":" << it->_col << " " << endl;
//cout << it->->_row << ":" << it->->_col << " "; 展开就成了下面的样子
cout << it.operator->()->_row << ":" << it.operator->()->_col << " "<< endl;
++it;
}
cout << endl;
}
运行结果如下:
template<class T>
struct ListIterator
{
typedef ListNode<T> Node;
typedef ListIterator<T> Self;
Node* _node;
ListIterator(Node* node = Node())//默认构造
:_node(node)
{}
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;
}
const T& operator*()
{
return _node->_data;
}
const T* operator->()
{
return &_node->_data;
}
bool operator!=(const Self& it)
{
return _node != it._node;
}
bool operator==(const Self& it)
{
return _node == it._node;
}
};
template<class T>
class list
{
typedef ListNode<T> Node;
public:
typedef ListIterator<T> iterator;
typedef ListIterator<T> const_iterator;
const_iterator begin() const
{
return const_iterator(_head->_next);
}
const_iterator end() const
{
return const_iterator(_head);
}
//...
};
template<class T, class Ref, class Ptr>
struct ListIterator
{
typedef ListNode<T> Node;
typedef ListIterator<T, Ref, Ptr> Self;
Node* _node;
ListIterator(Node* node = Node())//默认构造
:_node(node)
{}
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;
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
bool operator!=(const Self& it)
{
return _node != it._node;
}
bool operator==(const Self& it)
{
return _node == it._node;
}
};
template<class T>
class list
{
typedef ListNode<T> Node;
public:
typedef ListIterator<T, T&, T*> iterator;
typedef ListIterator<T, const T&, const T*> const_iterator;
const_iterator begin() const
{
return const_iterator(_head->_next);
}
const_iterator end() const
{
return const_iterator(_head);
}
iterator begin()
{
return iterator(_head->_next);
}
iterator end()
{
return iterator(_head);
}
//...
};
反向迭代器的模拟实现和正向迭代器类似,需要注意++和--跟正向迭代器的的方向完全反过来,还要注意迭代器begin和end函数给的结点位置。
template<class T, class Ref, class Ptr>
struct Reverse_ListIterator
{
typedef ListNode<T> Node;
typedef Reverse_ListIterator<T, Ref, Ptr> Self;
Node* _node;
Reverse_ListIterator(Node* node)
:_node(node)
{}
Self& operator++()//前置++,赋值为前面的结点的指针
{
_node = _node->_prev;
return *this;
}
Self operator++(int)//后置++
{
Self tmp(_node);
_node = _node->_prev;
return tmp;
}
Self& operator--()
{
_node = _node->_next;
return *this;
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
bool operator!=(const Self& it)
{
return _node != it._node;
}
bool operator==(const Self& it)
{
return _node == it._node;
}
};
template<class T>
class list
{
typedef ListNode<T> Node;
public:
typedef Reverse_ListIterator<T, T&, T*> reverse_iterator;
typedef Reverse_ListIterator<T, const T&, const T*> reverse_const_iterator;
reverse_const_iterator rbegin() const
{
return reverse_const_iterator(_head->_prev);
}
reverse_const_iterator rend() const
{
return reverse_const_iterator(_head);
}
reverse_iterator rbegin()
{
return reverse_iterator(_head->_prev);
}
reverse_iterator rend()
{
return reverse_iterator(_head);
}
//...
};
iterator insert(iterator pos, const T& x)
{
Node* cur = pos._node;
Node* newnode = new Node(x);
Node* prev = cur->_prev;
//prev newnode cur
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
return iterator(newnode);
}
//erase后Pos失效,因为被释放了
iterator erase(iterator pos)
{
assert(pos != end());
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
return iterator(next);
}
完成了对insert和erase函数的实现,头部或者尾部插入和删除的函数都可以复用这两个函数。
void push_back(const T& x)
{
insert(end(), x);
}
void pop_back()
{
erase(--end());
}
void push_front(const T& x)
{
insert(begin(), x);
}
void pop_front()
{
erase(begin());
}
写一个测试函数
void Func(const list<int>& lt)
{
list<int>::const_iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
void test_list3()
{
list<int> lt1;
//按需实例化
lt1.push_back(1);
lt1.push_back(2);
lt1.push_back(3);
lt1.push_back(4);
lt1.push_back(5);
Func(lt1);
lt1.push_front(10);
lt1.push_front(20);
lt1.push_front(30);
Func(lt1);
lt1.pop_back();
lt1.pop_back();
lt1.pop_back();
Func(lt1);
lt1.pop_front();
lt1.pop_front();
lt1.pop_front();
Func(lt1);
}
运行结果如下:
构造函数这块,有四种类型,默认构造,initializer初始化器构造,迭代器区间构造和拷贝构造。
void Printlist(const list<int> lt)
{
for (const auto& e: lt1)
{
cout << e << " ";
}
cout << endl;
}
void empty_init()
{
_head = new Node(T());
_head->_next = _head;
_head->_prev = _head;
}
list()
{
empty_init();
}
list(initializer_list<T> il)
{
empty_init();
for (const auto& e : il)
{
push_back(e);
}
}
void test1()
{
list<int> lt1 = { 1,2,3,4,5,6,7,8 };
Printlist(lt1);
}
template <class InputIterator>
list(InputIterator first, InputIterator last)
{
empty_init();
while (first != last)
{
push_back(*first);
++first;
}
}
void test2()
{
list<int> lt1 = { 1,2,3,4,5,6,7,8 };
Printlist(lt2);
list<int> lt2(lt1.begin(), lt2.end());
Printlist(lt2);
}
//lt1(lt2)
list(const list<T>& lt)
{
empty_init();
for (const auto& e : lt)
{
push_back(e);
}
}
void test3()
{
list<int> lt1;
lt1.push_back(1);
lt1.push_back(2);
lt1.push_back(3);
lt1.push_back(4);
Printlist(lt2);
list<int> lt2(lt1);
Printlist(lt2);
}
//lt1 = lt3
list<T>& operator=(list<T> lt)
{
std::swap(_head, lt._head);
return *this;
}
void test3()
{
list<int> lt1;
lt1.push_back(1);
lt1.push_back(2);
lt1.push_back(3);
lt1.push_back(4);
Printlist(lt2);
list<int> lt2;
lt2 = lt1;
Printlist(lt2);
}
void clear()
{
auto it = begin();
while (it != end())
{
it = erase(it);
++it;
}
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
看完整篇文章,我相信你对list的使用和一些底层实现原理有进一步的了解,你也可以尝试自己手撕一个简单的list容器实现。树欲静而风不止,既然我们无法改变大势,但是我们可以改变自己!
创作不易,希望这篇文章能给你带来启发和帮助,如果喜欢这篇文章,请留下你的三连,你的支持的我最大的动力!!!
因篇幅问题不能全部显示,请点此查看更多更全内容