本人写了一个关于用链表表示队列的简单函数,要求实现队列的插入和删除函数,同时本人为了测试程序,重载了操作符<<,但就是在操作符重载的代码出使用指针出了问题,这个问题本人遇到好几次,本人调试后知道问题在哪行代码了,但还是没能通过本人解决该问题,故希望有高手能够来为本人解答一下,下面是源码和错误展示:
首先贴出本人写的代码:
首先贴出本人写的代码:
#ifndef LISTQUEUE_H_
#define LISTQUEUE_H_
#include <iostream>
using namespace std;
template<class T>
struct ListQueueNode
{
T data;
ListQueueNode<T> * link;
};
template<class T>
class ListQueue{
private:
ListQueueNode<T> * front, *rear;
public:
ListQueue();
~ListQueue();
void EnQueue(const T & x);
bool DeQueue(T & x);
friend ostream & operator<<<>(ostream & os, ListQueue<T> & l);
};
template<class T>
ListQueue<T>::ListQueue()
{
front = rear = NULL;
}
template<class T>
ListQueue<T>::~ListQueue()
{
ListQueueNode<T> * temp;
while (front->link != NULL) //删除至队列的最后一个节点
{
temp = front;
front = front->link;
delete temp;
}
}
template<class T>
void ListQueue<T>::EnQueue(const T & x)
{
if (front == NULL)//假如对列为空则该节点成为队列的第一个节点
{
ListQueueNode<T> * newNode = new ListQueueNode < T >;
front = rear = newNode;
newNode->data = x;
}
else//队列不为空
{
ListQueueNode<T> * newNode = new ListQueueNode < T >;
newNode->data = x;
rear->link = newNode;
rear = newNode;
}
}
template<class T>
bool ListQueue<T>::DeQueue(T & x)
{
if (front == NULL)
{
cout << "队列为空,无法进行出队列操作!" << endl;
return false;
}
else
{
ListQueueNode<T> * temp;
temp = front;
front = front->link;
x = temp->data;
delete temp;
}
}
template<class T>
ostream & operator<<<>(ostream & os, ListQueue<T> & l) //友元函数,重载操作符<<
{
while (l.front != NULL) //当队列不为空时继续输出
{
os << l.front->data << " "; //进行调试后,本人知道是这里除了问题,
l.front = l.front->link;
}
return os;
}
#endif
// ex3-25.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "ListQueue.h"
int _tmain(int argc, _TCHAR* argv[])
{
ListQueue<int> a;
int value;
for (int i = 0; i < 5; i++) //初始化,为简单起见,测试程序默认输入5个节点,当然可以随意决定输入节点数,原因是这是链表表示的队列,所以不会产生溢出
{
cout << "请输入插入的第" << i + 1 << "个节点的值: ";
cin >> value;
a.EnQueue(value);
}
cout << a; //确认输入的5个节点值
int temp;
a.DeQueue(temp); //进行出队列测试
cout << a; //检查出队列结果
system("pause");
return 0;
}
同时给出运行结果和调试结果:



求高手解释,感激不尽
解决方案
40
局部变量 l.front替代ListQueue<T>&会调用拷贝构造,导致析构出错的
用一个指针记录下第一个元素的地址,遍历完以后再让l.front重新指回第一个元素就行
上面EnQueue函数里,应该将newNode->link = NULL;否则最后一个元素的link不是NULL会崩
用一个指针记录下第一个元素的地址,遍历完以后再让l.front重新指回第一个元素就行
上面EnQueue函数里,应该将newNode->link = NULL;否则最后一个元素的link不是NULL会崩
#ifndef LISTQUEUE_H_
#define LISTQUEUE_H_
#include <iostream>
using namespace std;
template<class T>
struct ListQueueNode
{
T data;
ListQueueNode<T> * link;
};
template<class T>
class ListQueue{
private:
ListQueueNode<T> * front, *rear;
public:
ListQueue();
~ListQueue();
void EnQueue(const T & x);
bool DeQueue(T & x);
friend ostream & operator<<<>(ostream & os, ListQueue<T> &l);
};
template<class T>
ListQueue<T>::ListQueue()
{
front = rear = NULL;
}
template<class T>
ListQueue<T>::~ListQueue()
{
ListQueueNode<T> * temp;
while (front->link != NULL) //删除至队列的最后一个节点
{
temp = front;
front = front->link;
delete temp;
}
}
template<class T>
void ListQueue<T>::EnQueue(const T & x)
{
if (front == NULL)//假如对列为空则该节点成为队列的第一个节点
{
ListQueueNode<T> * newNode = new ListQueueNode < T >;
front = rear = newNode;
newNode->data = x;
}
else//队列不为空
{
ListQueueNode<T> * newNode = new ListQueueNode < T >;
newNode->link = NULL;
newNode->data = x;
rear->link = newNode;
rear = newNode;
}
}
template<class T>
bool ListQueue<T>::DeQueue(T & x)
{
if (front == NULL)
{
cout << "队列为空,无法进行出队列操作!" << endl;
return false;
}
else
{
ListQueueNode<T> * temp;
temp = front;
front = front->link;
x = temp->data;
delete temp;
}
}
template<class T>
ostream & operator<<<>(ostream & os, ListQueue<T> &l) //友元函数,重载操作符<<
{
ListQueueNode<T> *temp = l.front;
while (l.front != NULL) //当队列不为空时继续输出
{
os << l.front->data << " "; //进行调试后,本人知道是这里除了问题,
l.front = l.front->link;
}
os << endl;
l.front = temp;
return os;
}
#endif