栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

带头节点的单项链表最全功能实现以及其中迭代器实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带头节点的单项链表最全功能实现以及其中迭代器实现

#include
using namespace std;
namespace my {
	//定义单链表节点
	template
	struct ListNode {
		ListNode(const T& x = T())
			:data(x)
			, next(nullptr)
		{}
		T data;
		ListNode* next;
	};
	//封装迭代器
	template
	class ListNodeIterator {
	public:
		typedef ListNodeIterator Self;
		ListNodeIterator( ListNode* l) 
			:_node(l)
		{}

		T& operator*() {
			return _node->data;
		}

		T* operator->() {
			return &(operator*());
		}

		Self& operator++() {
			_node = _node->next;
			return *this;
		}

		Self& operator++(int) {
			Self& temp = this;
			_node = _node->next;
			return *temp;
		}

		bool operator==(const Self& s) {
			return _node == s._node;
		}

		bool operator!=(const Self& s) {
			return _node != s._node;
		}
	private:
		ListNode* _node;
	};

	//链表类
	template
	class List {
	public:
		typedef ListNodeIterator iterator;
	public:
		List() {
			_head = new ListNode;
			_size = 0;
		}
		iterator begin() {
			
			return iterator(_head->next);
		}
		iterator end() {
			return iterator(Get_the_most_last()->next);
		}
		void push_back(const T& val) {
			//使用尾插法
			ListNode* Last = Get_the_most_last();//得到链表最后一个元素
			ListNode* newnode = new ListNode(val);//定义新节点
			Last->next = newnode;//将新节点挂入链表之中
			_size++;//修改链表中元素个数
		}

		void Push_front(const T& val) {
			//使用头插法
			ListNode* newnode = new ListNode(val);//定义新节点
			if (_head->next == nullptr) {
				//链表为空的情况
				_head->next = newnode;
			}
			else {
				newnode->next = _head->next;
				_head->next = newnode;
			}
			_size++;
		}
		void pop_back() {
			//尾删
			ListNode* last = Get_the_most_last();//得到链表最后最后一个节点
			ListNode* cur = _head;
			while (cur->next != last) {
				//查找链表待删除节点的前一个节点
				cur = cur->next;
			}
			delete last;
			cur->next = nullptr;
			_size--;

		}
		void pop_front() {
			//头删
			ListNode* cur = _head->next;
			_head->next = cur->next;
			delete cur;
			cur = nullptr;
			_size--;
		}
		void pop_listbyval(const T& val) {
			//删除链表中值为val元素
			//待删除节点为首元节点
			if (_head->next->data == val) {
				pop_front();
				return;
			}
			//链表为空,直接退出
			if (_head->next == nullptr)
				return;

			//查找待删除节点位置
			ListNode* cur = _head->next;
			while (cur->next && cur->next->data != val) {
				cur = cur->next;
			}
			//删除
			ListNode* del = cur->next;
			cur->next = cur->next->next;
			delete del;
			_size--;
		}
		void Show_List() {
			//打印链表
			ListNode* cur = _head->next;
			while (cur) {
				cout << cur->data << " ";
				cur = cur->next;
			}
		}
		size_t size() {
			//链表中插入元素个数
			return _size;
		}
		size_t find_val(const T& val) {
			//按值查找,找到返回所在位置
			ListNode* cur = _head->next;
			size_t pos = 1;
			while (cur) {
				if (cur->data == val)
					return pos;
				pos++;
				cur = cur->next;
			}
			return -1;
		}
		void modificy_list(const T& desc, const T& data) {
			//修改链表中元素
			ListNode* cur = _head->next;
			while (cur) {
				if (cur->data == desc)
					cur->data = data;
				cur = cur->next;
			}
		}
		void List_reverse() {
			//逆置链表
			ListNode* cur = _head->next;
			ListNode* next = _head->next->next;

			cur->next = nullptr;
			while (next) {
				cur = next;
				next = next->next;
				
				cur->next = _head->next;
				_head->next = cur;
			}
		}
		~List()
		{
			clear();
		}
	private:
		ListNode* _head;
		size_t _size;

	private:
		ListNode* Get_the_most_last() {
			//返回链表中最后一个元素
			ListNode* cur = _head;
			while (cur->next) {
				cur = cur->next;
			}
			return cur;
		}
		void clear() {
			//回收链表
			ListNode* cur = _head->next;
			while (cur) {
				_head->next = cur->next;
				delete cur;
				cur = _head->next;
			}
			delete _head;
			_head = nullptr;
		}
	};
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/629807.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号