C++删除错误

C++ delete error

本文关键字:错误 删除 C++      更新时间:2023-10-16

我是C++的新手,正在尝试编写一个基于链表的队列。我的测试程序运行良好,直到我在出列函数中添加了delete。然后我得到错误:对象0x7f8a61403a00的malloc:***错误:释放的指针未分配

如有任何帮助,我们将不胜感激。

#include <string>
#include "queue.h"
#include <iostream>
using namespace std;

Queue::Queue() { // Constructs a new empty queue.
    current_size = 0;   // number of elements in queue
    front_p = NULL; // first element in queue
    back_p = NULL;  // last element in queue
}
Queue::Queue( const Queue& q ) {// Copy constructor.
    // nothing to copy if queue empty
    if (q.current_size == 0) {
        return;
    }
    // queue not empty
    else {
        node * p = q.front_p;
        node * n;
        // assign front pointer
        n = new node(p -> data, NULL);
        front_p = n;
        p = p -> next;
        // middle elements
        while (p -> next != NULL) {
            n -> next = p;
            n = new node(p -> data, NULL);
            p = p -> next;
        }
        // assign back pointer
        n = new node(p -> data, NULL);
        back_p = n;
        current_size = q.current_size;
    }
}
void Queue::enqueue( int item ) { // Enqueues <item> to back
    node * n = new node(item, NULL);
    // first item in queue, front & back ptrs point to same element
    if (back_p == NULL) {
        front_p = n;
        back_p = n;
    }
    else {
        back_p -> next = n;
        back_p = n;
    }
    current_size++;
}
int Queue::dequeue() { // removes and returns the front item.
    // empty queue
    if (current_size == 0) {
        return -1;
    }
    int item = front();
    node * p = front_p;
    if (current_size == 1) {
        front_p = NULL;
        back_p = NULL;
        delete p;
        current_size--;
        return item;
    }
    else {
        front_p = front_p -> next;
        delete p;
        current_size--;
        return item;
    }
}

测试文件

//test  file
#include <iostream> 
#include "queue.h"
using namespace std;
int main(void) {
    Queue q1;
    cout << "Create a new queue q1" << endl;
    cout << "Size of q1 t" << q1.size() << endl;
    cout << "Is q1 empty? t" << q1.empty() << endl;
    cout << endl << endl;
    cout << "enqueue t1,2,3,4,5" << endl;
    q1.enqueue(1);
    q1.enqueue(2);
    q1.enqueue(3);
    q1.enqueue(4);
    q1.enqueue(5);
    cout << "front of q1 t" << q1.front() << endl;
    cout << "size of q1 t" << q1.size() << endl << endl;
    cout << "q2 is a deep copy of q1" << endl;
    Queue q2(q1);
    cout << "front of q2 t" << q2.front() << endl;
    cout << "size of q2 t" << q2.size() << endl << endl;
    cout << "removed 4 from q1" << endl;
    q1.remove(4);
    cout << "removed 2 from q2" << endl;
    q2.remove(2);
    cout << endl;
    cout << "print out remaining elements of q1" << endl;
    int N = q1.size();
    for(int i=0; i < N; i++) {
        cout << q1.dequeue() << " ";    
    }
    cout << endl << endl;
    cout << "print out remaining elements of q2" << endl;
    N = q2.size();
    for(int i=0; i < N; i++) {
        cout << q2.dequeue() << " ";
    }
    cout << endl;
}

头文件

    class Queue
    {
    public:
        Queue(); // Constructs a new empty queue.
        Queue( const Queue& q );// Copy constructor.
        ~Queue();// Destructor.
        void enqueue( int item ); // Enqueues <item>.
        int dequeue();  // Dequeues the front item.
        int front();  // Returns the front item without dequeuing it.
        bool empty();  // Returns true iff the queue contains no items.
        int size();  // Returns the current number of items in the queue.
        bool remove(int item); // If <item> occurs in the queue, removes the 
      // first occurrence of <item> and returns true; otherwise returns false.
    private:
        class node  // node type for the linked list 
        {
       public:
           node(int new_data, node * next_node){
              data = new_data ;
              next = next_node ;
           }
           int data ;
           node * next ;
    };
    node * front_p ; // pointer to the (node containing the) next item 
              //  which to be dequeud, or NULL if the queue is empty.
    node * back_p ; // pointer to the (node containing the) last item 
              // which was enqueued, or NULL if the queue is empty.
    int current_size ; // current number of elements in the queue.
};

代码还有其他问题,但最直接的问题是您的复制构造函数已损坏。

在这个周期

    while (p -> next != NULL) {
        n -> next = p;
        n = new node(p -> data, NULL);
        p = p -> next;
    }

p是指向队列q的元素的指针。同时,n队列的一个元素。通过执行n -> next = p;,您正在使新的队列节点链接到源队列节点链中。这创建了一个完全无意义的节点链接结构,该结构稍后会分解。