c++中list的简单实现

文章目录

  • list
    • 介绍
    • 节点类(listNode)
    • __list__iterator(迭代器类)
    • list的成员函数
      • [empty_init() 初始化节点](#empty_init() 初始化节点)
      • [list(list<T>& lt) 拷贝构造](#list(list<T>& lt) 拷贝构造)
      • [clear() 清除链表](#clear() 清除链表)
      • [~list() 析构](#~list() 析构)
      • [insert() 插入](#insert() 插入)
      • [erase() 删除](#erase() 删除)
      • [push_back() 尾插](#push_back() 尾插)
      • [push_front() 头插](#push_front() 头插)
      • [pop_back() 尾删](#pop_back() 尾删)
      • [pop_front() 头删](#pop_front() 头删)
      • [begin() 头节点](#begin() 头节点)
      • [end() 尾节点](#end() 尾节点)
  • 总结

list

介绍

list:
是数据结构中的链表,存储方式是在内存中每一个节点取一段空间用特定的方式链接起来,这样子就不会有浪费的空间

我们用的是带头循环双向链表

节点类(listNode)

因为一个节点中要包含其他信息所以单独弄成一个类

cpp 复制代码
template<class T>
//链表节点类
struct listNode
{
	listNode<T>* _next;//指向下一个节点
	listNode<T>* _prev;//指向上一个节点
	T date;//内容
	
	listNode(const T& x = T())
		:_next(nullptr)
		,_prev(nullptr)
		,date(x)
	{}
};

__list__iterator(迭代器类)

为什么要有迭代器类呢?

因为我们要封装一下这个迭代器,让迭代器该有的操作在list也可以用出来。

如果在list内部弄迭代器会很不好弄。

cpp 复制代码
//正向迭代器类
//Ref 来区别const和普通
template<class T,class Ref,class Ptr>
struct __list__iterator
{
	typedef listNode<T> Node;//减少代码
	typedef __list__iterator<T,Ref> self;//来控制他的类别
	Node* _node;//节点
	__list__iterator(Node* node)
		:_node(node)
	{}
	//++it
	self& operator++()
	{
		_node = _node->_next;
		return *this;
	}
	//it++
	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;
	}
	Ref operator*()
	{
		return _node->date;
	}
	bool operator!=(const self& s)
	{
		return _node != s._node;
	}
	bool operator==(const self& s)
	{
		return _node == s._node;
	}
	Ptr operator->()
	{
		return &_node->date;
	}
};

operator->

因为这个比较特殊很难看懂所以我们单独解释

他的本质是->->,代码解释更好看懂

因为之前的设计者觉得不好看,所以做的特殊处理

cpp 复制代码
struct MyStruct
{
	int _a1;
	int _a2;
	MyStruct(int _a1 = 1, int _a2 = 1)
		:_a1(_a1)
		,_a2(_a2)
	{

	}
};
void test2()
{
	list<MyStruct> s;
	s.push_back(MyStruct());
	s.push_back(MyStruct());
	s.push_back(MyStruct());//插入的是一个类
	list<MyStruct>::iterator lt = s.begin();
	while (lt != s.end())
	{
		cout << lt->_a1<<":"<< lt->_a2 << " ";//读这个类里面的内容
		//lt->_a1 的本质是 lt.operator->()->_a1; 特殊处理
		++lt;
	}
}

list的成员函数

因为我们偷点懒所以把一些东西typedef一下

cpp 复制代码
	typedef listNode<T> Node;
	typedef __list__iterator<T,T&,T*> iterator;
	typedef __list__iterator<T,const T&,const T*> const_iterator;
	typedef Reverselterator<T, T&, T*> reverse_iterator;

empty_init() 初始化节点

因为我们要多次用到所以单独写一个出来方便
同时可以用list()套一下他

cpp 复制代码
void empty_init()//初始化节点
{
	_head = new Node;
	_head->_next = _head;
	_head->_prev = _head;
}

list(list& lt) 拷贝构造

简单写法

引用的作用是深拷贝,不要弄成浅拷贝了

cpp 复制代码
list(list<T>& lt)
{
	empty_init();
	for (const auto& ch : lt)
	{
		push_back(ch);
	}
}

clear() 清除链表

直接把链表清空

cpp 复制代码
void clear()
{
	iterator it = begin();
	while (it != end())
	{
		it = erase(it);
	}
}

~list() 析构

清除之后直接把哨兵位删掉就可以了

cpp 复制代码
~list()
{
	clear();
	delete _head;
}

insert() 插入

在某个节点之前插入

cpp 复制代码
iterator insert(iterator pos, const T& x)//在摸个节点之前插入
{
	Node* cur = pos._node;//插入的位置
	Node* prev = cur->_prev;//插入的下一个位置
	Node* newnode = new Node(x);//构成节点
	prev->_next = newnode;
	newnode->_prev = prev;
	newnode->_next = cur;
	cur->_prev = newnode;
	return newnode;
}

erase() 删除

删除某个节点

cpp 复制代码
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 next;
}

push_back() 尾插

注释这段因为和insert中基本上没区别,所以简单化了

cpp 复制代码
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;*/
	insert(end(), x);
}

push_front() 头插

cpp 复制代码
void push_front(const T& x)//头插
{
	insert(begin(), x);
}

pop_back() 尾删

cpp 复制代码
void pop_back()//尾删
{
	erase(--end());
}

pop_front() 头删

cpp 复制代码
void pop_front()//头删
{
	erase(begin());
}

begin() 头节点

这里因为是要头节点,所以我们直接把begin设置为第一个节点(有用的节点)

cpp 复制代码
const_iterator begin() const
{
	return _head->_next;
}

end() 尾节点

因为end是尾 所以我们把哨兵位当尾,这样子就可以更好的读

cpp 复制代码
const_iterator end() const
{
	return _head;
}

总结

可以先尝试一下 自己实现

代码总体加我自己的注释给在这里

实现完可以自己对比一下

cpp 复制代码
template<class T>
//链表节点类
struct listNode
{
	listNode<T>* _next;
	listNode<T>* _prev;
	T date;
	
	listNode(const T& x = T())
		:_next(nullptr)
		,_prev(nullptr)
		,date(x)
	{}
};
	//正向迭代器类
	//Ref 来区别const和普通
	template<class T,class Ref,class Ptr>
	struct __list__iterator
	{
		typedef listNode<T> Node;
		typedef __list__iterator<T,Ref> self;
		Node* _node;
		__list__iterator(Node* node)
			:_node(node)
		{}
		//++it
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		//it++
		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;
		}
		Ref operator*()
		{
			return _node->date;
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;
		}
		bool operator==(const self& s)
		{
			return _node == s._node;
		}
		Ptr operator->()
		{
			return &_node->date;
		}
	};
template<class T>
class list
{
	typedef listNode<T> Node;
public:
	typedef __list__iterator<T,T&,T*> iterator;
	typedef __list__iterator<T,const T&,const T*> const_iterator;
	typedef Reverselterator<T, T&, T*> reverse_iterator;
	const_iterator begin() const
	{
		return _head->_next;
	}
	const_iterator end() const
	{
		return _head;
	}
	iterator begin()
	{
		//return iterator(_head->_next);
		return _head->_next;
	}
	iterator end()
	{
		//return iterator(_head);
		return _head;
	}
	iterator rbegin()
	{
		return end();
	}
	iterator rend()
	{
		return begin();
	}
	void empty_init()//初始化节点
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
	}
	list()
	{
		empty_init();
	}
	list(list<T>& lt)
	{
		empty_init();
		for (const auto& ch : lt)
		{
			push_back(ch);
		}
	}
	~list()
	{
		clear();
		delete _head;
	}
	void clear()
	{
		iterator it = begin();
		while (it != end())
		{
			it = erase(it);
		}
	}
	void swap(list<T>& lt)
	{
		std::swap(_head, lt._head);
	}
	list<T>& operator=(list<T> lt)
	{
		swap(lt);
		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;*/
		insert(end(), x);
	}
	void push_front(const T& x)//头插
	{
		insert(begin(), x);
	}
	void pop_back()//尾删
	{
		erase(--end());
	}
	void pop_front()//头删
	{
		erase(begin());
	}
	iterator insert(iterator pos, const T& x)//在摸个节点之前插入
	{
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		Node* newnode = new Node(x);
		prev->_next = newnode;
		newnode->_prev = prev;
		newnode->_next = cur;
		cur->_prev = newnode;
		return newnode;
	}
	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 next;
	}
private:
	Node* _head;//哨兵位
};
相关推荐
艾莉丝努力练剑2 小时前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
_殊途3 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
还债大湿兄3 小时前
《C++内存泄漏8大战场:Qt/MFC实战详解 + 面试高频陷阱破解》
c++·qt·mfc
珊瑚里的鱼7 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上7 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
Risehuxyc7 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
不知道叫什么呀7 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
liulilittle7 小时前
.NET ExpandoObject 技术原理解析
开发语言·网络·windows·c#·.net·net·动态编程
秋说8 小时前
【PTA数据结构 | C语言版】顺序队列的3个操作
c语言·数据结构·算法
lifallen8 小时前
Kafka 时间轮深度解析:如何O(1)处理定时任务
java·数据结构·分布式·后端·算法·kafka