xbWsz 2021-12-26 01:10 采纳率: 100%
浏览 81
已结题

C语言链表题 本地测试正常,上传到超星OJ显示错误

本地code blocks多次测试均没有问题,超星上五条测试仅有一条“若是空链表,则输出NULL”通过,其余四条未通过(其中一条显示超时)
以下是题目和我的代码(其中我写的部分前有注释“ /请在以下位置补充完整,实现函数 的功能/”):

/*
题目编号 :Exp09-Basic03
题目名称:求单链表中间结点
题目描述:请填写缺失代码完成程序,实现如下功能:
首先根据键盘随机输入,以0结束的若干非零整数建立单链表;
然后寻找处于链表中间位置的结点,若中间结点有两个,则设定前一个为中间位置结点;
最后将从中间结点开始到链表尾各结点值输出,相邻数字间以一个西文空格间隔,最后一个数字后无任何字符。
若是空链表,则输出NULL。

例如,

输入:5 4 2 1 3 0
输出:2 1 3

输入: 4 2 1 3 3 2 0
输出:1 3 3 2

***注意***:
提交答案时,需粘贴完整的源代码,仅粘贴填空处的代码将被判错。
*/
#include <stdio.h>
#include <malloc.h>
struct cell {//单链表结点结构体定义
 int x;
 struct cell* next;
};
struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
 struct cell* head, * tmp, * p;
 head = tmp = p = NULL;
 int x;
 /*请在以下位置补充完整,实现函数build的功能*/
   scanf("%d",&x);
   head=(struct cell*)malloc(sizeof(struct cell));
   if(x!=0) {head->x=x;
   tmp=head;
   scanf("%d",&x);
   while (x!=0){
        p=(struct cell*)malloc(sizeof(struct cell));
        p->x=x;
        p->next=NULL;
        tmp->next=p;
        tmp=p;
        scanf("%d",&x);
   }}
   else
    head=NULL;
 return head;//返回单链表头
}
struct cell* mid(struct cell* head) {//寻找链表中间位置结点地址并返回,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数mid的功能*/
    struct cell * p,*p0;
    int c=0,i;
    p=head;
    while(p!=NULL){
        c++;
        p=p->next;
    }
    p=head;
    if (c%2==0){
        for(i=1;i<(c/2);i++){
            p=p->next;
        }
    }
    else{
        for(i=1;i<(c/2+1);i++){
            p=p->next;
        }
    }
    return p;
}
void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数print的功能*/
    struct cell * p;
    p=head;
    printf("%d",p->x);
    p=p->next;
    while(p!=NULL){
        printf(" %d",p->x);
        p=p->next;
    }

}
void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数release的功能*/
    struct cell *x,*y;
    x=head;
    while(x!=NULL){
        y=x;
        free(y);
        x=x->next;
    }
}
int main(void) {
 struct cell* head,*half;
 head = build();
 half = mid(head);
 if(half!=NULL)
        print(half);
    else
        printf("NULL");
 release(head);
}


  • 写回答

1条回答 默认 最新

  • 关注

    代码有错误
    if(x!=0) {
    head=(struct cell*)malloc(sizeof(struct cell));
    head->x=x;
    head->next=NULL; // 加上这行,否则链表只有一个节点时会遍历出错
    tmp=head;

    while(x!=NULL){
        y=x;
        x=x->next;
        free(y);//这行要放x=x->next;下面
    }
    

    你题目的解答代码如下:

    #include <stdio.h>
    #include <malloc.h>
    struct cell {//单链表结点结构体定义
     int x;
     struct cell* next;
    };
    struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
     struct cell* head, * tmp, * p;
     head = tmp = p = NULL;
     int x;
     /*请在以下位置补充完整,实现函数build的功能*/
       scanf("%d",&x);
       if(x!=0) {
            head=(struct cell*)malloc(sizeof(struct cell));
            head->x=x;
            head->next=NULL;    // 加上这行,否则链表只有一个节点时会遍历出错
            tmp=head;
            scanf("%d",&x);
            while (x!=0){
                p=(struct cell*)malloc(sizeof(struct cell));
                p->x=x;
                p->next=NULL;
                tmp->next=p;
                tmp=p;
                scanf("%d",&x);
            }
       }
       else
        head=NULL;
     return head;//返回单链表头
    }
    struct cell* mid(struct cell* head) {//寻找链表中间位置结点地址并返回,head是单链表首结点指针
     /*请在以下位置补充完整,实现函数mid的功能*/
        struct cell * p,*p0;
        int c=0,i;
        p=head;
        p0=head;
        while(p!=NULL){  //只用一次遍历即可
            c++;
            if (c>2 && c%2==1)
                p0=p0->next;
            p=p->next;
        }
        // p=head;
        // if (c%2==0){
        //     for(i=1;i<(c/2);i++){
        //         p=p->next;
        //     }
        // }
        // else{
        //     for(i=1;i<(c/2+1);i++){
        //         p=p->next;
        //     }
        // }
        return p0;
    }
    void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针
     /*请在以下位置补充完整,实现函数print的功能*/
        struct cell * p;
        p=head;
        printf("%d",p->x);
        p=p->next;
        while(p!=NULL){
            printf(" %d",p->x);
            p=p->next;
        }
    }
    void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针
     /*请在以下位置补充完整,实现函数release的功能*/
        struct cell *x,*y;
        x=head;
        while(x!=NULL){
            y=x;
            x=x->next;
            free(y);//这行要放x=x->next;下面
        }
    }
    int main(void) {
     struct cell* head,*half;
     head = build();
     half = mid(head);
     if(half!=NULL)
            print(half);
        else
            printf("NULL");
     release(head);
    }
    

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月3日
  • 已采纳回答 12月26日
  • 修改了问题 12月26日
  • 创建了问题 12月26日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题