【发布时间】:2015-10-26 03:05:22
【问题描述】:
对于使用 c++ 实现的链表实现的队列,我的入队和出队遇到了一些问题。我的老师说模板是禁止使用的,我不能更改公共和私人功能,因为他给了我们它们。我不断收到分段错误。我真的不明白我做错了什么。我还包含了标头以及入队和出队函数。
标题
const int MAX_STRING = 6;
typedef char Element300[MAX_STRING + 1];
class Queue300
{
public:
Queue300();
Queue300(Queue300&);
~Queue300();
void enQueue300(const Element300);
void deQueue300(Element300);
void view300();
private:
struct Node300;
typedef Node300 * NodePtr300;
struct Node300
{
Element300 element;
NodePtr300 next;
};
NodePtr300 front, rear;
};
入队
void Queue300::enQueue300(const Element300 input)
{
NodePtr300 temp = NULL;
temp = new (std::nothrow) Node300;
if (temp == NULL)
{
cerr << "The queue is full, could not add(enqueue) any more elements." << endl;
}
else if (front == NULL && rear == NULL)
{
strcpy(temp->element, input);
rear = temp;
rear->next = NULL;
front = rear;
temp = NULL;
}
else
{
strcpy(temp->element, input);
temp = rear->next;
rear = temp;
rear->next = NULL;
temp = NULL;
}
}
出队
void Queue300::deQueue300(Element300 input)
{
NodePtr300 temp = NULL;
if (rear == NULL && front == NULL)
{
cerr << "The queue is already empty, could not delete(dequeue) any more elements." << endl;
}
else if (front == rear)
{
strcpy(temp->element, input);
temp = front;
delete temp;
temp = NULL;
front = NULL;
rear = NULL;
}
else
{
strcpy(temp->element, input);
temp = front;
front = front->next;
temp->next = NULL;
delete temp;
temp = NULL;
}
}
【问题讨论】:
标签: c++ linked-list queue