您的位置:首页 > 其它

简单实现单链表

2016-01-12 20:53 417 查看
#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()
:_head(NULL)
,_tail(NULL)
{}

~SList()
{
Destory();
}

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);
}

void Swap(SList& s)
{
swap(_head, s._head);
swap(_tail, s._tail);
}

SList&operator=(const SList& s)
{
if (&s != this)
{
SList tmp(s);
Swap(tmp);
}

return *this;
}

void Destory()
{
if (_head == NULL)
return;

LinkNode* begin = _head;
do{
LinkNode* del = begin;
begin = begin->_next;
delete del;
}while(begin != _head);

_head = NULL;
_tail = NULL;
}

public:
void Print()
{
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
{
_tail->_next = new LinkNode(x);
_tail = _tail->_next;
_tail->_next = _head;
}
}

void PopBack()
{
//1.没有节点
//2.一个节点
//3.两个或两个以上的节点
if(_head == NULL)
{
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 PushFront(const DataType& x)
{
// 1.链表为空
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;
}
}

void PopFront()
{
// 1.没有节点
if(_head == NULL)
{
return;
}
else if(_head == _tail)
{
delete _head;
_head = NULL;
_tail = NULL;
}
else
{

LinkNode* del = _head;
_head = _head->_next;
delete del;
_tail->_next = _head;
}
}

LinkNode* Find (DataType x)
{
LinkNode* begin = _head;
while(begin)
{
if (begin->_data == x)
{
return begin;
}

begin = begin->_next;

if (begin == _head)
{
break;
}
}

return NULL;
}

bool Remove (LinkNode * n)
{
assert(n);

// 1.链表为空
if (_head == NULL)
{
return false;
}

// 2.只有一个节点
if(_head == _tail)
{
if (n == _head)
{
delete _head;
_head = NULL;
_tail = NULL;
return true;
}

return false;
}

// 3.两个以上的节点
LinkNode* prev = _head;
while (prev->_next != n)
{
prev = prev->_next;

// 如果没有找到n,则返回false。
if (prev == _head)
{
return false;
}
}

if (n == _head)
{
_head = _head->_next;
delete n;
_tail->_next = _head;
}
else if (n == _tail)
{
prev->_next = _head;
_tail = prev;
delete n;
}
else
{
prev->_next = n->_next;
delete n;
}

return true;
}

void Reverse()
{
if (_head == NULL)
return;

LinkNode* newHead = NULL, *newTail = _head;
LinkNode* begin = _head;
while (1)
{
LinkNode* tmp = begin;
begin = begin->_next;
tmp->_next = newHead;
newHead = tmp;

if (begin == _head)
{
break;
}
}

_head = newHead;
_tail = newTail;
_tail->_next = _head;
}

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;

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

void Test1()
{
SList s1;
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.Print();

s1.PopBack();
s1.PopBack();
s1.PopBack();
s1.PopBack();
s1.PopBack();
s1.Print();
}

void Test2()
{
SList s1;
s1.PushFront(1);
s1.PushFront(2);
s1.PushFront(3);
s1.PushFront(4);
s1.Print();

s1.PopFront();
s1.PopFront();
s1.PopFront();
s1.PopFront();
s1.PopFront();
s1.Print();
}

void Test3()
{
SList s1;
s1.PushBack(1);
//s1.PushBack(2);
//s1.PushBack(3);
//s1.PushBack(4);
s1.Print();

LinkNode* ret = s1.Find(1);
cout<<"ret:"<<ret->_data<<endl;

ret = s1.Find(5);
cout<<"ret:"<<ret<<endl;
}

void Test4()
{
SList s1;
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.Print();

SList s2;
s2 = s1;
s2.Print();

s2.Reverse();
s2.Print();
}

int main()
{
//Test1();
//Test2();
//Test3();

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