My current struct is:
typedef char AirportCode[4];
typedef struct node{
AirportCode airport;
struct node *next;
}Node;
My idea for how the function should start is this:
Node *copy(Node *list) {
int count = 0;
while (list != NULL){
count++;
list = list->next;
}
}
Now we know how long the original list is, but the reason why I am so stumped is because I have no idea how to seperatly allocate the memory for each individual node that we have to copy to the second list.
You allocate a new node with malloc:
Then you can change stuff inside your new Node with
It might be easier to clone with recursion rather than loop, if you use a loop, unless you like to do a lot of pointer manipulation and keep track of many nodes at a time (similar to reversing a linked list in place), use a stack.
So it would be something like this:
I haven't tested it but this should do the job :