title: Deleting the front element in a linked list
body: <p>I'm trying to write a function that deletes the front element of my linked list and sets the head pointer to the next element after the deleted one.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>void LinkedList::delete_front(){
      if(head != NULL){
              if(head-&gt;next != NULL){
                      ListNode *tmp = head;
                      delete head;
                      head = tmp-&gt;next;
              }
              else {delete head; head = NULL;}
      }
      size--;
  }
  </code></pre>
  
  <p>And here are my class definitions:</p>
  
  <pre><code>class ListNode{
  
      public:
              Item data;
              ListNode *next;
  };
  class LinkedList{
  
      private:
              ListNode *head;
              int size;
  
      public:
              LinkedList();
              ~LinkedList(); 
              bool empty();
              void insert_front(Item i);
              void insert_back(Item i);
              void delete_front();
              void delete_back();
              void print();
  };
  </code></pre>
  
  <p>Annddddd.....this is the problem, when running in valgrind this error pops up right before the first delete_front() call:</p>
  
  <pre><code>==4738== Invalid read of size 8
  ==4738==    at 0x400B3C: LinkedList::delete_front() (in /home/jon/jball2_lab06/linkedlist)
  ==4738==    by 0x400E59: main (in /home/jon/jball2_lab06/linkedlist)
  ==4738==  Address 0x5a03f98 is 8 bytes inside a block of size 16 free'd
  ==4738==    at 0x4C2A4BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==4738==    by 0x400B37: LinkedList::delete_front() (in /home/jon/jball2_lab06/linkedlist)
  ==4738==    by 0x400E59: main (in /home/jon/jball2_lab06/linkedlist)
  </code></pre>