题目描述
已知链式队列类的定义、部分实现及主函数代码(勿改动)如下:
template
struct Node
{
T data;
Node
};
template
class LinkQueue
{
public:
LinkQueue( ); //构造函数,初始化一个仅有头结点的空队列
~LinkQueue( ); //析构函数,释放链队列中各结点的存储空间
void EnQueue(T x); //将元素x入队
T DeQueue( ); //将队头元素出队,若队列为空,抛出异常“Downflow”,否则函数返回值为队头元素值
T GetQueue( ); //取链队列的队头元素,若队列为空,抛出异常“Downflow”,否则函数返回值为队头元素值
bool Empty( ); //判断链队列是否为空,为空返回true,否则返回false
private:
Node
};
/
前置条件:队列不存在
输 入:无
功 能:初始化队列
输 出:无
后置条件:创建一个空队列
/
template
LinkQueue
{
front=rear=new Node
rear->next=NULL;
}
/
前置条件:队列存在
输 入:无
功 能:销毁队列
输 出:无
后置条件:释放队列所占用的存储空间
/
template
LinkQueue
{
while(front)
{
Node
p=front->next;
delete front;
front=p;
}
}
/
前置条件:队列已存在
输 入:无
功 能:判断队列是否为空
输 出:如果队列为空,返回true,否则,返回false
后置条件:队列不变
/
template
bool LinkQueue
{
return front==rear;
}
int main()
{
LinkQueue
int x;
while(1)
{
cin>>x;
if(!x) break;
Q1.EnQueue(x);
}
cout<<”DeQueue:”;
while(!Q1.Empty())
{
x=Q1.DeQueue();
cout<
try{
x=Q1.DeQueue();
cout<
catch(const char
{
cout<
cout<<”GetQueue:”;
try{
x=Q1.GetQueue();
cout<
catch(const char *ms)
{
cout<
return 0;
}
请实现其它的未实现的成员函数,达到任务要求。
输入
输出
样例输入
1 2 3 4 5 0
样例输出
DeQueue:1 2 3 4 5 Downflow
GetQueue:Downflow
提示
来源
提交
import java.util.Scanner;class Node{Object data;Node next;public Node() {}public Node(Object data) {this.data = data;this.next = null;}public Node(Object data, Node next) {this.data = data;this.next = next;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public Node getNext() {return next;}public void setNext(Node next) {this.next = next;}}class LinkQueue{Node front;Node rear;public LinkQueue(){rear = front = null;}void EnQueue(Object data){Node p =new Node(data);if(front != null){rear.next = p;rear = p;}else{front = rear = p;}}Object DeQueue() throws Exception{if(front != null){Node p = front;front = front.next;if(p==rear){rear = null;}return p.data;}else{throw new Exception("Downflow");}}Object GetQueue() throws Exception{if(front != null){return front.data;}else{throw new Exception("Downflow");}}boolean isEmpty(){return front == null;}}public class Main {public static void main(String[] args) {LinkQueue l = new LinkQueue();Scanner s = new Scanner(System.in);while(s.hasNext()){int x = s.nextInt();if(x==0)break;l.EnQueue(x);}System.out.print("DeQueue:");while(!l.isEmpty()){try {System.out.print(l.DeQueue()+" ");} catch (Exception e) {System.out.print(e.getMessage());}}try {System.out.print(l.DeQueue()+" ");} catch (Exception e) {System.out.print(e.getMessage());}System.out.println();System.out.print("GetQueue:");try {System.out.print(l.GetQueue()+" ");} catch (Exception e) {System.out.print(e.getMessage());}}}
