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

c++链表基本操作

2012-10-12 23:04 471 查看
发布时间:2012
年 10 月 12


目的:加强链表操作的熟练度

心的:某些函数实现的确不容易实现,但经过不断地思考,探索其中的规律,终究会实现。要知道欲速则不达!

 

#include<iostream>

#include<string>

#define NULL 0

using namespace std;

 

//节点数据类型

struct student


{int num;

string name;

float score;

student *next;

};

 

//链表操作封装类

class Handle_Linklist{

public:

       Handle_Linklist(){

              total=0;

       }

       student* Exchange(int signal_num,student *head);


       voidprint(student *head);

   student * creat();

   //student * insert(student *head,student *stud);

   //student * del(student*head,long num);

       //boolDisplay_LengthInfor();

   student * last_with_first_exchange(student *head);

private:

       inttotal;       //结点个数

};

 

 

//主函数

int main()

{Handle_Linklist handle;

student *head;

int num;

student *stu;

long del_num;

bool flag=false;

 

//创建链表

cout<<"input records:";

head=handle.creat();

handle.print(head);

 

 

cout<<"请逆置链表:";

head=handle.last_with_first_exchange(head);

handle.print(head);

 

 

cout<<"请交换链表前后节点的位置:";

int copy_num=head->num;

while(flag!=true){

       cin>>num;

       head=handle.Exchange(num,head);

   if(head->num!=copy_num){

              flag=true;

       }

}

handle.print(head);

 

 

cout<<endl<<"input thedeleted number:";

cin>>del_num;

while((del_num!=0)&&(handle.Display_LengthInfor()==flag))

       {head=handle.del(head,del_num);

       handle.print(head);

       cout<<"inputthe delated number:";

       cin>>del_num;

       }

 

cout<<endl<<"input theinserted record:";

stu=new student;


cin>>stu->num>>stu->name>>stu->score;

while(stu->num!=0)

       {head=handle.insert(head,stu);

       handle.print(head);

       cout<<endl<<"inputthe inserted record:";

       stu=newstudent;

       cin>>stu->num>>stu->name>>stu->score;

       }

return 0;

}

 

 

//创建链表

student * Handle_Linklist::creat()

{student *p1,*p2,*head;

p1=p2=new student;

cin>>p1->num>>p1->name>>p1->score;

head=NULL;

while(p1->num!=0)

{total++;

if(total==1)head=p1;

else p2->next=p1;

p2=p1;

p1=new student;

cin>>p1->num>>p1->name>>p1->score;

}

p2->next=NULL;

return head;

}

 

//遍历链表

void Handle_Linklist::print(student *head)

{student *p;

p=head;

cout<<"now,these"<<total<<"recordsare:"<<endl;

if(head!=NULL)

       do

       {cout<<p->num<<"  "<<p->name<<"  "<<p->score<<endl;

       p=p->next;

       }while(p!=NULL);

}

 

//插入结点操作

/*

student * Handle_Linklist::insert(student*head,student *stud)

{student *p0,*p1,*p2;

p1=head;

p0=stud;

if(head==NULL)

{head=p0;p0->next=NULL;}

else

{while((p0->num>p1->num)&&(p1->next!=NULL))

       {p2=p1;

       p1=p1->next;}

if(p0->num<=p1->num)

       {if(p1==head)head=p0;

       elsep2->next=p0;

       p0->next=p1;}

else

       {p1->next=p0;

       p0->next=NULL;}

}

       total++;

       return(head);

}

*/

 

//删除结点操作

/*

student * Handle_Linklist::del(student*head,long num)

{student *p1,*p2;

if(head==NULL)

       {cout<<"listnull!"<<endl;return (head);

       }

p1=head;

while((num!=p1->num)&&(p1->next!=NULL))

       {p2=p1;p1=p1->next;}

if(num==p1->num)

       {if(p1==head)head=p1->next;

       elsep2->next=p1->next;

       cout<<"delate:"<<num<<endl;

       total--;

}

else cout<<"cannotfind"<<num;

return (head);

}

*/

 

//提示针对链表操作的合法性

/*

boolHandle_Linklist::Display_LengthInfor(){

       boolflag;

       if(total==0){

              cout<<"Listhas been null now! Please stop handling!";

           flag=false;

              returnflag;

       }

       else

              {

              cout<<"Listhas a or more elements! You can handle it!";

           flag=true;

              returnflag;

       }

}

*/

 

 

      


//链表逆置

student *Handle_Linklist::last_with_first_exchange(student *head){

       //定义一个尾指针

       student*last_pointer=NULL;

       //判断是否为空表

       if(total==0){

              returnlast_pointer;

       }

 

   last_pointer=head;

       //为指针指向最后一个节点

       while(last_pointer->next!=NULL){

              last_pointer=last_pointer->next;

       }

   //提示

       cout<<"last_pinterhas point the last element!";

   //头指针依次前移,各节点依次逆置

       while(head->next->next!=NULL){

              head->next->next=head;

              head=head->next;

              head->next->next=NULL;

       }

       //头指针和尾指针同时指向新的同结点

    last_pointer->next=head->next;

       head->next=NULL;

       head=last_pointer;

 

       cout<<"handlehas been finished!";

       returnhead;

}

 

 

 

//链表前后一定数量的节点交换位置

student * Handle_Linklist::Exchange(intsignal_num,student *head){

       //定义头指针,头指针的拷贝,尾指针

       student*copy_head,*p;

       boolflag;

       //判断是否为空表

       if(total==0){

              flag=false;

              returnhead;

       }

       p=head;

       //将头指针赋值与拷贝头指针,指向目前链表的头节点

       copy_head=head;

       //寻找待交换的交接点,并让P指向该位置

       while(p->num!=signal_num){

              p=p->next;

       }

   

       //head和p同时指向交接点的下一个节点

       head=p->next;

       p->next=NULL;

    p=head;

 

    //令p指向尾节点

       while(p!=NULL){

              p=p->next;

       }

       //尾节点指向头结点

       p->next=copy_head;

   //返回头结点

       returnhead;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息