【c++】list模拟实现(1)

简介: 【c++】list模拟实现(1)

list的接口

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace zjw
{
  template<class T>
  struct listnode    
  {
    listnode* <T>_next;
    listnode* <T>_prev;
    T _data;
    listnode(const T& x = T())
      :_prev(nulllptr)
      ,_next(nullptr)
      ,_data(x)
    {
    
    
    
    
    }
  };
  template<class T>
  struct _list_iterator
  {
    typedef listnode <T>node;
    
    typedef _list_iterator<T>self;
    node* _node;
    _list_iterator(node* n)
      :_node(n)
    {
    }
    self& operator++()
    {
    }
    self& operator--()
    {
    }
    self operator++(int)
    {
    }
    self operator--(int)
    {
    }
    bool operator!=(const self& s)
    {
    
    }
    bool operator==(const self& s)
    {
    }
    T& operator()
    {
    
    }
  };
     template <class T>
   class list
   {    typedef listnode <T>node;
    public:
     typedef _list_iterator<T>  iterator;
     list()
     {
       empty_init();
     
     }
     void empty_init()
     {
     
     
     }
     iterator begin()
     {
     
     }
     iterator end()
     {
     
     
     
     }
     void clear()
     {
     
     
     }
     ~list
     {
     
     
     }
     list(const list<T>& it)
     {
     
     
     }
     void push_back(const T&x)
     {
     
     
     
     }
     void push_front(const T& x)
     {
     }
     void pop_back()
     {
     
     
     
     }
     void pop_front()
     {
     
     
     
     
     }
     void swap(list<T>& tmp)
     {
       
     }
     list<T>& operator=(list<T>it)
     {
     
     
     
     }
     iterator insert(iterator pos ,const T&x)
     {
     
     
     
     
     
     }
     iterator erase(iterator pos)
     {
     
     
     
     
     }
   private :
     node* _head;
   };
}

迭代器前置++,前置–,后置++,后置–

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

迭代器重载等于,以及不等于,以及解引用访问数据

bool operator!=(const self& s)
    {
      return s._node != _node;
    
    }
    bool operator==(const self& s)
    {
      return s._node == _node;
    }
    T& operator*()
    {
      return _node->_data;
    
    }
    

链表的默认构造,以及尾插,获取链表的头和尾巴,以及测试尾插函数

list()
     {
       empty_init();
     
     }
     void empty_init()
     {
       _head = new node();
       _head->_prev = _head;
       _head->_next = _head;
     
     
     }
     iterator begin()
     {
       return _head->_next;
     
     }
     iterator end()
     {
       return _head;
     
     
     }
       void push_back(const T&x)
     {
       node* newnode = new node(x);
       node* tail = _head->_prev;
       newnode->_next = _head;
       newnode->_prev = tail;
       tail->_next = newnode;
       _head->_prev = newnode;
       
     
     
     
     }

尾插测试,以及迭代器测试

void test1()
   {
     list<int>res;
     res.push_back(1);
     res.push_back(2);
     res.push_back(3);
     res.push_back(4);
     list<int> ::iterator it = res.begin();
     while (it != res.end())
     {
       cout <<*it << " ";
       it++;
      }
 
   }


const迭代器的实现

我们就可以新增两个带const的begin(),end(),权限平移就可以调动。

iterator begin() const
     {
       return _head->_next;
     }
     iterator end() const
     {
       return _head;
     }

如果我们要修改迭代器所指向位置的值呢??

我们应该如何模拟实现一个常量迭代器呢??

如果按照以下的方式,可以实现吗??

所以我们应该怎么实现呢??

迭代器中的解引用函数,我们需要让他的返回值不被修改,所以 这个函数的返回值加const 就好了,所以我们在实现一个类,这个类基本和普通迭代器的类一样,只是在解引用函数上有区分。

template<class T>
  struct const_list_iterator
  {
    typedef    listnode <T>  node;
    typedef const_list_iterator<T> self;
    node* _node;
    const_list_iterator(node* n)
      :_node(n)
    {
    }
    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;
    }
    bool operator !=(const self& s)
    {
      return s._node != _node;
    }
    bool operator==(const self& s)
    {
      return s._node == _node;
    }
    const T operator*()
    {
      return _node->_data;
    }
  };

这样子,就实现了const 的迭代器,但是重新搞一个类好麻烦呀,于是有大佬就想出了在提供一个模板参数来解决这个问题


->运算符重载函数

我们可以想一下什么时候会用到->,当指针it指向的是结构体的话,我们可以访问使用

it->data,来访问数据,也可先对指针解引用*it,*it表示拿到这个结构体,然后使用.来访问(*it).data;

T*  operator->()
    {
      return &_node->_data;
    }
struct AA
   {
     int a1;
     int a2;
     AA(int _a1=0,int _a2=0)
       :a1(_a1)
       ,a2(_a2)
     
     {
     
     }
   };

测试->

void test3()
   {
     list<AA>res; 
     res.push_back(AA(1,1));
     res.push_back(AA(2, 2));
     res.push_back(AA(3, 3));
     res.push_back(AA(4, 4));
     list<AA> ::iterator it = res.begin();
     while (it != res.end())
     {
       cout << it.operator->()->a1<<":" << it.operator->()->a2 << endl;
       it++;
   }
 
   
   }


这里有一个问题就是->访问也是取数据,但取到的数据能修改吗?这就面临和解引用取数据同样的问题。

我们现在需要做的是const迭代器的不能被将修改,普通的可以修改值

我发现这里和解引用那里不一样的是,解引用那里是值不可被修改,这里是地址不可被修改


=运算符重载赋值

void swap(list<T>& tmp)
     {
       std::swap(_head, tmp._head);
     }
     list<T>& operator=(list<T>it)
     {
       swap(it);
       return *this;
     
     
     
     }

赋值函数测试

void test4()
   {
    
     list<int>res;
     res.push_back(1);
     res.push_back(2);
     res.push_back(3);
     res.push_back(4);
     list<int> ret = res;
     list<int> ::iterator it = ret.begin();
     while (it != ret.end())
     {
       cout << *it << " ";
       it++;
     }
    
   
   
   
   }



目录
相关文章
|
2月前
|
存储 缓存 C语言
【C++】list介绍以及模拟实现(超级详细)
【C++】list介绍以及模拟实现(超级详细)
64 5
|
2月前
|
存储 缓存 算法
【C++】list的模拟实现
【C++】list的模拟实现
|
2月前
|
存储 C++ 容器
【C++】list的认识与使用
【C++】list的认识与使用
|
3月前
|
存储 Java C++
【c++】list详细讲解
【c++】list详细讲解
41 5
|
3月前
|
Java C++ Python
【c++】list 模拟
【c++】list 模拟
22 1
|
3月前
|
存储 编译器 C语言
【C++】list模拟实现
本文档介绍了C++ STL中`list`容器的模拟实现,包括`ListNode`节点类、迭代器类和`list`类的详细设计。`ListNode`模板类存储数据并维护前后指针;`ListIterator`是一个复杂的模板类,提供解引用、自增/自减以及比较操作。`list`类包含了链表的各种操作,如插入、删除、访问元素等,并使用迭代器作为访问接口。实现中,迭代器不再是简单的指针,而是拥有完整功能的对象。此外,文档还提到了迭代器的实现对C++语法的特殊处理,使得`it-&gt;_val`的写法成为可能。文章通过分步骤展示`list`的各个组件的实现,帮助读者深入理解STL容器的内部工作原理。
|
3月前
|
算法 搜索推荐 C++
【C++】list的使用(下)
`C++` 中 `std::list` 的 `merge()`、`sort()` 和 `reverse()` 操作: - `merge(x)` 和 `merge(x, comp)`: 合并两个已排序的`list`,将`x`的元素按顺序插入当前`list`,`x`清空。比较可自定义。 - `sort()` 和 `sort(comp)`: 对`list`元素排序,保持等价元素相对顺序。内置排序基于稳定排序算法,速度较慢。 -reverse(): 反转`list`中元素的顺序。 这些操作不涉及元素构造/销毁,直接移动元素。注意,`sort()`不适合`std::list`,因链表结构不利于快速排序
|
3月前
|
C++ 容器
【C++】list的使用(下)
这篇博客探讨了C++ STL中`list`容器的几个关键操作,包括`splice()`、`remove()`、`remove_if()`和`unique()`。`splice()`允许高效地合并或移动`list`中的元素,无需构造或销毁。`remove()`根据值删除元素,而`remove_if()`则基于谓词移除元素。`unique()`则去除连续重复的元素,可选地使用自定义比较函数。每个操作都附带了代码示例以说明其用法。
|
3月前
|
编译器 C++ 容器
【C++】list的使用(上)
迭代器在STL中统一了访问接口,如`list`的`begin()`和`end()`。示例展示了如何使用正向和反向迭代器遍历`list`。注意`list`的迭代器不支持加减操作,只能用`++`和`--`。容器的`empty()`和`size()`用于检查状态和获取元素数。`front()`和`back()`访问首尾元素,`assign()`重载函数用于替换内容,`push_*/pop_*`管理两端元素,`insert()`插入元素,`erase()`删除元素,`resize()`调整大小,`clear()`清空容器。这些接口与`vector`和`string`类似,方便使用。
|
3月前
|
存储 C++
C++的list-map链表与映射表
```markdown C++ 中的`list`和`map`提供链表和映射表功能。`list`是双向链表,支持头尾插入删除(`push_front/push_back/pop_front/pop_back`),迭代器遍历及任意位置插入删除。`map`是键值对集合,自动按键排序,支持直接通过键来添加、修改和删除元素。两者均能使用范围for循环遍历,`map`的`count`函数用于统计键值出现次数。 ```
28 1