Beating my head against my desk trying to understand how linked lists work.
(all those pointers pointing to pointers, then pointing to the same pointer that
started the pointing....)
The tutorial on this site helped.
I wrote the code below just for practice.
I chopped it to the down until it would compile.
When I execute the program I get "Segmentation fault".
I suspect the problem is in how I'm trying to get the program to print out the
lists. ????
ThanksCode:/*linked lists- here goes*/ #include <stdio.h> #include <stdlib.h> #include <string.h> struct list { char name[80]; struct list *next; } *new, *head, *current, *marker; int main(void) { new = NULL; /*set all pointers to NULL*/ head = NULL; current = NULL; marker = NULL; /*add the first list element*/ new = (struct list*)malloc(sizeof(struct list)); new -> next = head; head = new; strcpy(new -> name, "Amy"); /*add another element*/ new = (struct list*)malloc(sizeof(struct list)); new -> next = marker -> next; marker -> next = new; strcpy(new -> name, "Samantha"); /*add a third element*/ new = (struct list*)malloc(sizeof(struct list)); new -> next = marker -> next; marker -> next = new; strcpy(new -> name, "Angela"); /*create the final element*/ current = head; while (current -> next != NULL) { current = current -> next; } new = (struct list*)malloc(sizeof(struct list)); current -> next = new; new -> next = NULL; strcpy(new -> name, "Leanne"); /*print out all lists*/ current = head; while (current != NULL) { printf("%s\n", current -> name); current = current -> next; } printf("\n"); return(0); }