求大佬帮忙
队列的链表实现
头文件
#ifndef LINKQUEUE_H
#define LINKQUEUE_H
#include <iostream>
using namespace std;
/***链式队列,front指向队列附加头结点,rear指向最后一个元素,
和动态数组表示的队列不同,它的front指向第一个元素,rear指向下一个要插入的位置*/
template <class T>
struct Node
{
T data;
Node<T> *next;
Node(Node<T> *p = NULL) :next(p) {}
Node(const T& x, Node<T> *p = NULL) :data(x), next(p) {}
};
template <class T>
class LinkQueue
{
public:
LinkQueue() :front(new Node<T>) { rear = front; }
~LinkQueue() { makeEmpty(); }
void makeEmpty();
bool EnQueue(const T& x);
bool DeQueue(T& x);
bool IsEmpty() { return front == rear; }
bool getFront(T& x)const;
int getSize()const;
friend ostream& operator<< <T>(ostream&, LinkQueue<T>&);
protected:
Node<T> *front, *rear;
};
template <class T>
void LinkQueue<T>::makeEmpty()
{
Node<T> *p;
while (front->next)
{
p = front->next;
front->next = p->next;
delete p;
}
front = rear = NULL;
};
template <class T>
bool LinkQueue<T>::EnQueue(const T& x)
{
rear->next = new Node<T>(x);
if (rear == NULL) return false;
rear = rear->next;
return true;
};
template <class T>
bool LinkQueue<T>::DeQueue(T& x)
{
if (IsEmpty()) return false;
Node<T> *p = front->next;
front->next = p->next;
x = p->data;
//<span style = "white-space:pre"> < / span>if (rear == p)
// <span style = "white-space:pre"> < / span> rear = front;
delete p;
return true;
};
template <class T>
bool LinkQueue<T>::getFront(T& x)const
{
if (IsEmpty()) return false;
x = front->next->data;
return true;
};
template <class T>
int LinkQueue<T>::getSize()const
{
Node<T> *p = front;
int count = 0;
while (p != rear)
{
count++;
p = p->next;
}
return count;
};
template <class T>
ostream& operator<<(ostream& out, LinkQueue<T>& q)
{
if (q.IsEmpty())
{
out << "The queue is Empty!" << endl;
return out;
}
Node<T> *p = q.front->next;
int i = 0;
while (p)
{
cout << (++i) << ":" << p->data << endl;
p = p->next;
}
return out;
};
#endif
源文件#include <iostream>
//#include "SeqQueue.h"
#include "LinkQueue.h"
using namespace std;
int main()
{
//SeqQueue<int> q;
LinkQueue<int> q;
int a;
cin >> a;
while (a != 0)
{
q.EnQueue(a);
cin >> a;
}
int x;
for (int i = 1; i <= 5; i++)
{
q.DeQueue(x);
cout << "第" << i << "次出列:" << x << endl;
}
cout << "出列后的队列:" << endl;
cout << q;
system("pause");
}