尝试运行Queue程序时的分段错误(核心转储) - C ++

时间:2015-03-04 19:40:54

标签: c++ class queue core fault

每次我尝试在Linux上使用g ++运行代码时,我都会收到Segmentation fault(core dumped)错误。编译很好,但后来发生了......所有的功能(删除,添加和打印)似乎都有同样的问题,我似乎无法弄清楚出了什么问题......请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;
}

1 个答案:

答案 0 :(得分: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后的cout

  5. Queue::remove() Node *temp = new Node;处的内存泄漏。只需Node* temp = head;
  6. Queue::printQueue() Node *p = new Node;处的内存泄漏。您无需在此处分配节点。
  7. 空队列的remove()没有返回值。
  8. 修改

    将节点添加到空列表时,不要忘记初始化tail

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

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

    Node *temp = head;
    head = head->next;
    if (head) head->prev = NULL;
    size--;
    delete temp;
    if (isEmpty()) tail = NULL;