SCAU高级语言程序设计--实验11 链表操作(1)

SCAU高级语言程序设计--实验11 链表操作(1)

一、堂上限时习题

1、链表的合并

题目:下面程序创建两个链表,然后将第二个链表合并到第一个链表未尾,但合并部分的代码未完成,请你完成这部分代码。


#include "stdio.h" 
#include "malloc.h" 
#define LEN sizeof(struct student) 

struct student 

     long num; 
     int score; 
     struct student *next; 
}; 

struct student *create(int n) 
{  
     struct student *head=NULL,*p1=NULL,*p2=NULL; 
     int i; 
     for(i=1;i<=n;i++) 
     {  p1=(struct student *)malloc(LEN); 
        scanf("%ld",&p1->num);     
        scanf("%d",&p1->score);     
        p1->next=NULL; 
        if(i==1) head=p1; 
        else p2->next=p1; 
        p2=p1; 
      } 
      return(head); 


struct student *merge(struct student *head, struct student *head2) 
{  
_______________________ 



void print(struct student *head) 

    struct student *p; 
    p=head; 
    while(p!=NULL) 
    { 
        printf("%8ld%8d",p->num,p->score); 
        p=p->next; 
        printf("\n"); 
    } 


main() 

    struct student *head, *head2; 
    int n; 
    long del_num; 
    scanf("%d",&n);  
    head=create(n); 
    print(head); 
    scanf("%d",&n);  
    head2=create(n); 
    print(head2); 
    head = merge(head, head2);     
    print(head); 

思路:不清楚的可以画个图出来。具体看注释。简单来说,一个功能一个函数。

#include "stdio.h" 
#include "malloc.h" 
#define LEN sizeof(struct student) 
//学生结构体定义
struct student 
{ 
     long num;//学号
     int score;//成绩
     struct student *next;//连接指针 
}; 

//创建学生的函数
struct student *create(int n) 
{  
     struct student *head=NULL,*p1=NULL,*p2=NULL; 
     int i; 
     for(i=1;i<=n;i++) 
     {  p1=(struct student *)malloc(LEN); //根据len的取值,动态分配内存
        scanf("%ld",&p1->num);//输入学号     
        scanf("%d",&p1->score);//输入分数   
        p1->next=NULL; 
        if(i==1) head=p1;//链表头 
        else p2->next=p1;//链表增加
        p2=p1; //链表尾
      } 
      return(head);//结果返回 
} 

//合并两个链表
struct student *merge(struct student *head, struct student *head2) 
{  
    struct student *p=NULL;
    p=head;//链表头
    while(p->next!=NULL){//判断是否为链表1的尾巴
        p=p->next;//若不是继续向下寻找
    }
    p1->next=head2;//连接两个表
    return (head);
} 

//打印输出
void print(struct student *head) 
{ 
    struct student *p; 
    p=head; 
    while(p!=NULL) 
    { 
        printf("%8ld%8d",p->num,p->score); 
        p=p->next; //下一条
        printf("\n"); 
    } 
} 
//主程序
main() 
{ 
    struct student *head, *head2; //两个链表
    int n; 
    long del_num; 
    scanf("%d",&n); //链长
    head=create(n); //创建第一条链
    print(head); //打印第一条链
    scanf("%d",&n); //链长
    head2=create(n); //创建第二条链
    print(head2); //低音第二条链
    head = merge(head, head2); //合并两条链    
    print(head); //打印合并后的两条链
} 

 

2、链表的倒序

题目:下面程序,先创建一个链表,然后调用reverse函数,将链表中各结点变为倒序排列。请完成reverse函数,


#include "stdio.h" 
#include "malloc.h" 
#define LEN sizeof(struct student) 

struct student 

     long num; 
     int score; 
     struct student *next; 
}; 

struct student *create(int n) 
{  
     struct student *head=NULL,*p1=NULL,*p2=NULL; 
     int i; 
     for(i=1;i<=n;i++) 
     {  p1=(struct student *)malloc(LEN); 
        scanf("%ld",&p1->num); 
        scanf("%d",&p1->score); 
        p1->next=NULL; 
        if(i==1) head=p1; 
        else p2->next=p1; 
        p2=p1; 
      } 
      return(head); 


void print(struct student *head) 

    struct student *p; 
    p=head; 
    while(p!=NULL) 
    { 
        printf("%8ld%8d",p->num,p->score); 
        p=p->next; 
        printf("\n"); 
    } 


struct student *reverse(struct student *head) 

_______________________ 


main() 

    struct student *head,*stu; 
    int n; 
    scanf("%d",&n);   
    head=create(n); 
    print(head); 
    head=reverse(head); 
    print(head); 

 

思路:这个有点绕,需要画个流程图就清楚怎么回事了。简单来说,目的是把,第二个值的next连接到前一个值,第三个值的next连接到第二个值,如此类推,最后把原本第一个值的next清空,作为最后一个值。倒序就实现了。

struct student *reverse(struct student *head) 
{ 
    struct student *p1=NULL,*p2=NULL,*p3=NULL;
    p2=head;
    p3=head->next;
    do{
        p1=p2;
        p2=p3;
        p3=p2->next;
        p2->next=p1;//倒序连接
    while(p3!=NULL);//判断链是否结束
    head->next=NULL;//记得原来链表头next还连接着第二个值的,所以需要清空
    return (p2);
} 

 

  • 8
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值