Во-первых, непонятно, что вы хотите от newtemp->source = x;
. Вы не можете назначить один символ для массива char ... Я предполагаю, что это строка ...
В любом случае - способ classi c добавить в конец связанного списка - :
void addtoend(struct flightnode **p, char* x)
{
// Construct the new node
struct node* newtemp = malloc(sizeof *newtemp);
strcpy(newtemp->source, x);
newtemp->next = NULL;
// Is the list empty?
if (*p == NULL)
{
// yes... so the new node will be the new head
*p = newtemp;
return;
}
// Iterate to last node
struct flightnode *tmp = *p;
while(tmp->next != NULL) tmp = tmp->next;
// Add new node after last node
tmp->next = newtemp;
}
и назовите его так:
addtoend(&head, "Hello");
И полный пример:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct flightnode {
char source[20];
char destination[20];
struct flightnode *next;
};
void addtoend(struct flightnode **p, char* x)
{
// Construct the new node
struct flightnode* newtemp = malloc(sizeof *newtemp);
strcpy(newtemp->source, x);
newtemp->next = NULL;
// Is the list empty?
if (*p == NULL)
{
// yes... so the new node will be the new head
*p = newtemp;
return;
}
// Iterate to last node
struct flightnode *tmp = *p;
while(tmp->next != NULL) tmp = tmp->next;
// Add new node after last node
tmp->next = newtemp;
}
void printList(struct flightnode *p)
{
printf("---- LIST ----\n");
while(p)
{
printf("%s\n", p->source);
p = p->next;
}
printf("--------------\n");
}
int main() {
struct flightnode* head = NULL;
addtoend(&head, "Hello");
printList(head);
addtoend(&head, "world");
printList(head);
addtoend(&head, "have");
printList(head);
addtoend(&head, "a");
printList(head);
addtoend(&head, "nice");
printList(head);
addtoend(&head, "day");
printList(head);
return 0;
}
Вывод:
---- LIST ----
Hello
--------------
---- LIST ----
Hello
world
--------------
---- LIST ----
Hello
world
have
--------------
---- LIST ----
Hello
world
have
a
--------------
---- LIST ----
Hello
world
have
a
nice
--------------
---- LIST ----
Hello
world
have
a
nice
day
--------------