C++ ------>std :: list__链表解析
  L8iEHH07GzZb 2023年11月02日 46 0

各位好友, 接下来 继续推进 List(链表)相关模拟 与解析 !

----->头文件 “List.h”

//List 链表实现

#include <iostream>

using std :: cout;
using std :: endl;

namespace UC
{
  template<class T>
  struct list_node
  {
  	list_node<T>* _next;
    list_node<T>* _prev;
    T _val
    list_nod(const T& val = T())
      :_val(val)
    {}
  };
  
  template<class T, class Ref, class Ptr>
  struct _list_iterator
  {
  	typedef list_node<T> Node;
    typedef _list_iterator<T, T&, T*> iterator;
    typedef _list_iterator<T, const T&, const T*> const_iterator;
    typedef _list_iterator<T, Ref, Ptr> Self;
    Node* _node;
    _list_iterator(Node* node)
      :_node(node)
    {}
    _list_iterator() {}
    _list_iterator(const iterator& x)
      :_node(x._node)
    {}
    Ref operator*()
    {
    	return _node->_val;
    }
    Ptr operator->()
    {
    	return &_node->_val;
    }
    Self& operator++()
    {
    	_node = _node->_next;
      
      return *this;
    }
    Seld operator++(int)
    {
    	Self tamp(*this);
      
      _node = _node->_next;
      
      return tamp;
    }
    Self& operator--()
    {
      _node = _node->_prev;
      
    	return *this;
    }
    Self operator--(int)
    {
      Self tamp(*this);
      
      _node = _node->_prev;
      
    	return tamp;
    }
    bool operator!=()
    {
    	return _node != _node->_val;
    }
    bool operator==()
    {
      return _node == _node->_val;
    }
  };
  
  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;
    
    iterator begin()
    {
    	return iterator(_head->_next);
    }
    iterator end()
    {
    	return _head;
    }
    const_iterator begin() const
    {
      return const_iterator(_head->_prev);
    }
    const_iterator end() const
    {
    	return _head;
    }
    list()
    {
   		init_list();
    }
    list(const list<T>& It)
    {
    	init_list();
      for(auto& e : It)
      {
      	push_back(e);
      }
    }
    void swap(const list<T>& It)
    {
    	std :: swap(_head, It._head);
      std :: swap(_size, It._size);
    }
    //拷贝构造
    list<T>&(const list<T> It)
    {
    	swap(It);
      
      return *this;
    }
		//接口实现
    //尾插
    void push_back(const T& x)
    {
    	insert(end(), x);
    }
    //头插
    void push_front(const T& x)
    {
    	insert(begin(), x);
    }
    //尾删
    void pop_back()
    {
    	earse(--end());
    }
    //头删
    void pop_front()
    {
    	earse(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->_next = cur;
      
      cur->_prev = newNode;
      newNode->_prev = cur;
      
      ++_size;
      
      return newNode;
    }
    iterator earse(iterator pos)
    {
      assert(pos != end());
      Node* cur = pos._node;
    	Node* next = cur->_next;
      Node* Prev = cur->_prev;
      
      Prev->_next = next;
      next->_prev = Prev;
      
      --_size;
      
      return next;
    }
    size_t size() const
    {
    	return _size;
    }
    void init_list()
    {
    	_head = new Node;
      _head = _head->_next;
      _head = _head->_prev;
    }
    void clear()
    {
			iterator it = begin();
      while(it != end())
      	it = earse(it);
      
      _size = 0;
    }
    ~list()
    {
      clear();
      
      delete _head;
      _head = nullptr;
    }
    private:
    size_t _size;
    Node* _head;
  }
}

为了方便好友们, 有更好的观感体验  ! 现附上 彩色的代码图样 :>

------>"List.h" ---->NO.1

C++ ------>std :: list__链表解析_测试比较__容器__链表__排序


------>"List.h" ---->NO.2

C++ ------>std :: list__链表解析_List (链表)模拟实现_02


---------->多参数模板(重难点

C++ ------>std :: list__链表解析_测试比较__容器__链表__排序_03


C++ ------>std :: list__链表解析_List (链表)模拟实现_04


首先, 使用 多参数模板 ----->极大节省了时间成本,用起来更加舒服 !

而 模板参数  “Ref ~ Ptr”, 可自动匹配 -------->要实现的接口类型 !(相当 智能)😊

C++ ------>std :: list__链表解析_测试比较__容器__链表__排序_05

上述图示 :> 每一个模板 具体是什么类型并不知道 !只能由后续 实现的接口 进行匹配 !


-------->

C++ ------>std :: list__链表解析_多参数模板__自动匹配__类型_06


------>测试 Vector ~ List 算法排序 :>

------>如下 :>

//测试
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <stdlib.h>

using std :: cout;
using std :: endl;
using std :: vector;
using std :: list;

void test_OP()
{
	srand(time(0));
  const int N = 500000;
  
  vector<int> v;
  
  list<int> It;
  
  for(size_t i = 0; i < N; i++)
  {
  	auto e = rand();
    
    v.push_back(e);
    
    It.push_back(e);
  }
  
  //vector<int> 排序
  int begin1 = clock();
  sort(v.begin(), v..end());
  int end1 = clock();
  
  //list<int> 排序
  int begin2 = clock();
  It.sort();
  int end2 = clock();
  
  cout << "verctor sort :> " << end1 - begin1 << endl;
  cout << "list sort :> " << end2 - begin2 << endl;
}

int main()
{
  test_OP();
	return 0;
}

为了方便好友们, 有更好的观感体验, 现附上 彩色的代码图样 :>

C++ ------>std :: list__链表解析_List (链表)模拟实现_07

----->可见 :

上述 五十万整数 排序  ---------> List (链表) 更胜一筹 !


至此,本期内容 已完结 !下一期, 开战 栈区 ~ 队列 !  "敬请期待 !😊😊


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

L8iEHH07GzZb