Re: [問題] Linked list insert node卡關
謝謝板友的提示。我將程式修改了一下,原本卡關的地方有過。insert_node有成功,
但show_test_case的function,也就是要印出這些linked list的function,卻又卡關。
卡關的地方是,他show了兩筆之後就說segmentation fault,而且show的資料看起來
都是head而沒有後面的node。
使用https://www.programiz.com/c-programming/online-compiler/
編譯的結果:
/tmp/kKVJ6xsdfM.o
1
1
Segmentation fault
我上網查了類似的case,寫法幾乎都跟我一樣啊@@....
希望好心的高手大大能再教我一下,感激不盡
修改後的code:
struct node {
int data;
struct node *next;
};
typedef struct node Node;
void insert_node(int num, Node *head_node, Node *last_node)
{
while(num >0)
{
Node *newNode = malloc(sizeof(Node));
newNode-> data = num;
head_node -> next = newNode;
newNode -> next = last_node;
// printf("%d\n", head_node -> data);
num --;
}
}
void gen_test_case(int data_num, Node *head_node, Node *last_node)
{
insert_node(data_num, head_node, last_node);
}
void show_test_case(int print_num, Node *head_node)
{
Node *iteration;
iteration = head_node;
for (int i=0; i< print_num; i++)
{
printf("%d\n", iteration -> data);
iteration = iteration-> next;
}
}
// void find_middle_node()
// {
// }
int main(void)
{
Node *head_node = NULL;
Node *last_node = NULL;
head_node = malloc(sizeof(Node));
head_node -> data = 1;
head_node -> next = last_node;
gen_test_case(3, head_node, last_node);
show_test_case(3, head_node);
//find_middle_node();
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 219.71.109.75 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1628899494.A.BB8.html
推
08/14 12:37,
2年前
, 1F
08/14 12:37, 1F
→
08/14 12:37,
2年前
, 2F
08/14 12:37, 2F
→
08/14 12:37,
2年前
, 3F
08/14 12:37, 3F
推
08/14 16:42,
2年前
, 4F
08/14 16:42, 4F
→
08/14 16:42,
2年前
, 5F
08/14 16:42, 5F
→
08/14 16:42,
2年前
, 6F
08/14 16:42, 6F
→
08/14 16:42,
2年前
, 7F
08/14 16:42, 7F
→
08/14 16:51,
2年前
, 8F
08/14 16:51, 8F
→
08/14 16:51,
2年前
, 9F
08/14 16:51, 9F
→
08/14 16:51,
2年前
, 10F
08/14 16:51, 10F
推
08/15 04:06,
2年前
, 11F
08/15 04:06, 11F
→
08/15 04:06,
2年前
, 12F
08/15 04:06, 12F
→
08/15 04:06,
2年前
, 13F
08/15 04:06, 13F
→
08/15 04:06,
2年前
, 14F
08/15 04:06, 14F
→
08/15 07:07,
2年前
, 15F
08/15 07:07, 15F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):