?? 單鏈表.cpp
字號:
#include<iostream.h>
class listnode//結點描述
{
public:
int data;
listnode *next;
listnode(const int &itemval,listnode *ptrnext=NULL)//構造函數
{
data=itemval;
next=ptrnext;
}
listnode(listnode *ptrnext=NULL)//構造函數重載
{
next=ptrnext;
}
};
class linklist//建立鏈表類
{
public:
listnode *head;//存放頭結點
listnode *tail;//存放尾結點
listnode *currptr;//存放當前結點
public:
linklist(void);//構造函數:ok
void locate(int &itemval);//按值查找,將指針指向該結點:ok
void findorder(int i);//按序號查找,將指針指向該結點:ok
listnode* findprior(void);//找當前結點的直接前驅:ok
void inserttail(int &itemval);//尾插入:ok
void inserthead(int &itemval);//頭插入:ok
void insertafter(int &itemval);//在當前指針后插入:ok
void insertbefore(int &itemval);//在當前指針前插入:ok
int Delete(void);//刪除當前結點,currptr指向NULL:ok
int listsize(void);//求鏈表結點個數:ok
void clearlist(void);//清空鏈表:ok
int isempty(void);//判空:ok
void print(void);//:ok
};
linklist::linklist()
{
head=tail=currptr=NULL;
}
void linklist::locate(int &itemval)
{
listnode *p;
p=head;
while(p!=NULL&&p->data!=itemval)
{
p=p->next;
}
if(p==NULL)
cout<<"不存在此結點"<<endl;
else
{
cout<<"ok"<<endl;
currptr=p;
}
}
void linklist::findorder(int i)
{
listnode *p;
p=head;
int j;
for(j=0;j<i-1;j++)
{
if(p==NULL)
{
cout<<"不存在此結點"<<endl;
break;
}
p=p->next;
}
currptr=p;
}
listnode* linklist::findprior(void)
{
listnode *p;
p=head;
if(currptr==NULL)
{
cout<<"當前結點為空"<<endl;
return 0;
}
if(p==currptr)
{
cout<<"當前結點為頭結點"<<endl;
return p;
}
while(p->next!=currptr)
p=p->next;
return p;
}
void linklist::inserttail(int &itemval)
{
listnode *p;
p=new listnode(itemval,NULL);
if(head==NULL)
{
head=p;
}
else
tail->next=p;
currptr=tail=p;
}
void linklist::inserthead(int &itemval)
{
listnode *p;
p=new listnode(itemval,NULL);
if(head==NULL)
tail=p;
else
p->next=head;
head=currptr=p;
}
void linklist::insertafter(int &itemval)
{
listnode *p;
p=new listnode(itemval,NULL);
if(currptr==NULL)
inserthead(itemval);
else
if(currptr==tail)
{
tail=p;
}
else
{
p->next=currptr->next;
currptr->next=p;
currptr=p;
}
}
void linklist::insertbefore(int &itemval)
{
listnode *p;
p=new listnode(itemval,NULL);
if(currptr==NULL)
inserthead(itemval);
else
{
if(currptr==head)
{
p->next=head;
head=p;
}
else
{
listnode *q;
q=head;
while(q->next!=currptr)
q=q->next;
q->next=p;
p->next=currptr;
}
currptr=p;
}
}
int linklist::Delete(void)
{
listnode *p;
if(currptr==NULL)
return 0;
if(currptr==head&&currptr==tail)
currptr=head=tail=NULL;
else
if(currptr==head)
head=head->next;
else
{
p=head;
while(p->next!=currptr)
p=p->next;
if(currptr==tail)
{
tail=p;
p->next=NULL;
}
else
p->next=currptr->next;
}
delete currptr;
currptr=NULL;
return 1;
}
int linklist::listsize(void)
{
int n=0;
listnode *p;
p=head;
while(p!=NULL)
{
p=p->next;
n++;
}
return n;
}
void linklist::clearlist(void)
{
while(head!=NULL)
{
currptr=head;
head=head->next;
delete currptr;
}
head=tail=currptr=NULL;
}
int linklist::isempty(void)
{
if(head==tail)
return 1;
else return 0;
}
void linklist::print(void)
{
currptr=head;
while(currptr!=NULL)
{
cout<<currptr->data<<" ";
currptr=currptr->next;
}
}
void main()//測試
{
linklist a;
int i,j,k;
cin>>i>>j>>k;
a.inserthead(i);
a.inserthead(j);
a.inserttail(k);
a.insertbefore(i);
a.print();
cout<<endl;
a.locate(k);
a.findorder(1);
cout<<a.findprior()<<endl;
cout<<a.listsize();
cout<<endl;
a.Delete();
a.print();
cout<<endl;
cout<<a.head<<endl;
a.clearlist();
cout<<a.isempty();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -