セグメンテーションフォールトは "else if(head - > next == NULL){"で発生します。 ポインタに関する基本的なものが欠けているような気がします。CのLinkedList挿入メソッドでセグメンテーション違反が発生するのはなぜですか?
ここにコードがあります。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int value;
struct Node* next;
}Node;
void insert(Node* head, int value){
if(head == NULL){
head = (Node*)malloc(sizeof(struct Node));
head -> value = value;
head -> next = NULL;
}else if(head -> next == NULL){
printf("good\n");
head -> next = (Node*)malloc(sizeof(struct Node));
head -> next -> value = value;
head -> next -> next = NULL;
}else{
insert(head -> next, value);
}
}
int main(){
struct Node* head;
head = NULL;
insert(head, 3);
insert(head, 4);
printf("%d\n", head -> value);
return 0;
}
Cは_methods_をサポートしていません。 C++は異なる言語です。 – Olaf
そして、 'malloc'の結果をC言語でキャストしないでください。 – Olaf
そのCプログラムです。私はこれをC++とタグ付けするつもりはありませんでした。 –