Array to Linked List C
I am trying to convert an array to an linked list. so basically, im gonna have a structure called "head" which will be t开发者_开发百科he first element and node, which will be the other elements. any ideas so i can it started?
I don't see any solution simpler than just iterating through the array and appending the elements to the list.
The standard way to implement linked lists in C is with a single node structure containing a data member and a next pointer. Every time you want a new node, malloc
space for it and set the next pointer of the last node in the list to point to it. The last node's next pointer should point to NULL
.
You only have to hold onto a regular pointer to the first element. That's your head pointer.
Without using malloc you won't be able to easily add a new node to store data in so its best to just use the array to avoid the mess (but why can't you use malloc now?)
精彩评论