【C++】list的模拟实现来咯


一、整体框架

list的本质就是带头双向循环链表,我们直接来看一看整体框架:

namespace hwc
{
    //结点
	template<class T>
	struct list_node
	{
		list_node* _next;
		list_node* _prev;
		T _data;

		list_node(const T& x)
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{}
	};

	template<class T>
	class list
	{
		typedef list_node<T> node;
	public:
        //迭代器
		typedef __list_iterator<T> iterator;
        typedef __list_const_iterator<T> const_iterator;
        //构造
		list()
		{
			_head = new node(T());
			_head->_next = _head;
			_head->_prev = _head;
		}
	private:
		node* _head;
        size_t _size;
	};
}

第一个要实现测试的是push_back,后面可以直接复用insert():

尾插就是把新结点和原来的尾结点和头结点进行链接,这是我们很熟悉的了,直接动手实现:

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;
}

image-20221210091750913

这个接口是最基础的,有了这个接口,我们对于后面的实现更加容易。


二、迭代器

1、list迭代器的引入

把原生指针直接命名为迭代器?迭代器的价值在于封装底层的实现,不具体暴露底层的实现细节,提供统一的访问方式。

对于vector和string类而言,物理空间是连续的,原生的指针就是迭代器了(不一定哦,只是可能,版本可能不同),解引用就是数据了。但是对于这里的list而言,空间是不连续的,我们知道,迭代器有两个特征:1.解引用2.++ /–

此时如果解引用是拿不到数据的(空间不连续),更不用说++指向下一个结点了。所以,对于list的迭代器,原生指针已经不符合我们的需求了,我们需要去进行特殊处理:进行类的封装。我们可以通过类的封装以及运算符重载支持,这样就可以实现像内置类型一样的运算符。

2、迭代器的区分

const迭代器需要在这里重新说明一遍:

//1.const T* p1
list<int>::const_iterator cit = lt.begin();
//2.T* const p2
const list<int>::iterator cit = lt.begin();
//不符合const迭代器的行为,因为保护迭代器本身不能修改,那么我们也就不能++迭代器

灵魂拷问:const迭代器是p1还是p2?p1

const迭代器类似p1的行为,保护指向的对象不被修改,迭代器本身可以修改

3、list迭代器的实现

迭代器的实现我们需要去考虑普通迭代器和const迭代器。这两种迭代器的不同,也会带来不同的接口。我们可以分别单独去进行实现,我们先来看一看简单的构造迭代器,只需要提供一个结点即可,看一看实现的基本框架:

    template<class T>
	struct __list_iterator
	{
		typedef list_node<T> node;
		node* _pnode;

		__list_iterator(node* p)
			:_pnode(p)
		{}
    }

注意:对于迭代器的拷贝构造和赋值重载我们并不需要自己去手动实现,编译器默认生成的就是浅拷贝,而我们需要的就是浅拷贝,这也说明了,并不是说如果有指针就需要我们去实现深拷贝。另外,迭代器通过结构体指针访问修改链表,所以,对于迭代器我们并不需要构造函数,结点的释放由链表管理。

下面,我们来看看迭代器所需要的重载运算符:

重载*

这个比较简单,就是要获取迭代器指向的数据,并且返回数据的引用:

T& operator*()
{
    return _pnode->_data;
}

重载++、–、!=

	   __list_iterator<T>& operator++()
		{
			_pnode = _pnode->_next;
			return *this;
		}

		__list_iterator<T>& operator--()
		{
			_pnode = _pnode->_prev;
			return *this;
		}

		bool operator!=(const __list_iterator<T>& it)
		{
			return _pnode != it._pnode;
		}

如果按照上面的做法,我们在来看看此时普通迭代器和const迭代器的区别:

//typedef __list_iterator<T> iterator;
//typedef __list_const_iterator<T> const_iterator;
    template<class T>
	struct __list_iterator
	{
		typedef list_node<T> node;
		node* _pnode;

		__list_iterator(node* p)
			:_pnode(p)
		{}

		T& operator*()
		{
			return _pnode->_data;
		}

		__list_iterator<T>& operator++()
		{
			_pnode = _pnode->_next;
			return *this;
		}

		__list_iterator<T>& operator--()
		{
			_pnode = _pnode->_prev;
			return *this;
		}

		bool operator!=(const __list_iterator<T>& it)
		{
			return _pnode != it._pnode;
		}
	};

	//跟普通迭代器的区别:遍历,不能用*it修改数据
	template<class T>
	struct __list_const_iterator
	{
		typedef list_node<T> node;
		node* _pnode;

		__list_const_iterator(node* p)
			:_pnode(p)
		{}

		const T& operator*()
		{
			return _pnode->_data;
		}

		__list_const_iterator<T>& operator++()
		{
			_pnode = _pnode->_next;
			return *this;
		}

		__list_const_iterator<T>& operator--()
		{
			_pnode = _pnode->_prev;
			return *this;
		}

		bool operator!=(const __list_const_iterator<T>& it)
		{
			return _pnode != it._pnode;
		}
	};

如果是这样子去实现的话,我们就会发现,这两个迭代器的实现并没有多大的区别,唯一的区别就在于operator*的不同。const迭代器和普通迭代器的唯一区别就是普通迭代器返回T&,可读可写,const迭代器返回const T&,可读不可写,上面的代码存在很大的问题:代码冗余,所以我们应该去解决这个问题:我们可以参考源码的实现:类模板参数解决这个问题,这也是迭代器的强大之处

4、模板

template <class T,class Ref,class Ptr>
//typedef __list_iterator<T, T&, T*> iterator;
//typedef __list_iterator<T, const T&, const T*> const_iterator;

同一个类模板,此时我们传递不同的参数实例化成不同的迭代器了!!!这解决了我们刚刚所说的代码冗余问题。

    //  typedef __list_iterator<T, T&, T*> iterator;
	//  typedef __list_iterator<T, const T&, const T*> const_iterator;
	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
		typedef list_node<T> node;
		typedef __list_iterator<T, Ref, Ptr> Self;
		node* _pnode;
		__list_iterator(node*p)
			:_pnode(p)
		{
		}
		//返回数据的指针
		Ptr operator->()
		{
			return &_pnode->_data;
		}
		//模板参数做返回值
		Ref operator *()
		{
			return _pnode->_data;
		}

		//++it
		Self& operator ++()
		{
			_pnode = _pnode->_next;
			return *this;
		}

		//it++
		Self operator ++(int)
		{
			Self tmp(*this);
			_pnode = _pnode->_next;
			return tmp;
		}

		Self& operator--()
		{
			_pnode = _pnode->_prev;
			return *this;
		}

		Self operator--(int)
		{
			Self tmp(*this);
			_pnode = _pnode->_prev;
			return tmp;
		}

		bool operator !=(const Self& it)const
		{
			return _pnode != it._pnode;
		}

		bool operator ==(const Self& it)const
		{
			return _pnode == it._pnode;
		}
	};

重载->问题:

对于内置类型我们可以通过*解引用访问数据,但是如果是自定义类型,我们访问其中的成员,就需要重载->:

struct Pos
{
    int _row;
	int _col;
	Pos(int row = 0, int col = 0)
        :_row(row)
		, _col(col)
		{}
};
void test_list()
{
    int x = 5;
    int*p=&x;
    cout<<*p1<<endl;
    list<Pos> lt;
	Pos p1(1, 1);
	lt.push_back(p1);
	lt.push_back(p1);
	lt.push_back(p1);
	lt.push_back(Pos(2, 2));
	lt.push_back(Pos(3, 3));
	list<Pos>::iterator it = lt.begin();
	while (it != lt.end())
    {
        it->_row++;
		//cout << (&(*it))->_row << ":" << (*it)._col << endl;
		cout << it->_row << ":" << it->_col << endl;
		//cout << it.operator->()->_row << ":" << it->_col << endl;
		++it;
    }
    cout << endl;
}

对于内置类型(如上面的int),我们自然可以通过解引用访问。对于结构体指针,我们也可以(*it)对象.去进行访问成员,但是想通过->访问成员,我们就需要去进行重载了:

Ptr operator->()
{
    return &_pnode->_data;
}

image-20221208102139457

实际上本来应该是it->->_row,但是这样写可读性太差,也不好用,所以编译器做了优化,省略了一个->。


三、增删查改

1、insert和erase

insert:在pos位置上一个插入,返回插入位置的迭代器,对于list的insert迭代器不会失效,vector失效是因为扩容导致pos位置造成野指针问题。

		iterator insert(iterator pos,const T& x)
		{
			node* newnode = new node(x);
			node* cur = pos._pnode;
			node* prev = cur->_prev;

			newnode->_prev = prev;
			prev->_next = newnode;
			newnode->_next = cur;
			cur->_prev = newnode;

			++_size;
			return iterator(newnode);
		}

image-20221210092443015

erase:这里的带头(哨兵位)头结点不可删除,返回值是删除位置的下一个,对于list的erase迭代器是失效的

		iterator erase(iterator pos)
		{
			assert(pos != end());
			node* prev = pos._pnode->_prev;
			node* next = pos._pnode->_next;

			prev->_next = next;
			next->_prev = prev;
			delete pos._pnode;
			--_size;
			return iterator(next);
		}

2、push_back和push_front

push_back我们前面已经实现过了,这个地方只需要复用insert即可

        void push_back(const T& x)
		{
			/*node* newnode = new node(x);
			node* tail = _head->_prev;

			newnode->_prev = tail;
			tial->_next = newnode;
			newnode->_next = _head;
			_head->_prev = newnode;*/
			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

对于end和begin的位置:

image-20221210093213762

3、pop_back和pop_front

尾删和头删,复用erase即可

		void pop_front()
		{
			erase(begin());
		}

		void pop_back()
		{
			erase(--end());
		}

这里的尾删刚好用上了我们的重载–


四、list的接口

1、构造

默认构造

list()
{
    _head = new node(T());
	_head->_next = _head;
	_head->_prev = _head;
	_size = 0;
}

我们可以用empty_initialize()来封装初始化,方便复用,不用每次都写:

void empty_initialize()
{
    _head = new node(T());
    _head->_next = _head;
	_head->_prev = _head;
	_size = 0;
}

迭代器区间构造

	    //迭代器区间构造
		template <class InputIterator>
		list(InputIterator first, InputIterator last)
		{
			empty_initialize();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

拷贝构造

传统写法

		list(const list<T>& lt)
		{
			empty_initialize();
			for (const auto& e : lt)
			{
				push_back(e);
			}
		}

用范围for进行尾插,但是要注意要加上&,范围for是*it赋值给给e,又是一个拷贝,e是T类型对象,依次取得容器中的数据,T如果是string类型,不断拷贝,push_back之后又销毁。

现代写法

		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}		
		list(const list<T>& lt)
		{
			empty_initialize();
			list<T> tmp(lt.begin(), lt.end());
			swap(tmp);
		}

2、析构

对于list,有单独的clear()接口,list的析构可以直接复用clear(),同时还需要我们去释放掉头结点:

		~list()
		{
			clear();
            delete _head;
			_head = nullptr;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

3、赋值重载

传统写法

		list<T>& operator=(list<T>& lt)
		{
			if (this != &lt)
			{
				clear();
				for (const auto& e : lt)
				{
					push_back(e);
				}
			}
			return *this;
		}

现代写法

		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

类名和类型问题

查看官方文档,我们可以看到list没有类型:

image-20221210084548413

list<T>& operator=(list<T> lt)
list& operator=(list lt) 

对于普通类:类名等价于类型,对于类模板:类名不等价于类型(如list模板,类名:list 类型:list)

类模板里面可以用类名代表类型,但是并不建议,在类外面则必须要带模板参数list


五、list和vector的对比

vector:vector的优点在于下标的随机访问,尾插尾删效率高,CPU高速缓存命中高。而缺点在于前面部分插入删除数据效率低O(N),扩容有消耗,还存一定空间浪费。

list:list的优点在于无需扩容,按需申请释放,在任意位置插入删除O(1)。缺点在于不支持下标的随机访问,CPU高速缓存命中低。

vector和list的关系就想是在互补配合!

vector list
底 层 结 构 动态顺序表,一段连续空间 带头结点的双向循环链表
随 机 访 问 支持随机访问,访问某个元素效率O(1) 不支持随机访问,访问某个元素 效率O(N)
插 入 和 删 除 任意位置插入和删除效率低,需要搬移元素,时间复杂 度为O(N),插入时有可能需要增容,增容:开辟新空 间,拷贝元素,释放旧空间,导致效率更低 任意位置插入和删除效率高,不 需要搬移元素,时间复杂度为 O(1)
空 间 利 用 率 底层为连续空间,不容易造成内存碎片,空间利用率 高,缓存利用率高 底层节点动态开辟,小节点容易 造成内存碎片,空间利用率低, 缓存利用率低
迭 代 器 原生态指针 对原生态指针(节点指针)进行封装
迭 代 器 失 效 在插入元素时,要给所有的迭代器重新赋值,因为插入 元素有可能会导致重新扩容,致使原来迭代器失效,删 除时,当前迭代器需要重新赋值否则会失效 插入元素不会导致迭代器失效, 删除元素时,只会导致当前迭代器失效,其他迭代器不受影响
使 用 场 景 需要高效存储,支持随机访问,不关心插入删除效率 大量插入和删除操作,不关心随 机访问

而对于string的insert和erase迭代器也会失效跟vector类似。但是我们并不太关注。因为string的接口参数大部分是下标支持,迭代器反而用得少。

list.h

#pragma once
namespace hwc
{
	template <class T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _data;

		list_node(const T& x)
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{
		}
	};


	//	typedef __list_iterator<T, T&, T*> iterator;
	//  typedef __list_iterator<T, const T&, const T*> const_iterator;
	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
		typedef list_node<T> node;
		typedef __list_iterator<T, Ref, Ptr> Self;
		node* _pnode;
		__list_iterator(node*p)
			:_pnode(p)
		{
		}
		//返回数据的指针
		Ptr operator->()
		{
			return &_pnode->_data;
		}
		//模板参数做返回值
		Ref operator *()
		{
			return _pnode->_data;
		}

		//++it
		Self& operator ++()
		{
			_pnode = _pnode->_next;
			return *this;
		}

		//it++
		Self operator ++(int)
		{
			Self tmp(*this);
			_pnode = _pnode->_next;
			return tmp;
		}

		Self& operator--()
		{
			_pnode = _pnode->_prev;
			return *this;
		}

		Self operator--(int)
		{
			Self tmp(*this);
			_pnode = _pnode->_prev;
			return tmp;
		}

		bool operator !=(const Self& it)const
		{
			return _pnode != it._pnode;
		}

		bool operator ==(const Self& it)const
		{
			return _pnode == it._pnode;
		}
	};


	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_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end() const
		{
			return const_iterator(_head);
		}

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			//iterator it(_head);
			//return it;
			return iterator(_head);
		}


		void empty_initialize()
		{
			_head = new node(T());
			_head->_next = _head;
			_head->_prev = _head;

			_size = 0;

		}

		//迭代器区间构造
		template <class InputIterator>
		list(InputIterator first, InputIterator last)
		{
			empty_initialize();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}

		/*list(const list<T>& lt)
		{
			empty_initialize();
			for (const auto& e : lt)
			{
				push_back(e);
			}
		}*/
		list(const list<T>& lt)
		{
			empty_initialize();
			list<T> tmp(lt.begin(), lt.end());
			swap(tmp);
		}

		/*list<T>& operator=(list<T>& lt)
		{
			if (this != &lt)
			{
				clear();
				for (const auto& e : lt)
				{
					push_back(e);
				}
			}
			return *this;
		}*/
		//现代写法
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

		size_t size()const
		{
			return _size;
		}

		bool empty() const
		{
			return _size == 0;
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		list()
		{
			empty_initialize();
		}

		void push_back(const T& x)
		{
			/*node* newnode = new node(x);
			node* tail = _head->_prev;

			newnode->_prev = tail;
			tial->_next = newnode;
			newnode->_next = _head;
			_head->_prev = newnode;*/
			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_front()
		{
			erase(begin());
		}

		void pop_back()
		{
			erase(--end());
		}

		iterator insert(iterator pos,const T& x)
		{
			node* newnode = new node(x);
			node* cur = pos._pnode;
			node* prev = cur->_prev;

			newnode->_prev = prev;
			prev->_next = newnode;
			newnode->_next = cur;
			cur->_prev = newnode;

			++_size;
			return iterator(newnode);
		}
		
		iterator erase(iterator pos)
		{
			assert(pos != end());
			node* prev = pos._pnode->_prev;
			node* next = pos._pnode->_next;

			prev->_next = next;
			next->_prev = prev;
			delete pos._pnode;
			--_size;
			return iterator(next);
		}
		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);
		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_list2()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_front(5);
		lt.push_front(6);

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;

		lt.pop_back();
		lt.pop_back();
		lt.pop_front();
		lt.pop_front();

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_list3()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_front(5);
		lt.push_front(6);

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
		cout << lt.size() << endl;

		list<int> lt1(lt);
		for (auto e : lt1)
		{
			cout << e << " ";
		}
		cout << endl;
		list<int> lt2;
		lt2.push_back(10);
		lt2.push_back(20);
		lt2.push_back(30);
		lt2.push_back(40);
		cout << lt2.size() << endl;

		lt = lt2;
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void print_list(const list<int>& lt)
	{
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			// (*it) += 2; // 不能写
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	void test_list4()
	{
		list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);

		list<int>::iterator it = lt1.begin();
		while (it != lt1.end())
		{
			(*it) += 2;
			cout << *it << " ";
			++it;
		}
		cout << endl;
		print_list(lt1);
	}

	struct Pos
	{
		int _row;
		int _col;

		Pos(int row = 0, int col = 0)
			:_row(row)
			, _col(col)
		{}
	};

	void print_list(const list<Pos>& lt)
	{
		list<Pos>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << it->_row << ":" << it->_col << endl;
			++it;
		}
		cout << endl;
	}

	void test_list5()
	{
		list<Pos> lt;
		Pos p1(1, 1);
		lt.push_back(p1);
		lt.push_back(p1);
		lt.push_back(p1);
		lt.push_back(Pos(2, 2));
		lt.push_back(Pos(3, 3));
		list<Pos>::iterator it = lt.begin();
		while (it != lt.end())
		{
			it->_row++;
			cout << it->_row << ":" << it->_col << endl;
			++it;
		}
		cout << endl;
		print_list(lt);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_60478154/article/details/128262211