题目描述

已知链式队列类的定义、部分实现及主函数代码(勿改动)如下:
template
struct Node
{
T data;
Node next;
};
template
class LinkQueue
{
public:
LinkQueue( ); //构造函数,初始化一个仅有头结点的空队列
~LinkQueue( ); //析构函数,释放链队列中各结点的存储空间
void EnQueue(T x); //将元素x入队
T DeQueue( ); //将队头元素出队,若队列为空,抛出异常“Downflow”,否则函数返回值为队头元素值
T GetQueue( ); //取链队列的队头元素,若队列为空,抛出异常“Downflow”,否则函数返回值为队头元素值
bool Empty( ); //判断链队列是否为空,为空返回true,否则返回false
private:
Node
front, rear; //队头和队尾指针,分别指向头结点和终端结点
};
/

前置条件:队列不存在
输 入:无
功 能:初始化队列
输 出:无
后置条件:创建一个空队列
/
template
LinkQueue::LinkQueue( )
{

front=rear=new Node;
rear->next=NULL;
}
/
前置条件:队列存在
输 入:无
功 能:销毁队列
输 出:无
后置条件:释放队列所占用的存储空间
/
template
LinkQueue::~LinkQueue( )
{
while(front)
{
Node
p;
p=front->next;
delete front;
front=p;
}
}
/
前置条件:队列已存在
输 入:无
功 能:判断队列是否为空
输 出:如果队列为空,返回true,否则,返回false
后置条件:队列不变
/
template
bool LinkQueue::Empty( )
{
return front==rear;
}
int main()
{
LinkQueue Q1;
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
ms)
{
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

提示

来源

提交

  1. import java.util.Scanner;
  2. class Node{
  3. Object data;
  4. Node next;
  5. public Node() {
  6. }
  7. public Node(Object data) {
  8. this.data = data;
  9. this.next = null;
  10. }
  11. public Node(Object data, Node next) {
  12. this.data = data;
  13. this.next = next;
  14. }
  15. public Object getData() {
  16. return data;
  17. }
  18. public void setData(Object data) {
  19. this.data = data;
  20. }
  21. public Node getNext() {
  22. return next;
  23. }
  24. public void setNext(Node next) {
  25. this.next = next;
  26. }
  27. }
  28. class LinkQueue{
  29. Node front;
  30. Node rear;
  31. public LinkQueue(){
  32. rear = front = null;
  33. }
  34. void EnQueue(Object data){
  35. Node p =new Node(data);
  36. if(front != null){
  37. rear.next = p;
  38. rear = p;
  39. }else{
  40. front = rear = p;
  41. }
  42. }
  43. Object DeQueue() throws Exception{
  44. if(front != null){
  45. Node p = front;
  46. front = front.next;
  47. if(p==rear){
  48. rear = null;
  49. }
  50. return p.data;
  51. }else{
  52. throw new Exception("Downflow");
  53. }
  54. }
  55. Object GetQueue() throws Exception{
  56. if(front != null){
  57. return front.data;
  58. }else{
  59. throw new Exception("Downflow");
  60. }
  61. }
  62. boolean isEmpty(){
  63. return front == null;
  64. }
  65. }
  66. public class Main {
  67. public static void main(String[] args) {
  68. LinkQueue l = new LinkQueue();
  69. Scanner s = new Scanner(System.in);
  70. while(s.hasNext()){
  71. int x = s.nextInt();
  72. if(x==0)break;
  73. l.EnQueue(x);
  74. }
  75. System.out.print("DeQueue:");
  76. while(!l.isEmpty()){
  77. try {
  78. System.out.print(l.DeQueue()+" ");
  79. } catch (Exception e) {
  80. System.out.print(e.getMessage());
  81. }
  82. }
  83. try {
  84. System.out.print(l.DeQueue()+" ");
  85. } catch (Exception e) {
  86. System.out.print(e.getMessage());
  87. }
  88. System.out.println();
  89. System.out.print("GetQueue:");
  90. try {
  91. System.out.print(l.GetQueue()+" ");
  92. } catch (Exception e) {
  93. System.out.print(e.getMessage());
  94. }
  95. }
  96. }