title: C++ LinkedList Queue Implementation and Destructor Error:"Aborted (Core Dumped)"
body: <p>I'm trying to learn about C++ and I am having issues with memory management and the concept of destructors. I have tried to write a Queue implementation using Linked Lists, and I am receiving "Aborted (Core Dumped)" error messages when I try to delete my linked list nodes in my destructor method (bolded). If anyone knows what I'm doing wrong please let me know!</p>
  
  <p>Here's the code for my destructor method:</p>
  
  <pre><code>template &lt;class T&gt;
  Queue&lt;T&gt;::~Queue(){
    Node&lt;T&gt; *currHead = head;
    while(currHead != NULL){
      Node&lt;T&gt; *nextHead = currHead-&gt;next;
      delete currHead;
      currHead = nextHead;
    }
    Node&lt;T&gt; *currTail = tail;
    while(currTail != NULL){
      Node&lt;T&gt; *nextTail = currTail-&gt;next;
      delete currTail;
      currTail = nextTail;
    }
  }
  </code></pre>
  
  <p>And for reference here's my full linkedlist queue implementation:</p>
  
  <pre><code>template &lt;class T&gt;
  class Node{
  public:
    T data;
    Node&lt;T&gt; *next=NULL;
  };
  
  template &lt;class T&gt;
  class Queue{
  public:
    Queue();
    ~Queue();
    void push(T);
    T pop();
    int size=0;
    Node&lt;T&gt; *head=NULL;
    Node&lt;T&gt; *tail=NULL;
  };
  
  template &lt;class T&gt;
  Queue&lt;T&gt;::Queue(){}
  
  template &lt;class T&gt;
  Queue&lt;T&gt;::~Queue(){
    Node&lt;T&gt; *currHead = head;
    while(currHead != NULL){
      Node&lt;T&gt; *nextHead = currHead-&gt;next;
      delete currHead;
      currHead = nextHead;
    }
    Node&lt;T&gt; *currTail = tail;
    while(currTail != NULL){
      Node&lt;T&gt; *nextTail = currTail-&gt;next;
      delete currTail;
      currTail = nextTail;
    }
  }
  
  template &lt;class T&gt;
  void Queue&lt;T&gt;::push(T data){
    Node&lt;T&gt; *node = new Node&lt;T&gt;;
    node-&gt;data = data;
    if(head == NULL){
      head = node;
    }else{
      tail-&gt;next = node;
    }
    tail = node;
    size++;
  }
  
  template &lt;class T&gt;
  T Queue&lt;T&gt;::pop(){
    if(size == 0){
      throw "Empty Queue";
    }else{
      Node&lt;T&gt; *oldHead = head;
      T oldData = oldHead-&gt;data;
      head = head-&gt;next;
      size--;
      delete oldHead;
      return oldData;
    }
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I've also tried the following definition for the destructor but I am getting the same error:</p>
  
  <pre><code>template &lt;class T&gt;
  Queue&lt;T&gt;::~Queue(){
  
    while(head != NULL){
      Node&lt;T&gt; *currHead = head;
      head = head-&gt;next;
      delete currHead;
    }
  
    while(tail != NULL){
      Node&lt;T&gt; *currTail = tail;
      tail = tail-&gt;next;
      delete currTail;
    }
  }
  </code></pre>