阅读以下说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。说明类Queue表示队列,类中的方法如下表所示。类Node表示队列中的元素;类EmptyQueueException 给出了队列操作中的异常处理操作。Java 代码public class TestMain{ // 主类public static void main(String args[]) {Queue q = new Queue();q.enqueue("first!");q.enqueue("second!");q.enqueue("third!");(1) {while(true)System.out.println(q. dequeue());}catch((2)) ( }}}public class Queue { // 队列Node m_FirstNode;public Queue() { m_FirstNode = null; }public boolean isEmpty() {if(m_FirstNode == null) return true;else return false;}public void enqueue(Object newNode) {// 入队操作Node next = m_FirstNode;if(next==null) m_FirstNode = new Node(newNode);else {while(next.getNext() != null) next = next.getNext();next.setNext(new Node(newNode));}}public Object dequeue() (3) {// 出队操作Object node;if (isEmpty())(4); // 队列为空,抛出异常else {node = m_FirstNode.getObject();m_FirstNode = m_FirstNode.getNext();return node;}}}public class Node { // 队列中的元素Object m_Data;Node m_Next;public Node(Object data) { m_Data = data; m_Next = null; }public Node(Object data, Node next) { m_Data = data; m_Next = next; }public void setObject(Object data) { m_Data = data; }public Object getObject0 { return m_Data; }public void setNext(Node next) { m_Next = next; }public Node getNext() { return m_Next; }}public class EmptyQueueException extends (5) { // 异常处理类public EmptyQueueException0 {System.out.println("队列已空 ! ");}}
阅读以下说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。
说明
类Queue表示队列,类中的方法如下表所示。
类Node表示队列中的元素;类EmptyQueueException 给出了队列操作中的异常处理操作。
Java 代码
public class TestMain{ // 主类
public static void main(String args[]) {
Queue q = new Queue();
q.enqueue("first!");
q.enqueue("second!");
q.enqueue("third!");
(1) {
while(true)
System.out.println(q. dequeue());
}
catch((2)) ( }
}
}
public class Queue { // 队列
Node m_FirstNode;
public Queue() { m_FirstNode = null; }
public boolean isEmpty() {
if(m_FirstNode == null) return true;
else return false;
}
public void enqueue(Object newNode) {// 入队操作
Node next = m_FirstNode;
if(next==null) m_FirstNode = new Node(newNode);
else {
while(next.getNext() != null) next = next.getNext();
next.setNext(new Node(newNode));
}
}
public Object dequeue() (3) {// 出队操作
Object node;
if (isEmpty())
(4); // 队列为空,抛出异常
else {
node = m_FirstNode.getObject();
m_FirstNode = m_FirstNode.getNext();
return node;
}
}
}
public class Node { // 队列中的元素
Object m_Data;
Node m_Next;
public Node(Object data) { m_Data = data; m_Next = null; }
public Node(Object data, Node next) { m_Data = data; m_Next = next; }
public void setObject(Object data) { m_Data = data; }
public Object getObject0 { return m_Data; }
public void setNext(Node next) { m_Next = next; }
public Node getNext() { return m_Next; }
}
public class EmptyQueueException extends (5) { // 异常处理类
public EmptyQueueException0 {
System.out.println("队列已空 ! ");
}
}