【C++篇】list模拟实现

实现接口:

  1. list的无参构造、n个val构造、拷贝构造

  2. operator=重载

  3. 实现迭代器

  4. push_back()

  5. push_front()

  6. erase()

  7. insert()

  8. 头尾删

    #pragma once
    #include<iostream>
    #include<assert.h>
    using namespace std;

    namespace liu
    {
    //定义list节点
    template<class T>
    struct list_node
    {
    T _data;
    list_node<T>* _prev;
    list_node<T>* _next;

    复制代码
     	list_node(const T& x=T())
     		:_data(x)
     		,_prev(nullptr)
     		,_next(nullptr)
     	{}
     };
    
     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)
     	{}
    
     	Ref operator*()
     	{
     		return _node->_data;
     	}
     	
     	Ptr operator->()
     	{
     		return &_node->_data;
     	}
    
     	Self& operator++()
     	{
     		_node = _node->_next;
     		return *this;
     	}
    
     	Self& operator--()
     	{
     		_node = _node->_prev;
     		return *this;
     	}
    
     	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;
     	void empty_init()
     	{
     		_head = new Node();
     		_head->_prev = _head;
     		_head->_next = _head;
     	}
    
     	list()//无参构造
     	{
     		empty_init();
     	}
    
     	list(size_t n,const T& val=T())//n个val
     	{
     		empty_init();
     		for (int i=0;i<n;i++)
     		{
     			push_back(val);
     		}
     	}
    
     	list(const list<T>& i1)
     	{
     		empty_init();
     		for(auto& x:i1)
     		{
     			push_back(x);
     		}
     	}
    
     	~list()
     	{
     		clear();
    
     		delete _head;
     		_head = nullptr;
     	}
    
     	void clear()
     	{
     		auto it = begin();
     		while (it!=end())
     		{
     			it=erase(it);
     		}
     	}
    
     	iterator begin()
     	{
     		return iterator(_head->_next);
     	}
    
     	const_iterator begin()const
     	{
     		return const_iterator(_head->_next);
     	}
    
     	iterator end()
     	{
     		return iterator(_head);
     	}
    
     	const_iterator end()const
     	{
     		return const_iterator(_head);
     	}
    
    
     	iterator insert(iterator pos,const T&val)
     	{
     		
    
     		Node* new_node = new Node(val);
     		Node* cur = pos._node;
     		Node* prev = cur->_prev;
    
     		prev->_next = new_node;
     		new_node->_prev = prev;
    
     		cur->_prev = new_node;
     		new_node->_next = cur;
    
     		return iterator(new_node);
     	}
    
     	iterator erase(iterator pos)
     	{
     		assert(pos!=end());
    
     		Node* del = pos._node;
     		Node* prev = del->_prev;
     		Node* next = del->_next;
    
     		prev->_next = next;
     		next->_prev = prev;
     		delete del;
    
     		return iterator(next);
    
     	}
    
     	void pop_front()
     	{
     		erase(begin());
     	}
    
    
     	void pop_back()
     	{
     		erase(--end());
     	}
    
     	void swap(list<T>& i1)
     	{
     		std::swap(_head,i1._head);
     	}
    
     	
    
     	list<T>& operator=(list<T> il)
     	{
     		swap(il);
     		return *this;
     	}
    
     	void push_back(const T& x)
     	{
     		/*Node* new_node = new Node(x);
     		Node* tail = _head->_prev;
    
     		tail->_next = new_node;
     		new_node->_prev = tail;
    
     		new_node->_next = _head;
     		_head->_prev = new_node;*/
     		insert(end(),x);
     	}
    
     	void push_front(const T& x)
     	{
     		/*Node* new_node = new Node(x);
     		Node* prev = _head->_next;
    
     		_head->_next = new_node;
     		new_node->_prev = _head;
    
     		prev->_prev = new_node;
     		new_node->_next = prev;*/
     		insert(begin(), x);
     	}
     private:
     	Node* _head;//双端链表,哨兵位
     };
    
     template<class T>
     void swap(T&a,T& b)
     {
     	T c(a);
     	a = b;
     	b = c;
     }
     template<class T>
     void swap(list<T>& a, list<T>& b)
     {
     	a.swap(b);
     }
     
    
     void test1()
     {
     	/*liu::list<int>i1;
     	i1.push_back(1);
     	i1.push_back(2);
     	i1.push_back(3);
     	i1.push_back(4);
    
     	i1.push_front(5);
     	i1.push_front(6);
     	i1.push_front(7);
    
     	liu::list<int>::iterator it1 = i1.begin();
     	while (it1!=i1.end())
     	{
     		cout << *it1 << " ";
     		++it1;
     	}
     	cout << endl;
     	liu::list<int>::iterator it2 = i1.begin();
     	it2=i1.erase(it2);
     	while (it2 != i1.end())
     	{
     		cout << *it2 << " ";
     		++it2;
     	}*/
    
     	liu::list<int>i1(10,1);
     	liu::list<int>i2(10,2);
     	liu::swap(i1, i2);
    
     	for (auto x : i2)
     	{
     		cout << x << " ";
     	}
     	cout << endl;
    
     	for (auto x : i1)
     	{
     		cout << x << " ";
     	}
     	cout << endl;
     }

    }

相关推荐
三体世界32 分钟前
TCP传输控制层协议深入理解
linux·服务器·开发语言·网络·c++·网络协议·tcp/ip
zkmall39 分钟前
企业电商平台搭建:ZKmall开源商城服务器部署与容灾方案
运维·服务器·开源
你的冰西瓜1 小时前
C++ 中最短路算法的详细介绍
c++·算法·图论·最短路
随心点儿1 小时前
使用python 将多个docx文件合并为一个word
开发语言·python·多个word合并为一个
不学无术の码农1 小时前
《Effective Python》第十三章 测试与调试——使用 Mock 测试具有复杂依赖的代码
开发语言·python
华不完1 小时前
下一代防火墙混合模式部署
运维·服务器·网络
tomcsdn311 小时前
SMTPman,smtp的端口号是多少全面解析配置
服务器·开发语言·php·smtp·邮件营销·域名邮箱·邮件服务器
x县豆瓣酱1 小时前
ubuntu server配置静态IP
linux·运维·ubuntu
<但凡.1 小时前
数据结构与算法之美:广义表
数据结构·c++·算法
工藤新一¹1 小时前
Linux
linux·运维·服务器