# 数据结构课后题源码

#ifndef LISTQUEUE_H_
#define LISTQUEUE_H_
#include <iostream>

using namespace std;

template<class T>
struct ListQueueNode
{
T data;
};

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;
{
temp = front;
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 = newNode;
}
}

template<class T>
bool ListQueue<T>::DeQueue(T & x)
{
if (front == NULL)
{
cout << "队列为空，无法进行出队列操作！" << endl;
return false;
}
else
{
ListQueueNode<T> * temp;
temp = front;
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 << " ";    //进行调试后，我知道是这里除了问题，
}
os << endl;
l.front = temp;
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;
}