title: Inserting at head of linked list?
body: <p>Im trying to insert a node at the head of a linked list, and I'm not exactly sure why this function isn't working. I thought it was a fairly simple task, but I seem to be missing something here. I've also included my structs and a portion of main so you can get a clearer understanding of the code. Thanks</p>
  <pre><code>typedef struct node
  {
    struct node *next;
    int data;
  } node;
  
  typedef struct LinkedList
  {
    node *head;
    node *tail;
  } LinkedList;
  
  LinkedList *create_list(void)
  {
    return calloc(1, sizeof(LinkedList));
  }
  
  node *create_node(int data)
  {
    node *ptr = calloc(1, sizeof(node));
    ptr-&gt;data = data;
  
    return ptr;
  }
  
  void head_insert(LinkedList *list, int data) // problem
  {
    node *newHead = create_node(data);
    newHead-&gt;next = list-&gt;head;
  }
  
  void print_list_helper(node *head)
  {
    if (head == NULL)
      return;
    printf(&quot;%d%c&quot;, head-&gt;data, (head-&gt;next == NULL) ? '\n' : ' ');
    print_list_helper(head-&gt;next);
  }
  
  void print_list(LinkedList *list)
  {
    if (list == NULL || list-&gt;head == NULL)
      return;
    print_list_helper(list-&gt;head);
  }
  
  int main(void)
  {
    LinkedList *list = create_list();
    head_insert(list, 8);
    print_list(list); // print linked list function
  
    return 0;
  }
  </code></pre>
  <p>So I created a new Node, and set node-&gt;next to the head of the list. Im not sure what else im missing here. I have another function that prints the lists, thats why the function is void.</p>