我的问题是它返回了-1073741819。我应该怎么做才能更快地编译它?
#include "Queue.h"
#include <iostream>
Queue::Queue()
{
topPtr = NULL;
tailPtr = NULL;
}
bool Queue::isEmpty()
{
if(topPtr == NULL && tailPtr == NULL)
return true;
else
return false;
}
void Queue::enqueue(int data)
{
Node *newNodePtr = new Node;
newNodePtr->data = data;
if(topPtr == NULL)
{
topPtr = tailPtr = newNodePtr;
tailPtr->nextPtr = NULL;
}
else
{
tailPtr->nextPtr = newNodePtr;
tailPtr = newNodePtr;
tailPtr->nextPtr = NULL;
}
}
int Queue::dequeue()
{
if (isEmpty())
{
cout << "empty" <<endl;
}
else
{
int dataToReturn = topPtr->data;
Node *nodeToDeletePtr = topPtr;
dataToReturn = nodeToDeletePtr->nextPtr;
topPtr = topPtr->nextPtr;
delete nodeToDeletePtr;
return dataToReturn;
}
}
#ifndef QUEUE_H
#define QUEUE_H
struct Node
{
int data;
Node *nextPtr;
};
class Queue
{
public:
Queue();
void enqueue(int data);
int dequeue();
bool isEmpty();
private:
Node *topPtr;
Node *tailPtr;
};
#endif
#include <iostream>
#include <string.h>
#include <cstdlib>
#include "Queue.h"
using namespace std;
int main(int argc, char const *argv[])
{
Queue integers;
string seriesIntegers;
cout << "Enter integers: ";
getline(cin, seriesIntegers);
char *seriesIntegersCStr = new char[seriesIntegers.length() + 1];
strcpy(seriesIntegersCStr, seriesIntegers.c_str());
char *tokens = strtok(seriesIntegersCStr, " ");
while(tokens != NULL)
{
integers.enqueue(atoi(tokens));
tokens = strtok(NULL, " ");
}
while(!integers.isEmpty())
{
cout << " " << integers.dequeue() << "\n";
}
}
答案 0 :(得分:2)
错在其中
tailPtr
重置为null。isEmpty
成员错误地报告了队列仍然有数据,因为两个条件(tailPtr == nullptr
)中的一个都不为真,因此队列不是“空的” “尽管如此。main
中的while循环超出了队列的末端。int
。至少这应该引发异常。理想情况下,一开始就不允许这样做。Queue
应该在破坏时进行自我清理,而不是使节点链处于非空状态。Queue
应该符合rule-of-three,方法是为copy-ctor和赋值运算符定义适当的操作,或者将它们指定为delete(如我如下)解决了所有这些问题,并进行了一些其他修改,包括进入现代C ++的真实世界以利用字符串流读取整数而不是strtok
,并且像以前一样泄漏内存,请参见下面的代码:
#include <iostream>
#include <sstream>
#include <string>
struct Node
{
int data;
Node *nextPtr;
Node(int val, Node *next = nullptr)
: data(val)
, nextPtr(next)
{
}
};
class Queue
{
public:
Queue();
virtual ~Queue();
Queue(const Queue&) = delete;
Queue& operator =(const Queue&) = delete;
void enqueue(int data);
int dequeue();
bool isEmpty() const;
private:
Node *topPtr;
Node *tailPtr;
};
Queue::Queue()
: topPtr(nullptr)
, tailPtr(nullptr)
{
}
Queue::~Queue()
{
while (topPtr)
dequeue();
}
bool Queue::isEmpty() const
{
return topPtr == nullptr;
}
void Queue::enqueue(int data)
{
Node *newNodePtr = new Node(data);
if (topPtr == NULL)
{
topPtr = newNodePtr;
}
else
{
tailPtr->nextPtr = newNodePtr;
}
tailPtr = newNodePtr;
}
int Queue::dequeue()
{
if (isEmpty())
{
throw std::runtime_error("'dequeue' called on an already-empty queue");
}
// get top data
int dataToReturn = topPtr->data;
// advance top, retaining pointer to old node
Node *tmp = topPtr;
topPtr = topPtr->nextPtr;
// now delete the node
delete tmp;
// and reset tail if top hit end-of-queue
if (topPtr == nullptr)
tailPtr = nullptr;
// finally, return data
return dataToReturn;
}
int main()
{
std::cout << "Enter integers: ";
std::string line;
if (getline(std::cin, line) && !line.empty())
{
Queue integers;
std::istringstream iss(line);
int value;
while (iss >> value)
integers.enqueue(value);
while (!integers.isEmpty())
std::cout << integers.dequeue() << '\n';
}
}
输入
1 2 3 4 5
输出
Enter integers: 1 2 3 4 5
1
2
3
4
5