数据结构——单链表
文件LinkList.h
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
using namespace std;
typedef struct Node {
int data;
Node * next;
}Node;
//有头链表
class LinkList{
private:
Node * header_;
int count_;
public:
LinkList();
~LinkList();
bool IsEmpty();
bool IsExist(int value);
void Print();
void Reverse();
bool DeleteValue(int pos);
bool DeletePos(int pos);
bool Insert(int value);
bool Insert(int pos,int value);
void Destroy();
int GetLength(){
return count_;
}
};
LinkList::LinkList(){
header_ = new Node;
header_->data = 0;
header_->next = nullptr; //c++11 ,空指针不要用NULL
count_ = 0;
cout << "create the linklist" << endl;
}
LinkList::~LinkList(){
Destroy();
cout << "destory the linklist" << endl;
}
bool LinkList::IsEmpty(){
if(count_ == 0 || header_->next == nullptr)
{
cout << "The LinkList is empty!" << endl;
return true;
}else{
return false;
}
}
bool LinkList::IsExist(int value){
if(!IsEmpty())
{
Node * pcur = nullptr;
for(pcur = header_->next;pcur != nullptr;pcur = pcur->next)
{
if(pcur->data == value)
{
return true;
}
}
return false;
}
}
void LinkList::Print(){
if(!IsEmpty())
{
Node * pcur = nullptr;
int i = 1;
for(pcur = header_->next;pcur != nullptr;pcur = pcur->next,i++)
{
cout << i <<" : " << pcur->data << endl;
}
}
}
void LinkList::Reverse(){
if(!IsEmpty())
{
cout << "reverse the linklist"<< endl;
Node * pcur = header_->next;
Node * pnext= pcur->next;
Node * pnode = nullptr;
while(pcur != nullptr)
{
pnext = pcur->next;
pcur->next = pnode;
pnode = pcur;
pcur = pnext;
}
header_->next= pnode;
}
}
bool LinkList::DeleteValue(int value){
if(IsEmpty())
{
cout << "No element to delete" << endl;
return false;
}
Node * ptmp = header_;
Node * pcur;
cout << "delete element "<< value << endl;
for(pcur = header_->next;pcur->next->next != nullptr;pcur = pcur->next)
{
//cout << ptmp << endl;
if(pcur->data == value)
{
ptmp->next = pcur->next;
delete pcur;
pcur = ptmp;
count_--;
}
ptmp = ptmp->next;
}
if(pcur->next->data == value)
{
delete pcur->next;
pcur->next = nullptr;
count_--;
}
return true;
}
bool LinkList::DeletePos(int pos){
if(IsEmpty())
{
cout << "No element to delete" << endl;
return false;
}
cout << "delete element in pos "<< pos << endl;
if(pos > 0 && pos < count_)
{
Node * ptmp;
Node * pcur;
int post;
for(post = 1,pcur = header_->next;post < pos;pcur = pcur->next,post++)
{
ptmp = pcur;
}
ptmp->next = pcur->next;
delete pcur;
return true;
}else if(pos == count_){
Node * ptmp;
for(ptmp = header_->next;ptmp->next->next != nullptr;ptmp = ptmp->next)
{
;
}
delete ptmp->next;
ptmp->next = nullptr;
return true;
}else{
cout << "Error post" << endl;
return false;
}
}
bool LinkList::Insert(int value){
Node * pnode = new Node;
pnode->next = nullptr;
pnode->data = value;
Node * ptmp = header_;
cout << "inset element " << value << endl;
while(ptmp->next != nullptr)
{
ptmp = ptmp->next;
}
ptmp->next = pnode;
count_++;
return true;
}
bool LinkList::Insert(int pos,int value){
if(pos>0 && pos <= count_)
{
cout << "insert " << value << " in pos " << pos << endl;
Node * pnode = new Node;
pnode->next = nullptr;
pnode->data = value;
Node * ptmp = header_;
Node * pcur;
for(int post = 1;post != pos;ptmp = ptmp->next,post++)
{
}
pcur = ptmp->next;
ptmp->next = pnode;
pnode->next = pcur;
count_++;
return true;
}else{
cout << "Error post" << endl;
return false;
}
}
void LinkList::Destroy(){
Node * ptmp;
while(header_ != nullptr)
{
ptmp = header_->next;
delete header_;
header_ = ptmp;
}
}
#endif
测试文件main.cc
# include "SquenceList.h"
# include "LinkList.h"
using namespace std;
int main(int argc,char ** argv){
LinkList ll;
ll.IsEmpty();
ll.Insert(10);
ll.Insert(20);
ll.Insert(30);
ll.Insert(50);
ll.Insert(60);
ll.Insert(4,40);
ll.Print();
ll.DeleteValue(60);
ll.Print();
cout << "the linklist length is " << ll.GetLength() << endl;;
ll.Insert(3,70);
ll.Print();
ll.DeletePos(3);
ll.Print();
ll.Reverse();
ll.Print();
return 0;
}
输出结果:
[root@localhost DataStruct]# ./a.out
create the linklist
The LinkList is empty!
inset element 10
inset element 20
inset element 30
inset element 50
inset element 60
insert 40 in pos 4
1 : 10
2 : 20
3 : 30
4 : 40
5 : 50
6 : 60
delete element 60
1 : 10
2 : 20
3 : 30
4 : 40
5 : 50
the linklist length is 5
insert 70 in pos 3
1 : 10
2 : 20
3 : 70
4 : 30
5 : 40
6 : 50
delete element in pos 3
1 : 10
2 : 20
3 : 30
4 : 40
5 : 50
reverse the linklist
1 : 50
2 : 40
3 : 30
4 : 20
5 : 10
destory the linklist