void addNewNode (struct node *head, int n)
{
struct node* temp = (struct node*) malloc(sizeof(struct node));
temp -> data = n;
temp -> link = head;
head = temp;
}
上記のコードは、リンクされたリストの先頭に新しいノードを追加するための一般的に間違ったバージョンの関数です。一般的に、正しいバージョンは次のようになります。
void addNewNode (struct node **head, int n);
void addNewNode (struct node * &head, int n);
うまく機能する目的のために、別の単純な関数を作成しました。
struct node* addNewNode (struct node *head, int n)
{
struct node* temp = (struct node*) malloc(sizeof(struct node));
temp -> data = n;
temp -> link = head;
return temp;
}
しかし、これがコードやチュートリアルで使用または議論されているのを見たことがないので、このアプローチに何らかの欠陥があるかどうか知りたい.