尝试运行队列程序时出现分段错误(核心转储) - C++

问题描述 投票:0回答:1

每次尝试在 Linux 上使用 g++ 运行代码时,我都会收到分段错误(核心转储)错误。它编译得很好,但随后发生了这种情况...所有函数(删除、添加和打印)似乎都有同样的问题,我似乎无法弄清楚出了什么问题...请heeeelppp。

#include <iostream>
#include <string>

using namespace std;

//Create a node struct
struct Node {
  int data;
  Node *next;
  Node *prev;
};

class Queue {
private:
  Node *head;
  Node *tail;
  int size;
public:
  Queue();
  ~Queue();
  void add(int d);
  int remove();
  bool isEmpty();
  void printQueue(bool o);
};


//set to NULL
Queue::Queue() {

  head = tail = NULL;
  size = 0;

}

//destructor
//call remove until empty
Queue::~Queue() {

  while (!isEmpty())
    remove();
}

//adds a node with the given data at the back of the queue
void Queue::add(int d) {

  Node *temp = new Node();
  temp->data = d;
  temp->next = NULL;

  if (isEmpty()) {

    //add to head
    head = temp;

  } else {

    //append
    tail->next = temp;
    tail = temp;

    cout << "Added: " << tail->data << endl;
  }

  size++;
}

//removes the node at the head of the queue and returns its data
int Queue::remove() {

  if (isEmpty()) {

    cout << "The queue is empty." << endl;

  } else {

    Node *temp = new Node;
    temp = head;
    int value = head->data;

    //moves pointer to next node
    head = head->next;

    cout << "Removed: " << head->data << endl;

    size--;
    delete temp;
    return value;

  }
}

//determines if the queue is empty
bool Queue::isEmpty() {
  return (size == 0);
}

//prints the contents of the queue from front to back, or front
//to back, depending on the value of the parameter
void Queue::printQueue(bool o) {

  if (isEmpty()) {

    cout << "The queue is empty." << endl;

  } else {

    Node *p = new Node;

    if (o == true) {

      cout << "Printing in front to back:" << endl;

      //print front to back
      while(p != NULL) {
        p = head;
        cout << p->data << " ";
        p = p->next;
      }

    } else if (o == false) {

      cout << "Printing in back to front:" << endl;

      //print back to front
      while (p != NULL) {
        p = tail;
        cout << p->data << " ";
        p = p->prev;
      }
    }
  }
}

int main() {

  Queue q;

  q.add(8);

  return 0;
}

编辑:我对代码做了一些更改...但我仍然遇到相同的错误。我假设我没有正确更新头部和尾部和/或下一个和上一个节点...但我不知道为什么它是错误的或我缺少什么。

#include <iostream>
#include <string>

using namespace std;

struct Node {
  int data;
  Node *next;
  Node *prev;
};

class Queue {
private:
  Node *head;
  Node *tail;
  int size;
public:
  Queue();
  ~Queue();
  void add(int d);
  int remove();
  bool isEmpty();
  void printQueue(bool o);
};

Queue::Queue() {

  head = tail = NULL;
  size = 0;

}

Queue::~Queue() {

  while (!isEmpty())
    remove();
}

void Queue::add(int d) {

  Node *temp = new Node;
  temp->data = d;
  temp->next = NULL;
  temp->prev = tail;

  if (isEmpty()) {

    //add to head
    head = temp;

  } else {

    //append
    tail->next = temp;
    tail = temp;

    cout << "Added: " << tail->data << endl;
  }
  size++;
}

int Queue::remove() {

  if (isEmpty()) {

    cout << "The queue is empty." << endl;

    return 0;

  } else {

    Node *temp = head;
    int value = head->data;

    cout << "Removed: " << head->data << endl;

    //moves pointer to next node
    head = head->next;
    head->prev = NULL;

    size--;
    delete temp;
    return value;
  }
}

bool Queue::isEmpty() {
  return (size == 0);
}

void Queue::printQueue(bool o) {

  if (isEmpty()) {

    cout << "The queue is empty." << endl;

  } else {

    Node *p;

    if (o == true) {

      p = head;

      cout << "Printing in front to back:" << endl;

      //print front to back
      while(p != NULL) {
        cout << p->data << " ";
        p = p->next;
      }

    } else if (o == false) {

      p = tail;

      cout << "Printing in back to front:" << endl;

      //print back to front
      while (p != NULL) {
        cout << p->data << " ";
        p = p->prev;
      }
    }
  }
}

int main() {

  Queue q;

  q.add(9);
  q.add(10);
  q.add(11);
  q.add(12);
  q.add(13);
  q.add(14);
  q.add(15);
  q.add(16);

  q.remove();
  q.remove();

  q.printQueue(true);
  q.printQueue(false);

  return 0;
}
c++ class segmentation-fault queue
1个回答
2
投票

很多问题:

  1. 您有一个双链接的
    Node
    但从未在添加/删除方法中更新其
    prev
    成员。
  2. 您正在跟踪
    Queue
    头/尾,但在添加/删除节点时没有正确更新它们。
  3. printQueue()
    中的正向和反向循环都是错误的,并且会导致任何具有 2 个或更多元素的队列出现无限循环。队列输出应该类似于:

    Node *p = head;
    
    while (p != NULL) 
    {
        cout << p->data << " ";
        p = p->next;
    }
    
  4. 可能存在

    null
    指针在
    remove()
    处的引用,因为此时您已经移动了
    cout << "Removed: " << head->data << endl;
    指针。将
    head
    移到
    head
    之后。
    
    

  5. cout
  6. Queue::remove()
    发生内存泄漏。只要做
    Node *temp = new Node;
  7. Node* temp = head;
  8. Queue::printQueue()
    发生内存泄漏。您不需要在这里分配节点。
    对于空队列,
  9. Node *p = new Node;
  10. 中没有返回值。
    
    
    
编辑

向空列表添加节点时不要忘记初始化

remove()


tail

要从非空列表的头部删除节点,它应该类似于:

if (isEmpty()) { head = temp; tail = temp; }

© www.soinside.com 2019 - 2024. All rights reserved.