逆方向に出てくるリンクリストがあります。要素を前に配置したいときに、リストの後ろに要素を追加しているようです。私のノードは次のように表示されます。
struct node{
int data;
struct node* next;};
次に、ヘッドと変数を設定します
int info,x,listLength;
struct node *head = NULL;
struct node *temp;
printf("How many nodes?\n");
scanf("%d",&listLength);
ここで、リストに新しいエントリを入力するように求め、ノードに沿って移動します
for(x=1;x<=listLength;x++){
printf("Insert an X value for node %d\n",x);
scanf("%d",&info);
temp = (struct node*)malloc(sizeof(struct node));
temp->data = info;
temp->next = head;
head = temp;
}
最後に結果を出力し、メモリ領域を解放します
while(temp!=NULL){
printf("WE GOT %d\n",temp->data);
temp = temp->next;
}
free(temp);
ただし、3 つのノードの入力を入力し、1、2、3 の順に入力すると、出力は 3、2、1 になります。これを変更して、ノードが正しい場所に追加されるようにするにはどうすればよいですか? 前もって感謝します!