我想一個節點添加到一個單向鏈表的結尾,但我得到一個分割故障(核心轉儲錯誤)添加節點到端單向鏈表
void slist_add_back(struct slist *l, char *str)
{
struct snode *temp;
do{
l->front = l->front->next;
l->counter++;
}while(l->front !=NULL);
l->counter++;
temp = (struct snode *)malloc(sizeof(struct snode));
l->back = l->front;
l->back->next = temp;
l->back = temp;
}
你似乎只增加了一個元素而增加了計數器。 – chris
這是比C++更多的C代碼。 –
'front'代表你的列表的前面嗎?如果是這樣,你不想因爲你在這裏添加另一個元素而改變它。 – tabstop