1014: 链式队列
Description
已知链式队列类的定义、部分实现及主函数代码(勿改动)如下:
template <class T>
struct Node
{
T data;
Node<T> *next;
};
template <class T>
class LinkQueue
{
public:
LinkQueue( ); //构造函数,初始化一个仅有头结点的空队列
~LinkQueue( ); //析构函数,释放链队列中各结点的存储空间
void EnQueue(T x); //将元素x入队
T DeQueue( ); //将队头元素出队,若队列为空,抛出异常“Downflow”,否则函数返回值为队头元素值
T GetQueue( ); //取链队列的队头元素,若队列为空,抛出异常“Downflow”,否则函数返回值为队头元素值
bool Empty( ); //判断链队列是否为空,为空返回true,否则返回false
private:
Node<T> *front, *rear; //队头和队尾指针,分别指向头结点和终端结点
};
/*
* 前置条件:队列不存在
* 输 入:无
* 功 能:初始化队列
* 输 出:无
* 后置条件:创建一个空队列
*/
template <class T>
LinkQueue<T>::LinkQueue( )
{
front=rear=new Node<T>;
rear->next=NULL;
}
/*
* 前置条件:队列存在
* 输 入:无
* 功 能:销毁队列
* 输 出:无
* 后置条件:释放队列所占用的存储空间
*/
template <class T>
LinkQueue<T>::~LinkQueue( )
{
while(front)
{
Node <T> *p;
p=front->next;
delete front;
front=p;
}
}
/*
* 前置条件:队列已存在
* 输 入:无
* 功 能:判断队列是否为空
* 输 出:如果队列为空,返回true,否则,返回false
* 后置条件:队列不变
*/
template <class T>
bool LinkQueue<T>::Empty( )
{
return front==rear;
}
int main()
{
LinkQueue <int> Q1;
int x;
while(1)
{
cin>>x;
if(!x) break;
Q1.EnQueue(x);
}
cout<<"DeQueue:";
while(!Q1.Empty())
{
x=Q1.DeQueue();
cout<<x<<" ";
}
try{
x=Q1.DeQueue();
cout<<x<<" ";
}
catch(const char *ms)
{
cout<<ms<<endl;
}
cout<<"GetQueue:";
try{
x=Q1.GetQueue();
cout<<x<<" ";
}
catch(const char *ms)
{
cout<<ms<<endl;
}
return 0;
}
请实现其它的未实现的成员函数,达到任务要求。
Input
Output
Sample Input
1 2 3 4 5 0
Sample Output
DeQueue:1 2 3 4 5 Downflow
GetQueue:Downflow
//
// Created by Legends丶Hu on 2020/2/4.
//
#include <iostream>
using namespace std;
template<class T>
struct Node {
T data;
Node<T> *next;
};
template<class T>
class LinkQueue {
public:
LinkQueue(); //构造函数,初始化一个仅有头结点的空队列
~LinkQueue(); //析构函数,释放链队列中各结点的存储空间
void EnQueue(T x); //将元素x入队
T DeQueue(); //将队头元素出队,若队列为空,抛出异常“Downflow”,否则函数返回值为队头元素值
T GetQueue(); //取链队列的队头元素,若队列为空,抛出异常“Downflow”,否则函数返回值为队头元素值
bool Empty(); //判断链队列是否为空,为空返回true,否则返回false
private:
Node<T> *front, *rear; //队头和队尾指针,分别指向头结点和终端结点
};
template<class T>
LinkQueue<T>::LinkQueue() {
front = rear = new Node<T>;
rear->next = NULL;
}
template<class T>
LinkQueue<T>::~LinkQueue() {
while (front) {
Node<T> *p;
p = front->next;
delete front;
front = p;
}
}
template<class T>
bool LinkQueue<T>::Empty() {
return front == rear;
}
template<class T>
void LinkQueue<T>::EnQueue(T x) {
Node<T> *s = new Node<T>;
s->data = x;
s->next = rear->next;
rear->next = s;
rear = s;
}
template<class T>
T LinkQueue<T>::DeQueue() {
if(rear == front) throw "Downflow";
T x = front->next->data;
Node<T> *p = front->next;
front->next = p->next;
if(front->next == NULL)
rear = front;
delete p;
return x;
}
template<class T>
T LinkQueue<T>::GetQueue() {
return rear == front ? throw "Downflow" : front->next->data;
}
//1 2 3 4 5 0
int main() {
LinkQueue<int> Q1;
int x;
while (1) {
cin >> x;
if (!x) break;
Q1.EnQueue(x);
}
cout << "DeQueue:";
while (!Q1.Empty()) {
x = Q1.DeQueue();
cout << x << " ";
}
try {
x = Q1.DeQueue();
cout << x << " ";
}
catch (const char *ms) {
cout << ms << endl;
}
cout << "GetQueue:";
try {
x = Q1.GetQueue();
cout << x << " ";
}
catch (const char *ms) {
cout << ms << endl;
}
return 0;
}
本文介绍了一种链式队列的数据结构实现方法,并通过具体代码示例展示了如何进行初始化、元素入队、出队等操作。同时,文章还讨论了如何处理队列为空的情况。
2088

被折叠的 条评论
为什么被折叠?



