title: ANSI C S-Linked List - deleteLast() not functioning correctly
body: <p>When trying to run my deleteLast() function twice (to get an empty list) on a linked list with two nodes, I am running into a problem. The code compiles and runs, but when I call traverse() on my empty linked list, I get an infinite while loop, and I cannot determine why.</p>
  
  <p>Oddly enough, if I call deleteFirst() twice instead of deleteLast(), the program runs and terminates properly.</p>
  
  <p>Below is the code for my methods:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stddef.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  struct NODE
  {
      struct NODE *link;
      int value;
  };
  
  typedef struct NODE Node;
  
  
  
  /* Deletes the first item in the list and returns next item */
  Node *deleteFirst(Node **ptrToHeadPtr)
  {
      Node *current;
  
      // If list is empty do nothing
      if (*ptrToHeadPtr == NULL)
          return NULL;
  
      else
      {
          current = *ptrToHeadPtr;
          *ptrToHeadPtr = current-&gt;link;
          free(current);
      }
  
      return *ptrToHeadPtr;
  }
  
  /* Inserts a new Node to the end of the list and returns it */
  Node *insertLast(Node **ptrToHeadPtr, int val)
  {
      Node *current, *lastNode;
      lastNode = (Node *)malloc( sizeof (Node) );
  
      // Check if malloc was successful
      if(!lastNode) return NULL;
  
      lastNode-&gt;value = val;
      lastNode-&gt;link = NULL;
  
      if (*ptrToHeadPtr == NULL)
          *ptrToHeadPtr = lastNode;
  
      else
      {
          current = *ptrToHeadPtr;
  
          // Walk to the end of the list
          while(current-&gt;link != NULL)
              current = current-&gt;link;
  
          // Insert new item at the end of the list
          current-&gt;link = lastNode;
      }
      return lastNode;
  }
  
  /* Deletes the last Node in the list and returns*/
  Node *deleteLast(Node **ptrToHeadPtr)
  {
      Node *current, *previous;
  
      /* If list is empty do nothing */
      if (*ptrToHeadPtr == NULL)
          return NULL;
  
      current = *ptrToHeadPtr;
      previous = NULL;
  
      /* If list has one item delete it and return NULL */
      if (current-&gt;link == NULL)
      {
          *ptrToHeadPtr == NULL;
          free(current);
          return NULL;
      }
      else
      {
          /* Walk to the end of the list */
          while (current-&gt;link != NULL)
          {
              previous = current;
              current = current-&gt;link;
          }
  
          previous-&gt;link = NULL;
          free(current);
          return previous;
      }       
  }
  
  
  /* Traverses the list, printing the value of each Node */
  void traverse(Node*p)
  {
      while( p!= NULL )
      {
          printf("%d ",p-&gt;value);
          p=p -&gt;  link;
      }
  }
  
  /* Walks through the linked list, freeing memory of each Node */
  void freeList(Node *p)
  {
      Node *temp;
      while( p != NULL )
      {
          temp = p;
          p = p-&gt; link;
          free(temp);
      }
  }
  
  
  int main()
  {
      Node *headPtr = NULL;
  
      insertLast( &amp;headPtr, 33 );
      insertLast( &amp;headPtr, 35 );
  
      traverse(headPtr);
      printf("\n");
  
      deleteFirst ( &amp;headPtr );
      traverse(headPtr);
      printf("\n");
  
      deleteLast ( &amp;headPtr );
  
      traverse(headPtr);
      freeList(headPtr);
      return 1;
  }
  </code></pre>