SCAU程序设计在线实训平台_实验_高级语言程序设计_实验11_链表操作_堂上练习

1098 [填空]链表结点的插入

Description

完成插入链表结点的函数(按学号顺序),并调试通过、提交。

#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 *insert(struct student *head, struct student *stud)
{  
_______________________
}

main()
{
    struct student *head,*stu;
    int n;
    scanf("%d",&n);   
    head=create(n);
    print(head);
    stu=(struct student *)malloc(LEN);
    scanf("%ld",&stu->num);        
    scanf("%d",&stu->score);    
    stu->next = NULL;
    head=insert(head,stu);
    print(head);
}
输入样例

3 (3 students)
1 (code of no.1 student)
98 (score of no.1 student)
3 (code of no.2 student)
99 (score of no.2 student)
5 (code of no.3 student)
87 (score of no.3 student)
4 (code of no.3 student needs be inserted)
77 (score of no.3 student needs be inserted)

输出样例
   1      98
   3      99
   5      87
   1      98
   3      99
   4      77
   5      87
代码实现
struct student *insert(struct student *head, struct student *stud)
{
    struct student *p=head,*before=NULL;
    if(stud->num<head->num){    //如果为最小
        head=stud;
        stud->next=p;
    }
    else{
        while(stud->num > p->next->num)p=p->next;
        before=p;   //前驱结点
        p=p->next;
        before->next=stud;
        stud->next=p;
    }
    return head;
}
法2
    struct student *node = head, *before = NULL;
    if (stud->num < node->num){
        head = stud;
        stud->next=node;
    }
    while (stud->num > node->num && node != NULL) {
        before = node;
        node = node->next;
    }
    before->next = stud;
    stud->next = node;
    return head;

这道题不知道是不是OJ评判出错了,在插入到最后时会编译器报错,但在OJ上可以顺利通过。
可能是尾部追加不算是插入?有点奇怪

1104 [填空题]链表的倒序

Description

下面程序,先创建一个链表,然后调用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);
}
输入样例

3 (3 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
代码实现
struct student *reverse(struct student *head)
{
    struct student *node=head,*before=NULL,*after=NULL;
    while(node!=NULL){
        after=node->next;
        node->next=before;
        before=node;
        node=after;
    }
    return before;
}

在这里插入图片描述

有空会做个动画

错误的代码
struct student *reverse(struct student *head)
{
    struct student *before=NULL,*node=head,*after=NULL;
    while(node->next!=NULL){
        after=node->next;
        after->next=node;
        node=after;			//问题在这
    }
    return node;
}

1101 [填空题]链表的排序

Description

下面程序,先创建一个链表(链表中各结点未按学号由小到大排序),然后调用sort函数,将链表中各结点按学号由小到大排序。

#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 *insert(struct student *head, struct student *stud)
{  struct student *p0,*p1,*p2;
    p1=head;  p0=stud;
    if(head==NULL)
      {head=p0;}
    else
   { while( (p0->num > p1->num) && (p1->next!=NULL) )
       { p2=p1;     p1=p1->next;}
     if( p0->num <= p1->num )
      {  if( head==p1 ) head=p0;
           else p2->next=p0;
         p0->next=p1; }
     else {  p1->next=p0;}
     }
    return(head);
}

struct student *del(struct student *head,long num)
{
    struct student *p1,*p2;
    p1=head;
    while(p1!=NULL)
    {
        if(p1->num == num)
        {
          if(p1 == head) head=p1->next;
          else p2->next=p1->next;
          free(p1);
          break;
        }
        p2=p1;
        p1=p1->next;
    }
    return(head);
}

struct student *sort(struct student *head)
{
_______________________
}

main()
{
    struct student *head,*stu;
    int n;
    scanf("%d",&n);
    head=create(n);
    print(head);
    head=sort(head);
    print(head);
}
输入样例

3 (the 1st linked list, 2 students)
1 (code of no.1 student)
98 (score of no.1 student)
7 (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
   7      99
   5      87
   1      98
   5      87
   7      99
代码1
struct student *sort(struct student *head)
{
    struct student *node=head,*Alonenode=NULL;
    while (node!=NULL){
        Alonenode=(struct student *)malloc(LEN);
        Alonenode->num=node->num;
        Alonenode->score=node->score;
        Alonenode->next=NULL;


        head=del(head,Alonenode->num);
        // printf("--del-%d--\n",Alonenode->num);
        //print(head);

        head=insert(head,Alonenode);
        //printf("--ins---\n");
        //print(head);

        node=node->next;
        //printf("--end---\n");
        free(Alonenode);
    }
    return head;
}

题目代码中的DEL函数感觉有点问题,导致OJ提交不上

成功代码
struct student *sort(struct student *head){
    struct student *Alonenode=NULL,*node=head->next;//node从第二个开始

    head->next=NULL;   //删掉后部,只留头
    Alonenode=node;     //后部的第一个节点
    
    while(node!=NULL){
        node=node->next;    //先跑一步防止孤立
        Alonenode->next=NULL;   //断后、孤立(此时的Alonenode已经是前驱节点)
        head=insert(head,Alonenode);    //插入孤立节点
        Alonenode=node;     //前驱
    }
    return head;
}

在这里插入图片描述

根据@NeilGGG的代码优化而来

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值