I would not use an array to deal with a linked list -- there is no point. Part of the idea of a linked list is that you do not use any particular fixed variable (or array) to refer to "an element" (so call it a node, not an element). You use temporary pointers, and by assigning memory with malloc. Normally, if you did this and then threw the pointer away or reassigned it, this memory would be considered "leaked". But with a linked list, you can find a chunk of memory by using the pointers included in the node structure. So the answer to your last question is you always use -> because *node is always a pointer. Look:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int X;
struct _node *next;
} node;
void addnode (node *head, int value) {
node *new=malloc(sizeof(node)), *ptr=head;
new->X=value; new->next=NULL;
while (ptr->next) ptr=ptr->next; /* find the "tail" */
ptr->next=new;
}
void showlist (node *head) {
node *ptr=head;
while (ptr->next) {
printf ("%d\n",ptr->X);
ptr=ptr->next;
}
printf ("%d\n", ptr->X);
}
int main() {
node *head=malloc(sizeof(node));
int i;
head->X=100;
for (i=0; i<5; i++) addnode(head,(i+2)*100);
showlist(head);
return 0;
}
100
200
300
400
500
600
That's a singly linked list because there is no "previous" pointer. They are great way to improve your understanding of pointers and memory assignment. Notice how head is used; this is the essence of the linked list method.