0
我想寫一個函數,可以添加新的結構到鏈表的末尾。而它總是出現分段錯誤。C:鏈接列表推段錯誤
void
push(stk_t *stklist, info_t *gds)
{
stk_t *current = stklist;
if (current == NULL)
{
current->gds = (info_t *)malloc(sizeof(info_t));
current->gds = gds;
current->next = (stk_t *)malloc(sizeof(stk_t));
current->next = NULL;
}
else
{
while (current != NULL)
{
current = current->next;
}
current->next = (stk_t *)malloc(sizeof(stk_t));
current->next->gds = (info_t *)malloc(sizeof(info_t));
current->next->gds = gds;
current->next->next = (stk_t *)malloc(sizeof(stk_t));
current->next->next = NULL;
}
}
我的結構
typedef struct{
char name[NAME_SIZE];
char aisle;
int shelf;
int weight;
int price;
int quantity;
} info_t;
typedef struct stk stk_t;
struct stk{
info_t *gds;
stk_t *next;
};
功能推的目的()是第二個參數添加到鏈接列表的末尾。
提示:當你做'電流 - > GDS =(info_t *)malloc(sizeof(info_t));','current'具有什麼值? – molbdnilo
'if(current == NULL) { current-> gds' - 解引用NULL指針。 – szczurcio
@molbdnilo糟糕,我打破了你的暗示。當我發佈時沒有看到它,對不起。 – szczurcio