程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 單鏈表-c語言單向鏈表的問題???

單鏈表-c語言單向鏈表的問題???

編輯:編程綜合問答
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。 ????

最佳回答:


完善了一下,頭結點沒存數,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");
}
qq_27183003
xq734536013
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved