前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >list的模拟实现

list的模拟实现

作者头像
code-child
发布2023-05-30 10:09:42
2130
发布2023-05-30 10:09:42
举报
文章被收录于专栏:codechildcodechild

@[TOC]

底层说明:list的底层实现为带头的双向链表


成员变量

代码语言:javascript
复制
cpp	template<class T>
	struct Node
	{
		Node* prve;
		Node* next;
		T data;

		Node(const T &x=T())
			:prve(nullptr)
			,next(nullptr)
			,data(x)
		{}
	};
	template<class T>
	class list
	{
		typedef Node<T> Node;
		Node* head;
	}

构造函数

为什么会加一个empty_init函数呢?因为对于一些含参的构造或者是拷贝构造,都需要初始化,不能让head为野指针。

代码语言:javascript
复制
cpp		void empty_init()
		{
			head = new Node;
			head->prve = head;
			head->next = head;
		}
		list()
		{
			empty_init();
		}

		template<class Iterator>
		list(Iterator first, Iterator last)
		{
			empty_init();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
				list(int n, const T& x = T())
		{
			empty_init();
			while (n--)
			{
				push_back(x);
			}
		}

拷贝构造

代码语言:javascript
复制
cpp		list(const list<T>& it)
		{
			empty_init();
			list<T> temp;
			const_iterator cur = it.begin();
			while (cur != it.end())
			{
				temp.push_back(*cur);
				++cur;
			}
			swap(temp);
		}

析构函数

代码语言:javascript
复制
cpp		~list()
		{
			clear();
			delete head;
			head = nullptr;
		}

迭代器

代码语言:javascript
复制
cpp	template<class T,class ref,class ptr>
	struct __iterator
	{
		typedef Node<T> Node;
		typedef __iterator<T, ref, ptr> Self;
		Node* node;
		
		__iterator(Node* cur)
			:node(cur)
		{}

		Self operator++()
		{
			node = node->next;
			return *this;
		}
		Self operator++(int)
		{
			Node* temp = node;
			node = node->next;
			return Self(temp);
		}
		Self operator--()
		{
			node = node->prve;
			return *this;
		}
		Self operator--(int)
		{
			Node* temp = node;
			node = node->prve;
			return Self(temp);
		}
		ref operator*()
		{
			return node->data;
		}
		ptr operator->()
		{
			return &(node->data);
		}
		bool operator!=(Self x)
		{
			return node != x.node;
		}
	};

任意位置插

代码语言:javascript
复制
cpp	iterator insert(iterator pos, const T& x)
	{
		Node* newnode = new Node(x);
		Node* cur = pos.node;
		Node* pcur = cur->prve;
		pcur->next = newnode;
		newnode->prve = pcur;
		newnode->next = cur;
		cur->prve = newnode;
		return iterator(newnode);
	}

任意位置删

代码语言:javascript
复制
cpp		iterator erase(iterator pos)
		{
			Node* cur = pos.node;
			Node* pcur = cur->prve;
			Node* _next = cur->next;

			pcur->next = cur->next;
			cur->next->prve = pcur;
			delete cur;
			return iterator(_next);
		}

元素获得

获得尾元素

代码语言:javascript
复制
cpp		T& back()
		{
			return head->prve->data;
		}

获得首元素

代码语言:javascript
复制
cpp		T& front()
		{
			return head->next->data;
		}

是否为空

代码语言:javascript
复制
cpp		bool empty()
		{
			return head->next == head;
		}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-05-26c,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 成员变量
  • 构造函数
  • 拷贝构造
  • 析构函数
  • 迭代器
  • 任意位置插
  • 任意位置删
  • 元素获得
    • 获得尾元素
      • 获得首元素
      • 是否为空
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档