title: Clearing A Dynamic Linked List with a Single Stored Element Crashes Program
body: <p>For my scholastic assignment I have been given the task of creating a Dynamic Set ADT using linked list. My program is functioning properly, but when I attempt to clear a Set which only has a single element (ergo, single node)my program crashes.</p>
  
  <p>Here is how my linked list is defined:</p>
  
  <pre><code>typedef struct node{
  
      int value;
      struct node *next;
  
  }Node;
  
  typedef struct List{
  
      Node *head;
      Node *tail;
  }*SetList;
  </code></pre>
  
  <p>And here are the relevant functions:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include "List.h"
  
  SetList ListCreate(){
  
      SetList lptr;
      lptr = malloc(sizeof(SetList));
      lptr-&gt;head = NULL;
      lptr-&gt;tail = NULL;
      return lptr;
  }
  
  SetList ListAdd(SetList lptr, int x){
  
      if(List_Is_Element_Of(x, lptr)){
              puts("Value already exists, enter a unique value");
      }
      else{
          Node *new = malloc(sizeof(Node));
          new-&gt;value = x;
          new-&gt;next = NULL;
  
          if(lptr-&gt;head == NULL){
              lptr-&gt;head = new;
              lptr-&gt;tail = new;
          }
          else{
              lptr-&gt;tail-&gt;next = new;
              lptr-&gt;tail = new;
          }
      }
          return lptr;
  }
  
  
  SetList ListRemove(SetList lptr, int x){
  
      if(lptr == NULL){
              puts("Memory Allocation Failed. Shutting down.");
              exit(EXIT_FAILURE);
      }
      else if(!List_Is_Element_Of(x, lptr)){
          puts("Value is not in set");
      }
      else if(lptr-&gt;head == NULL){
          puts("Set is empty. Cannot remove that which does not exist");
      }
      else{
          if(lptr-&gt;head-&gt;value == x){
          Node *tempnode = malloc(sizeof(Node));
          tempnode =  lptr-&gt;head;
          lptr-&gt;head = tempnode-&gt;next;
          free(tempnode);
          }
          else{
          Node *tempnode = malloc(sizeof(Node));
          tempnode =  lptr-&gt;head;
          Node *tempnode2 = malloc(sizeof(Node));
          tempnode2 = tempnode;
          while(tempnode-&gt;value != x){
              tempnode2 = tempnode;
              tempnode = tempnode-&gt;next;
          }
          tempnode2-&gt;next = tempnode-&gt;next;
          free(tempnode);
          }
      }
      return lptr;
  }
  
  SetList ListClear(SetList lptr){
  
      SetList templist = ListCreate();
      templist = lptr;
      while(templist-&gt;head!= NULL){
          ListRemove(templist, templist-&gt;head-&gt;value);
          templist-&gt;head = templist-&gt;head-&gt;next;
      }
      free(templist);
      return lptr;
  }
  </code></pre>
  
  <p>I am not very experienced when it comes to pointers so I'm not entirely sure where the issue arises from. Removing a list with a single element stored works, but clearing it does not.</p>
  
  <p>Thank you in advance for reading.</p>