C++实现单链表
2016-06-03 13:15
423 查看
C++和C语言最大的不同就是类和模板。利用C++实现单链表当然少不了类。首先利用结构体定义好结点。
本文出自 “七月朔风” 博客,请务必保留此出处http://luminous.blog.51cto.com/10797288/1752180
如上所示,将函数在类内声明,实现写在类外,和C语言的写法大致相同,不在详述。void sList::PushBack(const DataType& d)
{
Node* newNode=new Node(d);
if(_head==NULL)
{
_head=newNode;
_tail=_head;
}
else
{
_tail->_next=newNode;
_tail=newNode;
}
}
void sList::PushFront(const DataType& d)
{
Node* newNode=new Node(d);
if(_head==NULL)
{
_head=newNode;
_tail=_head;
}
else
{
newNode->_next=_head;
_head=newNode;
}
}
void sList::PopBack()
{
if(_head==NULL)
{
return;
}
else if(_head==_tail)
{
delete _head;
_head=NULL;
_tail=NULL;
return;
}
else
{
Node* cur=_head;
while(cur->_next!=_tail)
{
cur=cur->_next;
}
delete _tail;
_tail=cur;
cur->_next=NULL;
}
}
void sList::PopFront()
{
if(_head==NULL)
{
return;
}
else
{
Node* del=_head;
_head=_head->_next;
delete del;
del=NULL;
}
}
Node* sList::Find(const DataType& d)
{
Node* cur=_head;
while(cur!=NULL)
{
if(cur->_data==d)
{
return cur;
}
cur=cur->_next;
}
return NULL;
}
void sList::Insert(Node* pos,const DataType& d)
{
if(pos==NULL)
{
return;
}
Node* newNode=new Node(d);
if(pos==_tail)
{
_tail->_next=newNode;
_tail=newNode;
}
else
{
newNode->_next=pos->_next;
pos->_next=newNode;
}
}
void sList::Reverse()
{
if((_head==NULL) || (_head==_tail))
{
return;
}
Node* cur=_head;//therr point
Node* prev=NULL;
Node* newHead=NULL;
while(cur)
{
prev=cur;
cur=cur->_next;
prev->_next=newHead;
newHead=prev;
_head=newHead;
}
}
void sList::Sort()
{
Node* cur=_head;
Node* end=NULL;
while(cur!=end)
{
while(cur && cur->_next!=end)
{
if(cur->_data < cur->_next->_data)
{
DataType tmp=cur->_data;
cur->_data=cur->_next->_data;
cur->_next->_data=tmp;
}
cur=cur->_next;
}
end=cur;
cur=_head;
}
}
void sList::Remove(const DataType& d)
{
Node* cur=_head;
Node* del=NULL;
Node* prev=NULL;
while(cur!=NULL)
{
if(cur->_data==d)
{
del=cur;
if(cur==_head)
{
_head=_head->_next;
}
else
{
prev->_next=cur->_next;
}
delete del;
break;
}
prev=cur;
cur=cur->_next;
}
}
void sList::RemoveAll(const DataType& d)
{
Node* cur=_head;
Node* del=NULL;
Node* prev=NULL;
while(cur!=NULL)
{
if(cur->_data==d)
{
del=cur;
if(cur==_head)
{
_head=_head->_next;
_head=cur;
}
else
{
prev->_next=cur->_next;
cur=prev->_next;
}
delete del;
}
else
{
prev=cur;
cur=cur->_next;
}
}
}
void sList::Erase(Node* pos)
{
if(pos==NULL)
{
return;
}
Node* del=NULL;
Node* cur=_head;
Node* prev=NULL;
while(cur!=NULL)
{
if(pos==cur)
{
del=cur;
{
if(pos==_head)
{
_head=_head->_next;
}
else
{
prev->_next=cur->_next;
}
delete del;
break;
}
}
prev=cur;
cur=cur->_next;
}
}
Node* sList::FindMidNode()
{
Node* Slow=_head;
Node* Fast=_head;
if(Fast==NULL || Fast->_next==NULL)
{
return NULL;
}
else
{
while(Fast && Fast->_next)
{
Slow=Slow->_next;
Fast=Fast->_next->_next;
}
return Slow;
}
}
Node* sList::CheckCircle()
{
Node* Slow=_head;
Node* Fast=_head;
while(Fast && Fast->_next)
{
Slow=Slow->_next;
Fast=Fast->_next->_next;
if(Fast==Slow)
{
return Slow;
}
}
return NULL;
}
int GetCircleLength(Node* meet)
{
Node* Start=meet;
int count=0;
do
{
count++;
Start=Start->_next;
}
while(Start!=meet);
return count;
}
Node* sList::GetCircleEntryNode(Node* meet)
{
Node* entry=_head;
while(entry!=meet)
{
entry=entry->_next;
meet=meet->_next;
}
return entry;
}
本文出自 “七月朔风” 博客,请务必保留此出处http://luminous.blog.51cto.com/10797288/1752180
相关文章推荐
- 利用C++类实现顺序表
- 利用C++日期类实现简单的日期计算器
- C++入门小程序练习
- c语言通用平台des加密算法
- iOS复习OC语言 NSString与NSArray 使用
- 简单总结C语言中的运算符优先级
- C语言运算符及其优先级汇总表口诀
- C语言运算符优先级列表(超详细)
- C++ STL 基础及应用(5) 字符串
- C语言的指针(进阶篇章之二)
- 非托管C++通过C++/CLI包装调用C# DLL
- C++面试笔记_1
- c语言 sqlite3数据库模块
- C++ 类的静态成员详细讲解
- 【C++错误检查】binary '>>' :
- c++ 修改内存
- c++ int to byte
- windows下C语言多线程编程
- vs添加lib等依赖项
- C++的强制类型转换