//头尾相连的单链表
#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.没有节点(空链表
【C++】单链表的基本操作
最新推荐文章于 2023-01-03 20:06:47 发布