list的简单模拟实现

list的简单模拟实现

  • 1.节点结构
  • [2. 迭代器结构](#2. 迭代器结构)
    • [2.1 迭代器结构定义](#2.1 迭代器结构定义)
    • [2. 2 迭代器成员函数的实现](#2. 2 迭代器成员函数的实现)
  • [3. list结构](#3. list结构)
    • [3.1 list结构定义](#3.1 list结构定义)
    • [2. 2 list成员函数的实现](#2. 2 list成员函数的实现)

1.节点结构


cpp 复制代码
template <class T>
struct list_node
{
	
	typedef list_node<T> Node;
	list_node(const T& v = T()) 
		:prev(nullptr), next(nullptr), val(v)
	{
	}
	Node* prev;
	Node* next;
	T val;
};

2. 迭代器结构


2.1 迭代器结构定义

cpp 复制代码
template <class T,class Ref,class Ptr>
struct list_iterator
{
	
	typedef list_node<T> Node;
	typedef list_iterator <T,Ref,Ptr> Self;
	 list_iterator( Node* v)  
		:node(v)  
	{

	}
	list_iterator( const Self& v) 
	{  
		node = v.node;
	}
	Self& operator++()
	{
		node = node->next;
		return *this;
	}
	Self& operator--(int)
	{
		Self tmp = *this;
		node = node->prev;
		return tmp;
	}
	Self& operator++(int)
	{
		Self tmp = *this;
		node = node->next;
		return tmp;
	}
	Self& operator--()
	{
		node = node->prev;
		return *this;
	}
	Ptr operator->()
	{
		return &node->val;
	}
	Ref operator*()
	{
		return node->val;
	}
	bool operator!=(const Self& v)
	{
		return node != v.node;
	}
	bool operator ==(const Self& v)
	{
		return  node == v.node;
	}
	 Node* node;
};

2. 2 迭代器成员函数的实现

cpp 复制代码
list_iterator( Node* v)  
		:node(v)  
	{

	}
	list_iterator( const Self& v) 
	{  
		node = v.node;
	}
	Self& operator++()
	{
		node = node->next;
		return *this;
	}
	Self& operator--(int)
	{
		Self tmp = *this;
		node = node->prev;
		return tmp;
	}
	Self& operator++(int)
	{
		Self tmp = *this;
		node = node->next;
		return tmp;
	}
	Self& operator--()
	{
		node = node->prev;
		return *this;
	}
	Ptr operator->()
	{
		return &node->val;
	}
	Ref operator*()
	{
		return node->val;
	}
	bool operator!=(const Self& v)
	{
		return node != v.node;
	}
	bool operator ==(const Self& v)
	{
		return  node == v.node;
	}

3. list结构


3.1 list结构定义

cpp 复制代码
template <class T>
class list
{
	typedef list_node<T> Node;  //T是aa
public:
	typedef list_iterator<T,T&,T*> iterator;//这个是正常对象的迭代器
	typedef list_iterator<T,const T&,const T*> const_iterator;//这个是const对象的迭代器

	
private:
	Node* head;
	size_t size;
};

2. 2 list成员函数的实现

cpp 复制代码
void empty_init()
	{
		head = new Node;
		head->next = head;
		head->prev = head;
		head->val = T();
		size = 0;
	}
	list()
	{
		empty_init();
	}
	list(int n, const T& value = T())
	{
		empty_init();
		for (int i = 0; i < n; i++)
		{
			push_back(value);
		}
	}
	template <class Iterator>
	list(Iterator first, Iterator last)
	{
		empty_init();
		while (first != last)
		{
			push_back(*first);
			first++;
		}
	}
	list(const list<T>& l)
	{
		empty_init();
		const_iterator it = l.begin();
		while (it != l.end())
		{
			push_back(*it);
			it++;
		}
	}
	void swap(const list<T>& l)
	{
		std::swap(head, l.head);
		std::swap(size, l.size);
	}
	list<T>& operator=(const list<T> l)
	{
		swap(l);
	}
	list(initializer_list<T> l)
	{
		empty_init();
		for (auto& ch : l)
		{
			push_back(ch);
		}
	}
	void clear()
	{
		auto it = begin();
		while (it != end())
		{
			it = erase(it);
		}
	}
	~list()
	{
		clear();
		delete head;
		head == nullptr;
	}
	iterator begin()
	{
		//隐式类型转换
		return head->next;
	}
	iterator end()
	{
		//隐式类型转换
		return head;
	}
	const_iterator begin()const
	{
		//隐式类型转换 
		return head->next;  //构造函数
	}
	const_iterator end()const
	{
		//隐式类型转换
		return head;
	}
	size_t _size()const
	{
		return size;
	}
	bool empty()const
	{
		return head->next == head;
	}
	T& front()
	{
		return head->next;
	}
	const T& front()const
	{
		return head->next;
	}
	T& back()
	{
		return head->prev;
	}
	const T& back()const
	{
		return head->prev;
	}
	void push_back(const T& val = T())
	{
		//先申请一个节点
		/*Node* newnode = new Node(val);
		Node* cur = head->prev;
		cur->next = newnode;
		newnode->prev = cur;
		newnode->next = head;
		head->prev = newnode;
		size++;*/
		insert(end(), val);
	}
	void pop_back() { erase(--end()); }
	void push_front(const T& val=T()) { insert(begin(), val); }
	void pop_front() { erase(begin()); }
	iterator insert(iterator pos,const T& val= T())
	{
		Node* newnode = new Node(val);
		Node* prev = pos.node->prev;
		prev->next = newnode;
		newnode->prev = prev;
		newnode->next = pos.node;
		pos.node->prev = newnode;
		++size;
		return newnode;
	}
	iterator erase(iterator pos)
	{
		assert(pos != end());
		Node* pcur = pos.node;
		Node* prev = pcur->prev;
		Node* next = pcur->next;
		prev->next = next;
		next->prev = prev;
		delete pcur;
		pcur = nullptr;
		--size;
		//这里会调用迭代器的单参数构造函数
		return next;
	}
相关推荐
吴天德少侠13 分钟前
c++返回一个pair类型
开发语言·c++
Pandaconda3 小时前
【C++ 面试 - 新特性】每日 3 题(六)
开发语言·c++·经验分享·笔记·后端·面试·职场和发展
北南京海3 小时前
【C++入门(5)】类和对象(初始类、默认成员函数)
开发语言·数据结构·c++
Yusei_05233 小时前
C++基础知识6 vector
开发语言·c++
黄卷青灯773 小时前
c++ 定义类 介绍
开发语言·c++·定义类
伍心3 小时前
004: VTK读入数据---vtkImageData详细说明
c++·windows·microsoft·mfc·软件工程·visual studio
bbqz0074 小时前
逆向WeChat(六)
c++·微信·小程序·逆向·mojo·嗅探·抓包https·devtool·sniff
我言秋日胜春朝★4 小时前
【C++】list的使用与简单模拟实现
c++
月夕花晨3744 小时前
C++学习笔记(13)
c++·笔记·学习
paidaxing_s4 小时前
【STL中容器汇总】map、list、vector等详解
开发语言·c++·list