大佬們,幫我看看代碼要崩潰了! 我想創建一個鏈表,鏈表后面插入一個元素 然后輸出這個元素,但就是插入時出錯!!
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *next;
}Node;
typedef struct Node *List;
void InitList(List L)
{
L=(List)malloc(sizeof(Node));
if(!L)
{
printf("記憶體分配失敗!");
exit(0);
}
L->next=NULL;
}
void Insert(List L,int a)
{
List p,s;
p=L;
s=(List)malloc(sizeof(Node));
if(!s)
{
printf("記憶體分配失敗!");
exit(0);
}
s->next=NULL;
s->data=https://bbs.csdn.net/topics/a;
s->next=p->next;
p->next=s;
}
main()
{
List ha;
InitList(ha);
Insert(ha,1);
printf("%d ",ha->next->data);
}
uj5u.com熱心網友回復:
你這個傳遞有問題,你初始化一個ha指標,把這個指標傳去初始化,這樣是無效的。接受引數你那里也是list,型別一樣,是傳值,你在init里的賦值是給L在操作,和ha沒關系的。uj5u.com熱心網友回復:
所以你又把NULL的ha拿去插入,NULL沒有->next,所以在s -next=p-next時會segmentation fault轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/98859.html
標籤:C語言
上一篇:Python3.6 + opencv3.4.4 + vs2017重新封裝海康sdk 生成python鏈接時報錯無法決議的外部符號
