title: "Aborted (core dumped)" error in dequeue method of queue class
body: <p>This queue class's dequeue method doesn't work properly and I'm not sure why. When I run it it says "<strong>* Error in `./a.out': double free or corruption (fasttop): 0x00000000018dfe50 *</strong>", followed by a backtrace and then "Aborted (core dumped)".</p>
  
  <p>At first I was deleting the bottom node directly, but I thought that might be the problem so I switched to deleting them another way.</p>
  
  <p>Here's the dequeue method:</p>
  
  <pre><code>int Queue::dequeue() {
    if (isEmpty()) {
      cout &lt;&lt; "ERROR: Can't dequeue from empty Queue"&lt;&lt; endl;
      return -1;
    } else {
      Node* n = top;
      if (n == NULL){
        bottom = top;
        return 0;
      }
      if (n-&gt;next == NULL){
        delete n;
        bottom = top;
        return 0;
      }
      while(n-&gt;next-&gt;next != NULL){
        n = n-&gt;next;
      }
      bottom = n;
      n = n-&gt;next;
      delete n;
      return 0;
    }
  }
  </code></pre>
  
  <p>And here's the whole file:</p>
  
  <pre><code>  #include &lt;iostream&gt;
    using namespace std;
  
    struct Node {
        int data;
        Node *next;
    };
  
    class Queue {
    public:
        Queue(); //constructor
        ~Queue(); //destructor
        void enqueue(int d); //enqueues node onto top of Queue
        int dequeue(); //dequeues bottom node off of Queue -- returns integer dequeued
        bool isEmpty(); //checks if Queue is empty
        void print(); //print contents of Queue from top to bottom
  
    private:
        Node* top; //points to top node in Queue
        Node* bottom; //points to bottom node in Queue
    };
  
    Queue::Queue() {
        top = NULL;
        bottom = NULL;
    }
  
    Queue::~Queue() {
      while (!isEmpty())
        dequeue();
    }
  
    void Queue::enqueue(int d) {
      Node* temp = new Node;
      temp-&gt;data = d;
  
      temp-&gt;next = top;
      top = temp;
  
      if (temp-&gt;next == NULL){
        delete bottom;
        bottom = temp;
      }
    }
  
    int Queue::dequeue() {
      if (isEmpty()) {
        cout &lt;&lt; "ERROR: Can't dequeue from empty Queue"&lt;&lt; endl;
        return -1; //error
      } else {
        Node* n = top;
        if (n == NULL){
          bottom = top;
          return 0;
        }
        if (n-&gt;next == NULL){
          delete n;
          bottom = top;
          return 0;
        }
        while(n-&gt;next-&gt;next != NULL){
          n = n-&gt;next;
        }
        bottom = n;
        n = n-&gt;next;
        delete n;
        return 0;
      }
    }
  
    bool Queue::isEmpty() {
      return (top==NULL);
    }
  
    //Print Queue from top to bottom
    void Queue::print(){
      Node* n = top;
      while(n != NULL){
        cout &lt;&lt; n-&gt;data &lt;&lt; endl;
        n = n-&gt;next;
      }
      cout &lt;&lt; endl;
    }
  
    int main(){
        Queue* s = new Queue();
        s-&gt;print();
        s-&gt;enqueue(20);
        s-&gt;enqueue(30);
        s-&gt;print();
        s-&gt;enqueue(40);
        s-&gt;enqueue(12);
        s-&gt;print();
        s-&gt;dequeue();
        s-&gt;print();
        s-&gt;dequeue();
        s-&gt;print();
        s-&gt;dequeue();
        delete s;
  
        return 0;
    }
  </code></pre>