queue.h头文件 代码:
#ifndef QUEUE_CLASS
#define QUEUE_CLASS
#include
#include
#include “LinkedList.h”
using namespace std;
template
class Queue
{
private:
LinkedList
public:
Queue(void);
void QInsert(const T& elt);
T QDelete(void);
T QFront(void);
int QLength(void) const;
int QEmpty(void) const;
void QClear(void);
};
template
Queue
{}
template
int Queue
{
return queueList.ListSize();
}
template
int Queue
{
return queueList.ListEmpty();
}
template
void Queue
{
queueList.ClearList();
}
template
void Queue
{
queueList.InsertRear(elt);
}
template
T Queue
{
if (queueList.ListEmpty())
{
cerr << “Calling QDelete for an empty queue!” << endl;
exit(1);
}
return queueList.DeleteFront();
}
template
T Queue
{
if (queueList.ListEmpty())
{
cerr << “Calling QFront for an empty queue!” << endl;
exit(1);
}
queueList.Reset();
return queueList.Data();
}
#endif
使用:
#include “queue.h”
#include
using namespace std;
int main()
{
Queue
cout<<“输入6个数值用来创建队列:”;
int a=0;
for(int i=0;i<=5;i++)
{
cin>>a;
A.QInsert(a);
}
cout<<“创建成功!”<<endl;
cout << “显示队列中的所有元素为:” ;
while(!A.QEmpty())
{
cout << A.QFront() << " ";
A.QDelete();
}
cout<<endl;
cout<<“判断队列是否为空:”<<(A.QEmpty()==0?“为空”:“不为空”)<<endl;
cout << endl;
cout<<“开始清空队列!”<<endl;
A.QClear();
cout<<“队列成功清空!”<<endl;
return 0;
}