【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()
	{}
	
	SList(const SList& s)
		:_head(NULL)
	    ,_tail(NULL)
	{
		if(s._head == NULL)
			return ;


		LinkNode* begin = s._head;
		do
		{
			this->PushBack(begin->_data);
			begin = begin->_next ;
		}while(begin != s._head);
	}

	SList&operator=(const SList& s)
	{
		if(this != &s)
		{
			Destory();
			LinkNode* begin = s._head;
			do
			{
				this->PushBack(begin->_data);
				begin = begin->_next ;
			}while(begin != s._head);
        }
}
	void Destory()
	{
		while(_head)
		{
			PopFront();
		}
	}
public:
	void Print()//打印
	{
		if(_head == NULL)
		{
			cout<<"链表为空"<<endl;
		}
		else
		{
			LinkNode *begin = _head;
			while(begin != _tail)
			{
				cout<<begin->_data<<"->";
				begin = begin->_next;
			}
			cout<<begin->_data<<endl;
			cout<<endl;
		}
	}
	void PushBack( const DataType& x)//尾插  
	{
		// 1.空链表
		// 2.有一个或一个以上节点
		if(_head == NULL)
		{
			_head = new LinkNode(x);
			
			_tail = _head;
			_tail->_next = _head;
		}
		else
		{
			_tail->_next =  new LinkNode(x);
			_tail = _tail->_next;
			_tail->_next = _head;
		}
	}
	void PopBack()//尾删
	{
		//没有节点
		//一个节点
		//两个以上节点
		if (_head == NULL)
		{
			cout<<"链表为空"<<endl;
			
		}
		else if(_head == _tail)
		{
			delete _head;
			_head = NULL;
			_tail = NULL;
		}
		else
		{
			LinkNode* tmp = _head;
			while(tmp->_next!=_tail)
			{
				tmp = tmp->_next;
			}
            delete _tail;
			_tail = tmp;
			_tail->_next = _head;
			
		}
	}
	void PopFront()//头删
	{
		//没有节点
		//一个节点
		//多个节点
		if(_head == NULL)
		{
			cout<<"链表为空"<<endl;
		}
		else if(_head == _tail)
		{
			delete _head;
			_head = NULL;
			_tail = NULL;
		}
		else
		{
			LinkNode* tmp = _head;
			_head = _head->_next;
			_tail->_next = _head;
			delete tmp;
		
		}
	}
	void PushFront(const DataType& x)//头插
	{
		if(_head == NULL)
		{
			_head = new LinkNode(x);
			
			_tail = _head;
			_tail->_next = _head;
		}
		else
		{
			LinkNode* tmp = _head;
			//delete _head;
			_head = new LinkNode(x);
			_head->_next = tmp;
			_tail->_next = _head;
			
		}
	}
LinkNode *Find (DataType x)
{
	if(_head == NULL)
	{
		cout<<"链表为空"<<endl;
		return NULL;
	}
	LinkNode* begin = _head;
	do
	{
		if(begin->_data ==x)
		{
			return begin;
		}
		else
		{
			begin = begin->_next ;
		}
	}while(begin!=_tail->_next );
	return NULL; 
}

bool Remove(LinkNode* n)
{
	//_head为空
	//n为空
	//只有一个节点
	//两个及以上几点
	if(_head == NULL)
	{
		cout<<"链表为空,不能删除"<<endl;
		return false;
	}
	else if(n == NULL)
	{
		cout<<"删除节点为空,请重新输入"<<endl;
		return false;
	}
	else if(_tail == _head)  //只有一个节点
	{
		if(n == _head)
		{
			delete _head;
			_head = NULL;
			_tail = NULL;
			return true;
		}
		return false;
	}
	else //2个及以上节点
	{
		LinkNode* begin = _head;
		while(begin->_next != n)
		{
			begin = begin->_next ;
			if(begin == _head)//没有找到该节点
			{
				return false;
			}
		}
		if(n == _head)
		{
			_head = _head->_next;
			_tail->_next = _head;
			
		}
		else if(n == _tail)
		{
			_tail = begin;
			_tail->_next = _head;
			
		}
		else
		{
			begin->_next = n->_next ;
			
		}
		delete n;
		return true;
	}

}
void Insert(LinkNode* n, DataType x)
{
	//链表为空
	//节点找不到
	//只有一个节点
	//头插
	//尾插
	//中间插
	if(_head == NULL)
	{
		cout<<"链表为空,正在为您创建新链表"<<endl;
		_head = new LinkNode(x);
		_tail = _head;
		_tail->_next = _head;
	}
	else if(n == NULL)
	{
		cout<<"找不到该节点,请重新输入节点插入"<<endl;
		return; 
	}
	else if(_head == _tail)
	{
		if(n == _head || n == _tail)
		{
			LinkNode *tmp = _head;
			_head = new  LinkNode(x);
			_head->_next = tmp;
			_tail = tmp;
			_tail->_next = _head;
		}
		else
		{
			cout<<"找不到该节点,请重新输入节点插入"<<endl;
			return; 
		}
	}
	else//两个以上节点
	{
		LinkNode* begin = _head;
		while(begin->_next != n)
		{
			begin = begin->_next ;
			if(begin == _head)//没有找到该节点
			{
				cout<<"找不到该节点,请重新输入节点插入"<<endl;
				return ;
			}
		}
		if(n == _head)
		{
			LinkNode *tmp = _head;
			_head = new LinkNode(x);
			_head->_next = tmp;
			_tail->_next = _head;
		}
		else if(n == _tail)
		{
			LinkNode *tmp = _tail;
			_tail = new LinkNode(x);
			tmp->_next = _tail;
			_tail->_next = _head;
		}
		else
		{
			 LinkNode *tmp  = new LinkNode(x);
			 begin->_next = tmp;
			 tmp->_next = n;
		}

		
	}
}
void Erase(DataType x)//删除指定元素
{
	LinkNode *begin = _head;
	LinkNode *tmp = begin;
	while(begin->_data != x)
	{
		tmp = begin;
		begin = begin->_next ;
		if(begin == _head)
		{
			cout<<"没有此元素,无法删除"<<endl;
		}
	}
	if(begin == _head)
	{
		_head = _head->_next;
		_tail->_next = _head;
	}
	else if(begin == _tail)
	{
		_tail = tmp;
		_tail->_next = _head;
	}
	else
	{
		tmp->_next = begin->_next ;	
	}
    //delete tmp;
	//delete begin;

}
void Reverse()
{
	_tail = _head;
	LinkNode* newhead = _head;
	LinkNode* begin = _head->_next ;
	while(begin)
	{
		LinkNode* tmp = begin;
		tmp ->_next = newhead;
		newhead = tmp;
		begin = begin->_next ;
	}
    _head = newhead;
}


private:
	LinkNode* _head;	// 指向链表头的指针
	LinkNode* _tail;	// 指向链表尾的指针
};

void Test1()
{
	SList s1;
	s1.PushFront(1);
	s1.PushFront(2);
	s1.PushFront(3);
	s1.PushFront(4);
	s1.Print();
	SList s2(s1);
	s2.Print ();
	s2 = s1;
	s2.Print ();
	//s1.Reverse ();
	//s1.Print();
	//s1.Erase(4);
	//s1.Print();
	/*LinkNode* ret1 = s1.Find(2); 
	s1.Insert(ret1,5);
    s1.Print();*/
	//s1.PopFront();
	
	//s1.Print();
	//s1.PopBack();
	//s1.Print();
	/*s1.PushFront(5);
	s1.PushFront(6);
	s1.PushFront(7);
	s1.PushFront(8);
	s1.Print();
	s1.PopFront();
	s1.PopFront();
	s1.PopFront();
	s1.PopFront();
	s1.PopFront();
	s1.PopFront();
	s1.PopFront();
	s1.PopFront();
	s1.Print();*/
   //LinkNode* ret = s1.Find(1); 
   //cout<<ret->_data<<endl;
	/*
	s1.Remove(ret);
    s1.Print();

	LinkNode* ret1 = s1.Find(2); 
	s1.Remove(ret1);
    s1.Print();
	
	LinkNode* ret2 = s1.Find(3); 
	s1.Remove(ret2);
    s1.Print();
	
	LinkNode* ret3 = s1.Find(4); 
	s1.Remove(ret3);
    s1.Print();*/
}

int main()
{
	Test1();
	getchar();
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值