【发布时间】:2012-11-19 10:55:21
【问题描述】:
我正在实现一个队列数据结构,但我的应用程序崩溃了。我知道我在队列类的节点指针 front 或 Front() 方法上做错了
#include <iostream>
using namespace std;
class Node
{
public:
int get() { return object; };
void set(int object) { this->object = object; };
Node * getNext() { return nextNode; };
void setNext(Node * nextNode) { this->nextNode = nextNode; };
private:
int object;
Node * nextNode;
};
class queue{
private:
Node *rear;
Node *front;
public:
int dequeue()
{
int x = front->get();
Node* p = front;
front = front->getNext();
delete p;
return x;
}
void enqueue(int x)
{
Node* newNode = new Node();
newNode->set(x);
newNode->setNext(NULL);
rear->setNext(newNode);
rear = newNode;
}
int Front()
{
return front->get();
}
int isEmpty()
{
return ( front == NULL );
}
};
main()
{
queue q;
q.enqueue(2);
cout<<q.Front();
system("pause");
}
【问题讨论】:
-
写完代码之后,接下来要做的就是调试了。您可以使用调试器,也可以添加打印语句来跟踪其行为。这是每个程序员都需要具备的技能。混过去,逐渐缩小问题的范围。你会开始掌握它的窍门,下次它会更快。
标签: c++ data-structures queue crash