【C++】单链表的基本操作

本文介绍了如何使用C++实现单链表的各种基本操作,包括构造、析构、复制、交换、插入、删除、反转、查找、打印等。通过示例代码详细展示了链表的增删改查和反转功能。
摘要由CSDN通过智能技术生成
//头尾相连的单链表
#include<iostream>
using namespace std;
typedef int DataType;
struct LinkNode
{
	DataType _data;
	LinkNode *_next;
	LinkNode(const DataType &x)
		:_data(x)
		, _next(NULL)

	{}
};
class sList
{
public:
	sList()
		:_head(NULL)
		,_tail(NULL)
	{}
	~sList()
	{
		Destory();
	}
	sList(const sList &s) :_head(NULL), _tail(NULL)
	{
		LinkNode *begin = s._head;
		do
		{
			this->PushBack(begin->_data);
			begin = begin->_next;
		} while (begin != s._head);
	}
	//优化
	void Swap(sList &s)
	{
		swap(_head, s._head);
		swap(_tail, s._tail);
	}
	sList &operator=(const sList &s)
	{
		if (this != &s)
		{
			this->Destory();
			/*LinkNode *begin = s._head;
			do
			{
				this->PushBack(begin->_data);
				begin = begin->_next;
			} while (begin != s._head);*/
			sList tmp(s);
			Swap(tmp);
		}
		return *this;
	}
	void PushBack(const  DataType &x)
	{
		//1.没有节点(空链表)
		if (_head == NULL)
		{
			_head = new LinkNode(x);
			_tail = _head;
			_tail->_next = _head;
			return;
		}
		_tail->_next = new LinkNode(x);
		_tail = _tail->_next;
		_tail->_next = _head;

	}
	void PopBack()
	{
		if (_head == NULL)
		{
			return;
		}
		if (_head == _tail)
		{
			delete _head;
			_head = NULL;
			_tail = NULL;
			return;
		}
		LinkNode *begin = _head;
		while (begin->_next != _tail)
		{
			begin=begin->_next;
		}
		begin->_next = _head;
		delete _tail;
		_tail = begin;
	}
	void PushFront(const DataType &x)
	{
		if (_head == NULL)
		{
			_head = new LinkNode(x);
			_tail = _head;
			_tail->_next = _head;
			return;
		}
		_tail->_next = new LinkNode(x);
		_tail->_next->_next = _head;
		_head=_tail->_next;
	}
	void PopFront()
	{
		if (_head == NULL)
		{
			return;
		}
		if (_head == _tail)
		{
			delete _head;
			_head = NULL;
			_tail = NULL;
			return;
		}
		_tail->_next = _head->_next;
		delete _head;
		_head = _tail->_next;
		
	}
	LinkNode *Find(const DataType &x)
	{
		LinkNode *begin = _head;
		//1.空链表
		if (_head == NULL)
		{
			return NULL;
		}
		do
		{
			if (begin->_data == x)
			{
				return begin;
			}
			begin = begin->_next;
		} while (begin != _head);
		return NULL; //没找到该节点
	}
	//插到n后
	void Insert(LinkNode *n,const DataType &x)
	{
		//1.空链表
		if (_head == NULL)
		{
			_head = new LinkNode(x);
			_tail = _head;
			_tail->_next = _head;
		}
		//2.尾结点后,更新尾
		else if (_tail == n)
		{
			_tail->_next = new LinkNode(x);
			_tail = _tail->_next;
			_tail->_next = _head;
		}
		//3.插入到链表间
		else
		{
			LinkNode *tmp = n->_next;
			n->_next = new LinkNode(x);
			n->_next->_next = tmp;
		}
	}
	//删除n结点
	void Remove(LinkNode *n)
	{
		//1.空链表
		if (_head == NULL)
		{
			return;
		}
		//2.删除的是头结点
		else if (_head == n)
		{
			PopFront();
		}
		else
		{
			LinkNode *prev = _head;
			while (prev->_next != n)//删除的是中间任意一个结点
			{
				prev = prev->_next;
			}
			prev->_next = n->_next;
			if (n == _tail) 
			{
				_tail = prev;//删除的是尾结点,更新尾
			}
			delete n;
		}
	}
	//链表逆序
	void Reverse()
	{
		if (_head == NULL || _head == _tail)
		{
			return;
		}
		LinkNode *begin = _head->_next;
		LinkNode *NewHead = _head;
		LinkNode *NewTail = _head;
		NewHead->_next = NULL;
		while (begin!=_head)
		{
			LinkNode *tmp = begin;
			begin = begin->_next;
			tmp->_next = NewHead;
			NewHead = tmp;
		}
		_head = NewHead;
		_tail= NewTail;
		_tail->_next = _head;
	}
	void Destory()
	{
		while (_head)
		{
			PopBack();
		}
	}
	void Print()
	{
		if (_head == NULL)
		{
			return;
		}
		
		LinkNode *begin = _head;
		while (begin != _tail)
		{
			cout << begin->_data<<" ";
			begin = begin->_next;
		}
		cout << begin->_data << endl;;
	}
private:
	LinkNode *_head;
	LinkNode *_tail;
};
int main()
{
	sList op1;
	op1.PushBack(1);
	op1.PushBack(2);
	op1.PushBack(3);
	op1.PushBack(4);
	op1.PushBack(5);
	op1.Print();
	op1.Reverse();
	op1.Print();
	/*op1.PopBack();
	op1.PopBack();
	op1.PopBack();*/
	//op1.PopBack();
	//op1.PopBack();
	/*op1.PushFront(100);
	op1.PushFront(200);
	op1.PushFront(300);
	op1.PushFront(400);
	op1.Print();
	op1.PopFront();
	op1.PopFront();
	op1.PopFront();
	op1.PopFront();
	op1.Print();*/
	/*LinkNode *tmp =op1. Find(5);
	cout << "tmp->_data=" << tmp << endl;
	op1.Remove(tmp);
	op1.Print();*/
	//op1.Destory();
	/*op1.Print();
	sList op2 = op1;
	cout << "op2= " << endl;
	op2.Print();
	sList op3;
	op3 = op2;
	cout << "op3=";
	op3.Print();*/
	/*op1.Insert(tmp,6);
	op1.Print();*/
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值