彗星爱酿酒 2023-03-28 00:34 采纳率: 59.3%
浏览 80
已结题

关于单链表的插入问题

这么写有什么不对吗,为什么它运行不了?有些不太理解,希望有人能具体解惑一下,如果可以的话,请讲的简单易懂一点。

#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
    int Data;
    struct Node *Next;
}node,*List; 

List Read(); 
void Print( List L ); 
List Insert( List L, int X );

int main()
{
    List L;
    int X;
    L = Read();
    scanf("%d", &X);
    L = Insert(L, X);
    Print(L);
    return 0;
}
List Insert( List L, int X )
{
    List pr;
    pr=(List)malloc(sizeof(struct Node));
    if(X<=L->Data){
        pr->Next=L;
        pr->Data=X;
        return pr;
    }else{
        if(L->Next==NULL){
            L->Next=pr;
            pr->Data=X;
            pr->Next=NULL;
            
        }else{
            L->Next=Insert(L->Next,X);
        }
    }
    return L;
}

List Read(){
    List L,h;
    L=h=(List)malloc(sizeof(struct Node));
    int i,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        L=(List)malloc(sizeof(struct Node));
        scanf("%d",L->Data);
        L=L->Next;
    }
    L->Next=NULL;
    L=h->Next;
    free(h);
    h=L;
    return h;
}
void Print( List L){
    while(L->Next!=NULL){
        printf("%d",L->Data);
        L=L->Next;
    }
}

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-03-28 07:55
    关注

    目测有3个错误
    List Insert(List L, int X) 函数中应该先判断 L 是否为 NULL。
    scanf("%d", L->Data) 应该改为 scanf("%d", &(L->Data))。
    Print 函数中的 while(L->Next!=NULL) 应该改为 while(L!=NULL)。

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node {
        int Data;
        struct Node *Next;
    } node, *List;
    
    List Read();
    void Print(List L);
    List Insert(List L, int X);
    
    int main()
    {
        List L;
        int X;
        L = Read();
        scanf("%d", &X);
        L = Insert(L, X);
        Print(L);
        return 0;
    }
    
    List Insert(List L, int X)
    {
        if (L == NULL) {
            L = (List)malloc(sizeof(struct Node));
            L->Data = X;
            L->Next = NULL;
        } else {
            if (X <= L->Data) {
                List pr;
                pr = (List)malloc(sizeof(struct Node));
                pr->Next = L;
                pr->Data = X;
                L = pr;
            } else {
                L->Next = Insert(L->Next, X);
            }
        }
        return L;
    }
    
    List Read()
    {
        List L, h;
        L = h = (List)malloc(sizeof(struct Node));
        int i, n;
        scanf("%d", &n);
        for (i = 1; i <= n; i++) {
            L->Next = (List)malloc(sizeof(struct Node));
            scanf("%d", &(L->Next->Data));
            L = L->Next;
        }
        L->Next = NULL;
        L = h->Next;
        free(h);
        h = L;
        return h;
    }
    
    void Print(List L)
    {
        while (L != NULL) {
            printf("%d ", L->Data);
            L = L->Next;
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月29日
  • 已采纳回答 3月28日
  • 创建了问题 3月28日

悬赏问题

  • ¥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时遇到的编译问题