问题描述
- 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 *Lint 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------ ""); printf_s(""0 InitList ""); printf_s(""1 CreateList ""); printf_s(""2 PrintList "");}void main(){ int nc; 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(""ERROREnter 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 *&Lint 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------
""); printf_s(""0 InitList
""); printf_s(""1 CreateList
""); printf_s(""2 PrintList
"");}void main(){ int nc; 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(""ERROREnter again: ""); break; } } struct node *te;//增加释放空间 if(La) { te=La->next; free(La); La=te; } system(""pause"");}
解决方案二:
struct node *La;
->
struct node *La = (node *)malloc(sizeof(node));
解决方案三:
C语言之单向链表
C语言实现链表之单向链表(十五)测试用例
C语言单向循环链表解决约瑟夫问题
解决方案四:
可以用引用。给你改了一部分,自己再完善吧,还需要free
#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 = NULL; printf_s(""InitList sucess!"");}//创建单链表void CreateList(struct node *&Lint 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;}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------
""); printf_s(""0 InitList
""); printf_s(""1 CreateList
""); printf_s(""2 PrintList
"");}void main(){ int nc; 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(""ERROREnter again: ""); break; } } system(""pause"");}
时间: 2024-10-08 12:19:49