list的模拟实现

目录

1.默认成员函数模拟实现

1.1 构造函数(头节点)

1.2 析构函数

1.3 拷贝构造函数

1.4 赋值重载函数

2.增删查改模拟实现

2.1 insert

2.2 erase

2.3 push_back、pop

3.前置++、--、后置++、--

3.1前置: 

3.2后置:

3.3 =、!=

4.普通迭代器和const迭代器

4.1 普通迭代器

4.2 const迭代器

1.默认成员函数模拟实现

1.1 构造函数(头节点)

void empty_init()
{
	_head = new Node;
	_head->_next = _head;
	_head->_prev = _head;
	_size = 0;
}
list()
{
	empty_init();
}

1.2 析构函数

~list()
{
	clear();
	delete _head;
	_head = nullptr;
}
void clear()
{
	//清理数据,不清头节点
	iterator it = begin();
	while (it != end())
	{
		it = erase(it);//it就是下一个位置
	}
}

1.3 拷贝构造函数

先搞头节点  

void empty_init()
{
	_head = new Node;
	_head->_next = _head;
	_head->_prev = _head;
}
//lt2(lt1)   lt2拷贝构造lt1,this就是lt2
list(const list<T>& lt)
{
	empty_init();
	for (auto e : lt)
	{
		push_back(e); 
	}
}

1.4 赋值重载函数

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

2.增删查改模拟实现

2.1 insert

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

2.2 erase

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

2.3 push_back、pop

void push_back(const T& x)
{
	insert(end(), x);
}
void push_front(const T& x)
{
	insert(begin(), x);
}
void pop_front()//头删就是在begin位置删除
{
	erase(begin());
}
void pop_back()//尾删
{
	erase(--end());
}

3.前置++、--、后置++、--

3.1前置: 

self& operator++()
{
	_node = _node->_next;
	return *this;
}
self& operator--()
{
	_node = _node->_prev;
	return *this;
}

3.2后置:

self operator++(int) //能用前置就不要用后置,后置要返回++之后的
{
	self tmp(*this);
	_node = _node->_next;
	return tmp;
}
self operator--(int) 
{
	self tmp(*this);
	_node = _node->_prev;
	return tmp;
}

3.3 =、!=

bool operator!=(const self& s)
{
	return _node != s._node;  //看节点的指针是否相等
}
bool operator==(const self& s)
{
	return _node == s._node;  //看节点的指针是否相等
}

4.普通迭代器和const迭代器

4.1 普通迭代器 iterator

list迭代器不是原生的指针,将节点的指针进行封装,来模拟指针的行为。 普通迭代器是可读可写

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)
		{}
	};
	template<class T>
	struct __list_iterator
	{
		typedef list_node<T> Node;
		typedef __list_iterator<T> 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;
		}
		T& operator*()
		{
			return _node->_data;
		}
		T* operator->()
		{
			return &_node->_data;
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;  //看节点的指针是否相等
		}
		bool operator==(const self& s)
		{
			return _node == s._node;  //看节点的指针是否相等
		}
	};

如果list存的是自定义类型,怎么访问其中的数据呢?

 需要实现重载->  返回的是A对象的指针,在用->访问其中的成员

T* operator->()
{
	return &_node->_data;
}
struct AA
{
	AA(int a1=0,int a2=0)
	:_a1(a1)
	,_a2(a2)
	{}
	int _a1;
	int _a2;
};
void test_list3()
{
	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;
}

4.2 const迭代器 const_iterator

const对象只能使用const类型的迭代器,const是只读的,模拟const T*,是指向的内容不可改变,而不是本身不可改变。const_iterator是一个全新的类型,不是const iterator

template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		typedef __list_iterator<T> iterator;
		typedef __list_const_iterator<T> const_iterator;
		const_iterator begin() const
		{
			return const_iterator(_head->_next);
			//也可以写成 return _head->_next;隐式类型转换,和上面是一样的
		}
		const_iterator end() const//哨兵卫位置
		{
			return const_iterator(_head);
		}
}
template<class T>
	struct __list_const_iterator
	{
		typedef list_node<T> Node;
		typedef __list_const_iterator<T> self;
		Node* _node;
		__list_const_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;
		}
		//返回的是data的别名
		const T& operator*()
		{
			return _node->_data;
		}
		const T* operator->()
		{
			return &_node->_data;
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;  //看节点的指针是否相等
		}
		bool operator==(const self& s)
		{
			return _node == s._node;  //看节点的指针是否相等
		}
	};

使用:

void print_list(const list<int>&lt)
{
	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);
}

但是const迭代器和普通迭代器主要区别就是返回值不一样,所以上面的写法太冗余了。使用模板,交给编译器去做。 

#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>&lt)
	{
		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);
	}
}
//void print_list(const list<int>& lt)
	//{
	//	list<int>::const_iterator it = lt.begin();
	//	while (it != lt.end())
	//	{
	//		//*it = 10;不能修改,调用operator*返回data的别名,加const不能修改
	//		cout << *it << " ";
	//		it++;
	//	}
	//}
针对list
template<typename T>
//template<class T>
void print_list(const list<T>&lt)
{
		//list<T>为实例化的类模板,编译器不能去它里面找
		//编译器就无法list<T>::const_iterator是内嵌类型还是静态成员变量
		//前面加一个typename就是告诉编译器这里是一个类型,等list<T>实例化了再去取
		typename list<T>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			//*it = 10;不能修改,调用operator*返回data的别名,加const不能修改
			cout << *it << " ";
			it++;
		}
}
在升级
template<typename Container>
	void print_container(const Container& con)
	{
		typename Container::const_iterator it = con.begin();
		while (it != con.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);
		list<string> lt1;
		lt1.push_back("1111111111111");
		lt1.push_back("1111111111111");
		lt1.push_back("1111111111111");
		lt1.push_back("1111111111111");
		print_list(lt1);
		vector<string> v;
		v.push_back("222222222222222");
		v.push_back("222222222222222");
		v.push_back("222222222222222");
		v.push_back("222222222222222");
		print_container(v);
}

模板实现泛型编程,本质是本来应该由程序员干的活交给了编译器 

 

  • 29
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用和提供了关于实现vector的两种方法。其中,引用展示了一个使用reserve和push_back方法的示例,而引用展示了一个使用new和memcpy函数的示例。这两种方法都是常见的实现vector的方式。 在第一种方法中,通过reserve函数可以预留足够的内存空间,然后使用push_back函数逐个将元素添加到vector中。这种方法的好处是可以避免不必要的内存重分配,提高了效率。 而第二种方法使用new操作符在堆上分配内存空间,并使用memcpy函数将已有的vector对象的数据复制到新的内存空间中。通过这种方式,可以实现深拷贝,即两个vector对象拥有独立的内存空间。这种方法的好处是可以在不修改原始vector对象的情况下创建一个新的vector对象。 除了以上两种方法,还可以使用其他方式实现vector类。例如,可以使用动态数组来实现vector的底层数据结构,然后通过成员函数实现vector的各种操作,如增加、删除、查找等。 总结来说,c语言模拟实现vector的关键是动态内存管理和对元素的增删改查操作。可以使用预留空间和逐个添加元素的方式,也可以使用动态数组和复制数据的方式来实现vector类。具体的实现方式可以根据需求和实际情况选择。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C++——vector模拟实现](https://blog.csdn.net/weixin_49449676/article/details/126813526)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值