リンクリストの末尾にノードを挿入できません。私はそのコンセプトを理解しています。私のコードは正しいと思いますが、私のプログラムはクラッシュしています。私はリストの先頭に新しいノードを挿入する関数でメインで作成したリストを持っています。私はこれに問題はありませんが、関数はちょうどテールに挿入します。以下にコードを示します。リンクリストの末尾に新しいノードを挿入する
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int number;
struct node * next;
} Node;
typedef Node * Nodeptr;
void insertnode_tail(Nodeptr head);
Nodeptr find_last(Nodeptr head);
void insertnode_tail(Nodeptr head) // function to insert node at the tail.
{
Nodeptr here = find_last(head);
Nodeptr newentry = NULL;
int n = 0;
printf("Enter the value to be assigned to the new entry? \n");
scanf("%d", &n);
if((newentry = malloc(sizeof(Node))) == NULL) {
printf("No Memory\n");
exit(0);
}
newentry -> number = n;
newentry -> next = NULL;
here -> next = newentry;
traverse1(head);
}
Nodeptr find_last(Nodeptr head) // Function to return the last node of list
{
Nodeptr aux = head;
int n = 0;
while(aux != NULL) {
aux = aux->next; // moves the aux pointer along the list
n++;
}
return aux;
}
感謝を定義することができます。それは今働いている。 –
あなたは正しくansをマークすることができます。 –