title: Segfault and logic errors
body: <p>I am making a linked list program which utilizes several functions to simplify adding or removing nodes from the list. I believe that my logic is alright with allocating and adding a new node but I am still getting a seg fault. Could you look over my logic and explain what is causing the seg fault? I am very inexperienced with linked lists. Thank you!</p>
  <p>This is my function declarations</p>
  <pre><code>#define SIMPLELL_H
  
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  typedef struct Node
  {
    int data;
    struct Node *next;
  }node_t;
  
  node_t* head;
  
  void printList();
  void append(int num);
  void addFront(int num);
  void deleteList();
  void removeNode(int num);
  int length();
  
  
  
  #endif
  </code></pre>
  <p>This is the function definitions</p>
  <pre><code>void printList(){
    // declare temp to traverse the LL and print each value
    node_t *temporary = head;
    while(temporary != NULL){
      printf(&quot;%d, &quot;, temporary-&gt;data);
      temporary = temporary-&gt;next;
    }
    if (head == NULL){
      printf(&quot;This list is empty.\n&quot;);
    }
  }
  void append(int num){
    //declare temporary pointer to traverse LL
    node_t *tmp = head;
    node_t *prev = NULL;
    //malloc the new node
    node_t *new = malloc(sizeof(node_t));
    if(head == NULL){
      //initialize the new node
      new-&gt;data = num;
      new-&gt;next = NULL;
  
      head-&gt;next = new;
    }
    else{
      //initialize the new node
      new-&gt;data = num;
      new-&gt;next = NULL;
      //loop to traverse to the last value in the node
      while(tmp != NULL){
        prev = tmp;
        tmp = tmp-&gt;next;
      }
      prev-&gt;next = new;
    }
  }
  void addFront(int num){
    //allocate memory for new node and allocate local variable equal to head
    node_t *new = malloc(sizeof(node_t));
    new-&gt;data = num;
    new-&gt;next = head;
    head = new;
  }
  void deleteList(){
    node_t *tmp = head;
    node_t *prev = NULL;
    while(tmp != NULL){
      prev = tmp;
      tmp = tmp-&gt;next
      free(prev);
    }
  }
  void removeNode(int num){
  //temp is used to traverse the ll until I find the node with the value
  //then the previous value is linked to the next value removing the middle value.
    node_t *temp = head;
    node_t *prev = NULL;
    if(head == NULL){
      printf(&quot;List is not initialized\n&quot;);
      return;
    }
    while(temp-&gt;data != num){
      prev = temp;
      temp = temp-&gt;next;
      if (temp-&gt;data == num){
        prev-&gt;next = temp-&gt;next;
        free(prev);
      }
      else{
        printf(&quot;Value is not in the list\n&quot;);
        return;
      }
  
  }
  int length(){
    int length = 0;
    node_t *temp = head;
    while (temp != NULL){
      length += 1;
      temp = temp-&gt;next;
    }
    return length;
  }
  </code></pre>
  <p>This is the driver function which I should not have to change</p>
  <pre><code>#include &quot;SimpleLL.h&quot;
  #include &quot;SimpleLL.c&quot;
  int main()
  {
    /*head is a global variable*/
    head = NULL;
    int size =0;
    removeNode(1);
    append(2);
    append(3);
    addFront(1);
    append(4);
    printList();
    size = length();
    printf(&quot;size now %d\n&quot;,size);
    removeNode(8);
    printList();
    removeNode(1);
    printList();
    removeNode(4);
    printList();
    deleteList();
    printList();
    size = length();
    printf(&quot;size after deletion %d\n&quot;,size);
  
    return 0;
  }
  </code></pre>