您的位置:首页 > 编程语言 > C语言/C++

c++实现单链表

2015-08-26 12:07 381 查看
#include<iostream>
#include<assert.h>
using namespace std;
typedef int DataType;

struct LinkNode   //创建一个节点
{
DataType _data;
LinkNode* _next;

LinkNode(const DataType &x) :_data(x), _next(NULL)
{}
};
class SList
{
public:
SList(LinkNode* head = NULL, LinkNode* tail = NULL)
:_Head(head), _Tail(tail)
{}
void Swap( SList &s)
{
swap(_Head, s._Head);
swap(_Tail, s._Tail);
}
SList(const SList &s)
:_Head(NULL)
, _Tail(NULL)
{
if (s._Head == NULL)
{
return;
}
LinkNode* begin = s._Head;
do{
PushBack(begin->_data);
begin = begin->_next;
} while (begin != s._Head);
}
~SList()
{
while (_Head)
{
if (_Head == _Tail)
{
delete _Head;
_Head = NULL;
_Tail = NULL;
}
else
{
LinkNode *del = _Head;
_Head = _Head->_next;
delete del;
_Tail->_next = _Head;
}
}
}
SList operator= ( SList& s)
{
SList tmp(s);
Swap(tmp);
return *this;
}
public:
void Destory()
{
if (_Head == NULL)
{
return;
}
//先不管_Head和_Tail等全部删完将_Head和_Tail置为NUL
LinkNode* begin = _Head;
do{
LinkNode* del = begin;
begin = begin->_next;
delete del;
} while (begin != _Head);
_Head = NULL;
_Tail = NULL;
}
void Print()
{
//1.没有节点
//2.多个节点
if (_Head == NULL)
{
return;
}
LinkNode *begin = _Head;
do{
cout << begin->_data<< "->";
begin = begin->_next;
} while (begin != _Head);
cout<< endl;
}
void PushBack(const DataType& x)//尾插
{//1.没有节点
//2.有多个节点
if (_Head == NULL)
{
_Head = new LinkNode(x);
_Tail = _Head;
_Tail->_next = _Head;
}
else
{
LinkNode *tmp = new LinkNode(x);
_Tail->_next = tmp;
tmp->_next = _Head;
_Tail = tmp;
}
}
void PopBack()//尾删
{
if (_Head == NULL) //没有节点
{
cout << "SList is empty" << endl;
return;
}
else if (_Head == _Tail) //有一个节点
{
delete _Head;
_Head = NULL;
_Tail = NULL;
}
else    //有多个节点
{
LinkNode *prev = _Head;
while (prev->_next != _Tail)
{
prev = prev->_next;
}
prev->_next = _Head;
delete _Tail;
_Tail = prev;
}
}

void PopFront()//头删
{
if (_Head == NULL) //没有节点
{
cout << "SList is empty" << endl;
return;
}
else if (_Head == _Tail) //有一个节点
{
delete _Head;
_Head = NULL;
_Tail = NULL;
}
else
{
LinkNode* del = _Head;
_Head = _Head->_next;
delete del;
_Tail->_next = _Head;
}
}

void PushFront(DataType x)//头插
{
if (_Head == NULL)  //没有节点
{
_Head = new LinkNode(x);
_Tail = _Head;
_Tail->_next = _Head;
}
else    //有多个节点
{
LinkNode* tmp = new LinkNode(x);
tmp->_next = _Head;
_Head = tmp;
_Tail->_next = _Head;
}
}

LinkNode* Find(DataType x)
{
if (_Head == NULL) //没有节点
{
cout << "SList is empty" << endl;
return NULL;
}
LinkNode* begin = _Head;
do{
if (begin->_data == x)
{
cout << "find " << x << endl;
return begin;
}
begin = begin->_next;
} while (begin != _Head);
cout << "not find " << x << endl;
return NULL;
}

bool Remove(LinkNode * n)
{
assert(n);
if (_Head == NULL) //没有节点
{
cout << "SList is empty" << endl;
return false;
}
if (n == _Head)  //n是头指针
{
_Head = _Head->_next;
delete n;
_Tail->_next = _Head;
return true;
}

LinkNode* prev = _Head;
do{
if (prev->_next == n)  //n是中间指针
{
prev->_next = n->_next;
delete n;
if (n == _Tail)    //n是尾指针
{
_Tail = prev;
_Tail->_next = _Head;
}
return true;
}
prev = prev->_next;

} while (prev != _Head);
return false;
}
void Insert(LinkNode * n, DataType x) //先找到节点n,在n后面插一个节点
{
assert(n);
if (_Head == NULL) //没有节点
{
cout << "SList is empty" << endl;
return ;
}
LinkNode* begin = _Head;
do{
if (begin == n)
{
LinkNode* tmp = new LinkNode(x);
tmp->_next = n->_next;
n->_next = tmp;
if (n == _Tail)
{
_Tail = tmp;
_Tail->_next = _Head;
}
return;
}
begin = begin->_next;
} while (begin != _Head);
cout << "not find " << n << endl;
}
void Reverse()  //翻转
{
if (_Head == NULL) //没有节点
{
cout << "SList is empty" << endl;
return;
}
if (_Head == _Tail)
{
cout << "只有一个节点" << endl;
return;
}
LinkNode* tmp = _Head;
LinkNode* newhead = tmp, *newtail = tmp;
do{
LinkNode* prev = tmp;
tmp = tmp->_next;
prev->_next = newhead;
newhead = prev;
} while (tmp != _Head);
newtail->_next = newhead;
_Head = newhead;
}
private:
LinkNode* _Head;
LinkNode* _Tail;
};

void test()
{
SList s1;
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.Print();
SList s2 = s1;
/*s2.PushBack(4);
s2.PushBack(5);
s2.PushBack(6);*/
s2.Print();
/*s1 = s2;
s1.Print();*/
//s1.PopBack();
//s1.PopBack();
//s1.PopBack();
//s1.PopBack(); //没有节点的时候删除
//s1.Print();
}

void test2()
{
SList s1;
s1.PushFront(1);
s1.PushFront(2);
s1.PushFront(3);
s1.Print();
/*s1.Reverse();
s1.Print();*/
LinkNode* node = s1.Find(1);
s1.Insert(node, 4);
/*s1.Remove(node);*/
s1.Print();
/*s1.PopFront();
s1.PopFront();
s1.PopFront();
s1.PopFront();
s1.Find(2);
s1.Print();*/

}

int main()
{
test();
getchar();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: