High-level language programming-Experiment 11 linked list operation (1)

1. Learning at the upper limit
1. Merging linked lists The
following program creates two linked lists, and then merges the second linked list to the end of the first linked list, but the code of the merged part is not completed. Please complete this part of the code.

#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);
}
输入样例
2			(the 1st linked list, 2 students)//2个学生(链表1)
1			(code of no.1 studentof the 1st linked list)//第一个学生编号为1
98			(score of no.1 student of the 1st linked list)//学生编号1的分数为98分
7			(code of no.2 student of the 1st linked list)//第二个学生编号为7
99			(score of no.2 student of the 1st linked list)//学生编号为7的分数为99分
1			(the 2nd linked list, 1 student)//1个学生(链表2)
5			(code of no.1 student of the 2nd linked list)//第一个学生编号为5
87			(score of no.1 student of the 2nd linked list)//学生编号为5的分数为87分


输出样例
       1      98
       7      99
       5      87
       1      98
       7      99
       5      87

answer:

#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;//首先创建一个节点p1,并输入值,他的next为NULL
        if(i==1) head=p1;//链表头
        else p2->next=p1;//链表增加,
        p2=p1;//链表尾
      /*首先创建一个节点p1,并输入值,他的next为NULL。head 指向这个节点,
      然后p2 = p1,p2指向第一个节点,第二次循环。p1又被创建,这时候p2->next = p1,
      p2指向上一次创建的节点,这样p2->next = p1就把这两个节点连上了,
      紧接着p2 = p1,意思是p2指针后一一位,然后就一样了*/}
      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;//若不是则继续向下寻找
    }
    p->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 order of linked list The
following program first creates a linked list, and then calls the reverse function to change the nodes in the linked list into reverse order. Please complete the reverse function.

#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);
}
输入样例
33 students)
1			(code of no.1 student)
98			(score of no.1 student)
4			(code of no.2 student)
99			(score of no.2 student)
5			(code of no.3 student)
87			(score of no.3 student)


输出样例
       1      98
       4      99
       5      87
       5      87
       4      99
       1      98
Published 10 original articles · Like1 · Visits 190

Guess you like

Origin blog.csdn.net/weixin_39475542/article/details/105219284