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

【C++】单链表的实现

2015-08-25 20:48 363 查看
链表概念--链表是一种线性表,但是并不是顺序存储,而是每个节点里面存储着下一个节点的指针,把存储数据元素的数据串链起来。



单链表演示图:



#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: