热爱学习的呆萌的文青 2015-12-18 03:16 采纳率: 54.8%
浏览 1621
已采纳

c语言单向链表的问题???

 #include<stdio.h>
#include<stdlib.h>
struct node{
    int num;
    struct node *next;
};

//构建空的链表
struct node* InitList(struct node *L){
    L = (struct node*)malloc(sizeof(struct node));
    L = NULL;
    printf_s("InitList sucess!");
    return L;
}
//创建单链表
struct node* CreateList(struct node *L,int n){
    struct node *temp,*p;
    L = (struct node*)malloc(sizeof(struct node));
    L->next = NULL;
    temp=L;
    for (int i = 0; i < n; i++){
        p = (struct node*)malloc(sizeof(struct node));
        scanf_s("%d", &p->num);
        temp->next = p;
        temp = p;
    }
    temp->next = NULL;
    return L;
}
void PrintList(struct node *L){
    struct node *temp = L;
    while (temp != NULL){
        printf_s("%d", temp->num);
        temp = temp->next;
    }
}
void PrintMenu(){
    printf_s("------Menu------\n");
    printf_s("0  InitList\n");
    printf_s("1  CreateList\n");
    printf_s("2  PrintList\n");
}
void main(){
    int n,c;
    struct node *La;
    PrintMenu();
    printf_s("Enter the command: ");
    scanf_s("%d", &c);
    switch (c){
    case 0:
        La = InitList(La);
        break;
    case 1:
        printf_s("Enter the number of LinkList: ");
        scanf_s("%d", &n);
        La = CreateList(La, n);
        break;
    case 2:
        PrintList(La);
        break;
    default:
        printf_s("ERROR,Enter again: ");
        break;
    }
    system("pause");
}

为什么主函数case 0 的La = InitList(La); 这句报错: error C4700: uninitialized local variable 'La' used。 ????

  • 写回答

3条回答

  • ysuwood 2015-12-18 06:36
    关注

    完善了一下,头结点没存数,print时要从第二个开始:

     #include<stdio.h>
    #include<stdlib.h>
    struct node{
        int num;
        struct node *next;
    };
    
    //构建空的链表
    void InitList(struct node *&L){//修改
        L = (struct node*)malloc(sizeof(struct node));
        L->next = NULL;
        L->num=0;
        printf_s("InitList sucess!");
    }
    //创建单链表
    void CreateList(struct node *&L,int n){//修改
        struct node *temp,*p;
        L = (struct node*)malloc(sizeof(struct node));
        L->next = NULL;
        L->num=0;
        temp=L;
        for (int i = 0; i < n; i++){
            p = (struct node*)malloc(sizeof(struct node));
            scanf_s("%d", &p->num);
            temp->next = p;
            temp = p;
        }
        temp->next = NULL;
    }
    void PrintList(struct node *L){
        struct node *temp = L->next;//修改,头结点不使用
        while (temp != NULL){
            printf("%d", temp->num);
            temp = temp->next;
        }
    }
    void PrintMenu(){
        printf_s("------Menu------\n");
        printf_s("0  InitList\n");
        printf_s("1  CreateList\n");
        printf_s("2  PrintList\n");
    }
    void main(){
        int n,c;
        struct node *La;
        c=1;
        while(c>=0)
        {
            PrintMenu();
            printf_s("Enter the command: ");
            scanf_s("%d", &c);
            switch (c){
            case 0:
                InitList(La);
                break;
            case 1:
                printf_s("Enter the number of LinkList: ");
                scanf_s("%d", &n);
                CreateList(La, n);
                break;
            case 2:
                PrintList(La);
                break;
            default:
                printf_s("ERROR,Enter again: ");
                break;
            }
        }
    
        struct node *te;//增加释放空间
        if(La)
        {
            te=La->next;
            free(La);
            La=te;
        }
        system("pause");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?