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

日常联系:一个C++程序完成链表的增加结点、删除结点、打印、查询、逆序等操作

2017-01-07 20:11 525 查看
一、每个结点结构体声明如下,注意该链表是不带表头的单链表:

struct node

{

int num;

int score;

struct node *next;

};

二、定义每个结点有学号和分数以及指向下一个结点的指针,现在想写一个creat_nod能够创建一个结点,希望它带num和score两个参数,创建完毕以后直接返回该结点结构体类型指针。

struct node *creat_nod(int x,int y)

{

struct node *p; 

        p = new node;

p->num = x;

p->score = y;

return p;

}

三、删除某个结点,希望直接给定链表head以及学号,就删除特定学号结点,这里有3种情况,表头,表尾和表中。

1、表头情形,将第二个结点升为头结点,head=head->next;释放头结点存储空间。 

2、表中与表尾情形类似,直接删除,将自己指向下一个的地址传给上一个结点,表尾时传的是NULL。

struct node *del_nod(node *n_head,int x)

{

   struct node *p,*q;

   p = n_head;

   if(n_head==NULL) cout<<"链表为空!";//排除特殊情形

   else

   {

  while(p!=NULL&&p->num!=x) {q=p;p=p->next;}//找到要删的结点位置

       if(p==n_head) {n_head=p->next;delete p;}//删除头结点情形

  else if(p!=NULL) {q->next=p->next;delete p;}

else cout<<x<<"不在链表中!";

   

   

   }

   return n_head;

}

四、插入某个结点,希望链表根据学号予以排序,也分3种情形,表头、表尾和表中。

1、表头情形下,插入节点变成表头,先将表头地址给新结点的next,再将新结点地址赋给表头。

2、表尾情形下,先将新结点地址赋给表尾next,再将新结点的next置空。新结点成了尾节点。

3、表中情形下,新结点地址给前一个结点的next,新结点的next指向下一个结点。

struct node *insert_nod(node *n_head,int x,int y)

{

struct node *p,*q,*n_nod;

n_nod=new node;

n_nod->num=x;

n_nod->score=y;

p=n_head;

if(n_head==NULL) {n_head=n_nod;n_nod->next=NULL;}//排除特殊情形

else 

{

while(p!=NULL&&x>p->num)

{

q=p;p=p->next;

}//找到插入点

if(p==n_head){n_nod->next=n_head;n_head=n_nod;}//插入头结点之前的情形

else if(p==NULL){q->next=n_nod;n_nod->next=NULL;}

else{ 

     q->next=n_nod;n_nod->next=p;

}

}

return n_head;

}

五、给一个头指针,即可打印所有结点,需要遍历每个结点。

void print_nod(node *w)

{

    struct node *p;

p=w;

if(w=NULL) cout<<"链表为空!";

while(p!=NULL)

{

cout<<"学号:"<<p->num<<setw(10)<<"分数:"<<p->score<<endl;

p=p->next; 

}

}

六、根据头指针和学号能够查找相应结点并输出成绩。

void search_nod(node *w,int x)

{

struct node *p;

p=w;

while(p!=NULL&&p->num!=x)

{

p=p->next;

}

if(p==NULL) cout<<"您找的学号不存在!";

else print_nod(p);

}

七、链表的逆序,我希望只给一个头指针就能实现逆序功能,注意交换指针时要注意保存指向下一个结点的指针。

struct node *reverse_nod(node *n_head)

{

 struct node *p,*q,*t; 

 int i=0;

 p=n_head;

 if(n_head==NULL) cout<<"链表为空!";

 else

 {

  while(t!=NULL)

  {

   if(i==0){

    q=p;

    p=p->next;   

    q->next=NULL;

    t=p->next;

    p->next=q;   

    i++;

   }//开头的情形,需要将头结点的next清空

   else 

    {

     q=p;

     p=t;

     t=p->next;

     p->next=q;

    }

  }

 

  n_head=p;

 

 }

 return n_head;

}

八、下面写一个主函数测试它们。

int main()

{

struct node *head,*stu1,*stu2,*stu3;

int num,score;

num=1170381;

score=55;

head=creat_nod(num,score);

num=1170382;

score=78;

stu1=creat_nod(num,score);

num=1170384;

score=43;

stu2=creat_nod(num,score);

num=1170385;

score=76;

stu3=creat_nod(num,score);

head->next=stu1;

stu1->next=stu2;

stu2->next=stu3;

stu3->next=NULL;//创建了一个4个结点的链表

head=insert_nod(head,1170383,88);

    head=insert_nod(head,1170386,44);

head=insert_nod(head,1170380,44);

print_nod(head);//插入3个结点并打印

cout<<endl;

head=del_nod(head,1170380);

head=del_nod(head,1170383);

head=del_nod(head,1170386);

print_nod(head);

cout<<endl;//删除插入的结点

cout<<"请输入您查找的学号:";

cin>>num;

search_nod(head,num);//查找指定结点并打印

 

return 0;

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