Compile with warnings turned all the way up. In GCC, use the -Wall flag. If you have another compiler, you will have to check it's documentation.
Code:
$ make list
gcc -Wall -g -std=c99 -lssl -lm -lpthread -lcurses -lefence list.c -o list
list.c:4: warning: ‘struct node’ declared inside parameter list
list.c:4: warning: its scope is only this definition or declaration, which is probably not what you want
list.c:5: warning: ‘struct node’ declared inside parameter list
list.c:6: warning: ‘struct node’ declared inside parameter list
list.c: In function ‘main’:
list.c:27: warning: assignment makes integer from pointer without a cast
list.c:36: warning: passing argument 1 of ‘printList’ from incompatible pointer type
list.c:4: note: expected ‘struct node *’ but argument is of type ‘struct node *’
list.c:22: warning: unused variable ‘ptr_to_head’
list.c: At top level:
list.c:41: error: conflicting types for ‘isEmpty’
list.c:5: note: previous declaration of ‘isEmpty’ was here
list.c:48: error: conflicting types for ‘makeEmpty’
list.c:6: note: previous declaration of ‘makeEmpty’ was here
list.c:53: error: conflicting types for ‘printList’
list.c:4: note: previous declaration of ‘printList’ was here
make: ***
[list] Error 1
Most of those can be fixed by simply moving the struct definition above your function prototypes. After doing that, all that is left is:
Code:
$ make list
gcc -Wall -g -std=c99 -lssl -lm -lpthread -lcurses -lefence list.c -o list
list.c: In function ‘main’:
list.c:27: warning: assignment makes integer from pointer without a cast
list.c:22: warning: unused variable ‘ptr_to_head’
Line 27, you do root->data = NULL. NULL is a pointer type, data is an int. Set it equal to 0.
To do what you want, you need to pass in a pointer to current, i.e. a struct node **, to insert. Something like this (which is untested):
Code:
void insert(struct node **list, int item) // note the double * there
{
// exact same code as your existing insert function, but using *list everywhere instead of list
// list now points to current, so to actually access current, you must use *list
// at the end of the function, update *list, or current in your main function
*list = (*list)->next;
}
// when you call insert, pass in a pointer to current (i.e. the address of current)
insert(¤t, k * 5);
EDIT: I should tell you, you have a somewhat unconventional way of implementing a linked list in C. Having a current node outside your insert function, basically just to track the end of the list is not ideal. Using a "dead" node at the beginning to signify an empty list is also not ideal. I would either just set the root pointer to NULL, or create a special node that perhaps contains other useful stuff. A typical list header node would contain a pointer to the start of the list, perhaps a pointer to the end, and a count of elements in it.