I am reading Skiena's "The Algorithm Design Manual" for personal learning, and I am trying to implement the linked list in C.
How do I use the insert_list() function and then print out my list to verify the values?
/* definition of linked list */
typedef struct list {
int item;
struct list *next;
} list;
/* insert item into list */
void insert_list(list **l, int x) {
list *p;
p = malloc(sizeof(list));
p->item = x;
p->next = *l;
*l = p;
}
I wrote a main function to try and figure out how to use insert_list():
int main(void) {
struct list *root;
struct list *traverse;
root = (struct list *)malloc(sizeof(struct list));
root->next = 0;
root->item = 5;
traverse = root;
insert_list(traverse, 1); /* this is the problem */
printf("%d\n", root->item);
}
I receive an error at compilation with gcc:
expected 'struct list **' but argument is of type 'struct list *
How do I properly use this function to insert items into and then print my list? Without the insert_list() call, it prints root->item as 5, as expected.