前言:
list的底层结构为带头结点的双向循环链表
大体框架:
namespace simulation {
template <class T>
struct list_node {
//成员函数
};
template<class T,class Ref,class Ptr>
struct _list_iterator {
//成员函数
};
template<class 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;
//成员函数
private:
Node* _head;
size_t _size;
};
}
一、节点的封装(list_node)
template <class T>
struct list_node {
//带头的双向循环链表
//在类模板中:
//类名不等于类型,所以定义指针时类名要显示实例化
list_node<T>* _prev;
list_node<T>* _next;
T _val;
list_node(const T&val=T())
//T()相当于调用构造函数;
:_prev(nullptr)
,_next(nullptr)
,_val(val)
{}
};
二、迭代器的封装(_list_iterator)
因为list的数据不是在一片连续的内存中(像vector,string那样),所以我们不能用原生指针来当迭代器iterator,我们要自己设计一个迭代器,自己去实现其++以及比较等操作。
1.类模板的定义:
//设计const迭代器:可以把迭代器理解为类似指针
// 错误示范: typedef const __list_iterator<T> const_iterator;
// 这样设计const迭代器是不行的,因为const迭代器期望指向内容不能修改
// 这样设计是迭代器本身不能修改
//为了设计不那么繁琐,我们传入三个模板参数
template<class T,class Ref,class Ptr>
//相当于:
// typedef __list_iterator<T, T&, T*> iterator;
// typedef __list_iterator<T, const T&, const T*> const_iterator;
2.构造函数
typedef list_node<T> Node;
typedef _list_iterator<T, Ref, Ptr> self;
Node* _node;//成员变量
_list_iterator(Node* node)
:_node(node)
{}
3.前置++,后置++
self& operator++() {//前置++
//typedef _list_iterator<T, Ref, Ptr> self;
_node = _node->_next;
return *this;
}
self operator++(int) {
//后置++
self tmp(*this);
_node = _node->_next;
return tmp;
}
4.前置–,后置–
self& operator--() {
_node = _node->_prev;
return *this;
}
self operator--(int) {
self tmp(*this);
_node = _node->_prev;
return tmp;
}
5.解引用(operator*())
Ref operator*() {
//因为可能为const T&类型或者T&类型
//为了避免繁琐,使用模板参数里面的Ref
//编译器根据不同的类型会生成对应的函数
return _node->_val;
}
6. ->重载(operator- >())
//template<class T,class Ref,class Ptr>
//相当于:
// typedef __list_iterator<T, T&, T*> iterator;
// typedef __list_iterator<T, const T&, const T*> const_iterator;
Ptr operator->() {
return&_node->_val;
}
7.比较运算符的重载:
bool operator!=(const self& it)const {
return _node != it._node;
}
bool operator==(const self& it)const {
return _node == it._node;
}
三、list成员函数
1.构造函数
typedef list_node<T> Node;
public:
typedef _list_iterator<T, T&, T*> iterator;
typedef _list_iterator<T, const T&, const T*> const_iterator;
void empty_init() {
_size = 0;
_head = new Node;
_head->_prev = _head;
_head->_next = _head;
}
list() {//构造函数
empty_init();
}
private:
Node*_head;//哨兵位
size_t _size;
2.begin(),end()
iterator begin() {
//begin()为哨兵位下一个
return _head->_next;
}
const_iterator begin()const {
return _head->_next;
}
//end()为哨兵位
iterator end() {
return _head;
}
const_iterator end()const {
return _head;
}
3.插入(insert)在pos之前一个位置插入
在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
iterator insert(iterator pos, const T& x) {
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* newnode = new Node(x);
newnode->_prev = prev;
newnode->_next = cur;
prev->_next = newnode;
cur->_prev = newnode;
++_size;
//返回指向第一个新插入元素的迭代器。
return newnode;
}
4.删除(erase)
iterator erase(iterator pos) {
assert(pos != end());
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
delete cur;
prev->_next = next;
next->_prev = prev;
--_size;
//返回被删除节点的下一个位置
return next;
}
5.尾插尾删
void push_front(const T& x) {
insert(begin(), x);
}
void pop_back() {
erase(--end());
}
6.头插头删
void push_back(const T&x) {
insert(end(), x);
}
void pop_front() {
erase(begin());
}
7.析构函数
void clear() {
iterator it = begin();
while (it != end()) {
it = erase(it);
}
_size = 0;
}
~list() {
clear();
delete _head;
_head = nullptr;
}
8.赋值运算符重载
void swap(list<T><){
std::swap(_head, lt._head);
std::swap(_size, lt._size);
}
list<T>operator=(list<T> lt) {
swap(lt);
return *this;
}
9.拷贝构造函数
list(const list<T>& lt) {
empty_init();
_size = lt._size;
for (auto &ch : lt) {
push_back(ch);
}
}
10.大小(size)
size_t size() {
return _size;
}
四,完整代码
#pragma once
#include<iostream>
using namespace std;
namespace bit
{
template<class T>
struct list_node
{
T _data;
list_node<T>* _next;
list_node<T>* _prev;
list_node(const T& x=T())
:_data(x)
,_next(nullptr)
,_prev(nullptr)
{}
};
//T T& T*
//T const T& const T*
template<class T,class Ref,class Ptr>
struct __list_iterator
{
typedef list_node<T> Node;
typedef __list_iterator<T,Ref,Ptr> self;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{}
self& operator++()
{
_node = _node->_next;
return *this;
}
self& operator--()
{
_node = _node->_prev;
return *this;
}
self operator++(int) //能用前置就不要用后置,后置要返回++之后的
{
self tmp(*this);
_node = _node->_next;
return tmp;
}
self operator--(int)
{
self tmp(*this);
_node = _node->_prev;
return tmp;
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
bool operator!=(const self& s)
{
return _node != s._node; //看节点的指针是否相等
}
bool operator==(const self& s)
{
return _node == s._node; //看节点的指针是否相等
}
};
template<class 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;//通过模板参数去控制是普通迭代器还是const
const_iterator begin() const
{
return const_iterator(_head->_next);
//也可以写成 return _head->_next;隐式类型转换,和上面是一样的
}
const_iterator end() const//哨兵卫位置
{
return const_iterator(_head);
}
iterator begin()
{
return iterator(_head->_next);
}
iterator end() //哨兵卫位置
{
return iterator(_head);
}
void empty_init()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
_size = 0;
}
list()
{
empty_init();
}
//lt2(lt1) lt2拷贝构造lt1,this就是lt2
list(const list<T>& lt)
{
empty_init();
for (auto e : lt)
{
push_back(e);
}
}
void swap(list<T>& lt)
{
std::swap(_head, lt._head);//交换头指针
std::swap(_size, lt._size);//交换_size
}
//lt3=lt1 lt就是lt1的拷贝构造 赋值重载
list<int>& operator=(const list<int>& lt)
{
//现代写法
swap(lt);
return *this;
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
void clear()
{
//清理数据,不清头节点
iterator it = begin();
while (it != end())
{
it = erase(it);//it就是下一个位置
}
}
void push_back(const T& x)
{
/*Node* tail = _head->_prev;
Node* newnode = new Node(x);
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;*/
insert(end(), x);
}
void push_front(const T& x)
{
insert(begin(), x);
}
void pop_front()//头删就是在begin位置删除
{
erase(begin());
}
void pop_back()//尾删
{
erase(--end());
}
iterator insert(iterator pos, const T& x)//在pos之前插入x
{
Node* cur = pos._node;
Node* newnode = new Node(x);
Node* prev = cur->_prev;
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
++_size;
return iterator(newnode);
}
iterator erase(iterator pos) //在pos位置删除
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
delete cur;
prev->_next = next;
next->_next = prev;
--_size;
return iterator(next);
}
size_t size()
{
return _size;
}
private:
Node* _head;
size_t _size;
};
void test_list1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
//封装屏蔽底层差异和实现细节
//提供统一的访问修改遍历方式
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
struct AA
{
AA(int a1=0,int a2=0)
:_a1(a1)
,_a2(a2)
{}
int _a1;
int _a2;
};
void test_list2()
{
list<AA> lt; //list中存的是自定义类型
lt.push_back(AA(1, 1));
lt.push_back(AA(2, 2));
lt.push_back(AA(1, 3));
list<AA>::iterator it = lt.begin();
while (it != lt.end())
{
// cout << *it << " ";//*it是A,A不支持流插入
// cout << (*it)._a1 << " " << (*it)._a2<<endl;
cout << it->_a1 << " " << it->_a2 << endl;
++it;
}
cout << endl;
}
void print_list(const list<int><)
{
list<int>::const_iterator it = lt.begin();
while (it != lt.end())
{
//*it = 10;不能修改,调用operator*返回data的别名,加const不能修改
cout << *it << " ";
it++;
}
}
void test_list3()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
print_list(lt);
}
}
384

被折叠的 条评论
为什么被折叠?



