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


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

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

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

		}
		void pop_front() {
			//头删
			ListNode<T>* 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<T>* cur = _head->next;
			while (cur->next && cur->next->data != val) {
				cur = cur->next;
			}
			//删除
			ListNode<T>* del = cur->next;
			cur->next = cur->next->next;
			delete del;
			_size--;
		}
		void Show_List() {
			//打印链表
			ListNode<T>* cur = _head->next;
			while (cur) {
				cout << cur->data << " ";
				cur = cur->next;
			}
		}
		size_t size() {
			//链表中插入元素个数
			return _size;
		}
		size_t find_val(const T& val) {
			//按值查找,找到返回所在位置
			ListNode<T>* 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<T>* cur = _head->next;
			while (cur) {
				if (cur->data == desc)
					cur->data = data;
				cur = cur->next;
			}
		}
		void List_reverse() {
			//逆置链表
			ListNode<T>* cur = _head->next;
			ListNode<T>* 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<T>* _head;
		size_t _size;

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小明学编程~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值