阅读下列函数说明和Java代码,将应填入(n)处的字句写在对应栏内。【说明】类Queue表示队列,类中的方法如下表所示。类Node表示队列中的元素;类EmptyQueueException给出了队列操作中的异常处理操作。public class TestMain { //主类public static viod main (String args[]){Queue q=new Queue();q.enqueue("first!");q.enqueue("second!");q.enqueue("third

题目

阅读下列函数说明和Java代码,将应填入(n)处的字句写在对应栏内。

【说明】

类Queue表示队列,类中的方法如下表所示。

类Node表示队列中的元素;类EmptyQueueException给出了队列操作中的异常处理操作。

public class TestMain { //主类

public static viod 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 viod 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 (isEempty())

(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 getObject(Object data) {return m_data;}

public void setNext(Node next) {m_Next=next;}

public Node getNext() {return m_Next;}

}

public class EmptyQueueException extends (5) { //异常处理类

public EmptyQueueException() {

System.out.println("队列已空! ");

}

}


相似考题

1.阅读以下技术说明和Java代码,将Java程序中(1)~(5)空缺处的语句填写完整。[说明]类Queue表示队列,类中的方法如表4-12所示。类Node表示队列中的元素;类EmptyQueueException给出了队列中的异常处理操作。[Java代码]public class testmain { //主类public static viod 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;elsereturn false;}public viod 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 (is empty())(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 getobject(object data) {return m_data; }public void setnext(node next) {m_next=next; }public node getnext() {return m_next; }}public class emptyqueueexception extends (5) { //异常处理类public emptyqueueexception() {system. out. println ( "队列已空!" );}}

3.阅读以下说明和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给出了队列操作中的异常处”相关问题
  • 第1题:

    阅读下列说明和C++-代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图5-1所示的类图。

    【C++代码】 #include using namespace std; class invoice{ public: (1){ cout<<"This is the content of the invoice!"<

    答案:
    解析:
    (1) virtual void printInvoice() (2) ticket->printInvoice() (3) Decorator::printInvoice() (4) Decorator::printInvoice() (5) &a
    【解析】

    试题分析
    1.Invoice类下,义虛函数,按类图,函数名是printInvoice
    2.前面定义对象名是ticket,那么在ticket不为空的时候调用函数printInvoice
    3.这部分填写发票的抬头,看类图应该实现函数printInvoice ,Decorator装饰模式使用该方法
    4.这部分是发票的脚注,看类图应该实现函数printlnvoice,Decorator装饰模式使用该方法
    5.FootDecorator a(NULL) ;脚步的装饰参数是a,调用a参数,

  • 第2题:

    阅读下列说明和 Java 代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 某软件公司欲开发一款汽车竞速类游戏,需要模拟长轮胎和短轮胎急刹车时在路面上 留 下的不同痕迹,并考虑后续能模拟更多种轮胎急刹车时的痕迹。现采用策略(Strategy) 设 计模式来实现该需求,所设计的类图如图 5-1 所示。




    答案:
    解析:
    1.void stop()2.BrakeBehavior3.wheel.stop()4.wheel=behavior5.brake()

  • 第3题:

    阅读下列说明和?C++代码,将应填入(n)处的字句写在答题纸的对应栏内。
    【说明】
    阅读下列说明和?Java代码,将应填入?(n)?处的字句写在答题纸的对应栏内。
    【说明】
    某快餐厅主要制作并出售儿童套餐,一般包括主餐(各类比萨)、饮料和玩具,其餐品种
    类可能不同,但其制作过程相同。前台服务员?(Waiter)?调度厨师制作套餐。现采用生成器?(Builder)?模式实现制作过程,得到如图?6-1?所示的类图。






    答案:
    解析:

  • 第4题:

    试题四(共 15 分)阅读以下说明和 C 函数,填补函数中的空缺,将解答填入答题纸的对应栏内。【说明】简单队列是符合先进先出规则的数据结构,下面用不含有头结点的单向循环链表表示简单队列。函数 enqueue(queue *q,KeyType new_elem) 的功能是将元素new_elem 加入队尾。函数 Dnqueue(queue *q,KeyType *elem)的功能使将非空队列的队头元素出队(从队列中删除),并通过参数带回刚出队的元素。用单向循环链表表示的队列如图 4-1 所示。

    图 4-1 单向循环链表表示的队列示意图队列及链表结点等相关类型定义如下:enum {errOr, OK};typedef int KeyType;typedef struct qNode﹛KeyType data;Struct qNode*next;﹜qNode,*Linkqueue; Typedef struct﹛int size;Link:queue rear;}queue; 【C 函数】int enqueue(queue*q,KeyType new_elem)﹛ //元素 new_elem 入队列qNode*p;P=(qNode*)malloc(sizeof(qNode));if(!p)return errOr;P->data=new_elem;if(q->rear)﹛P->next=q->rear->next;();﹜elseP->next=p;﹙﹚;q->size++;return OK;﹜ int Dequeue(queue*q,KeyType*elem)﹛ //出队列qNode*p;if(0==q->size) //是空队列return errOr;P=(); //令 p 指向队头元素结点*elem =p->data;q->rear->next=(); //将队列元素结点从链表中去除if(()) //被删除的队头结点是队列中唯一结点q->rear=NULL //变成空队列free(p);q->size--;return OK;﹜


    答案:
    解析:
    (1)Q→rear→next=p(2)Q→rear=p(3)Q→rear→next(4)p→next(5)Q→rear==p 或 Q→rear→next==p→next 或 Q→size==1
    【解析】

    本题考察C语言指针与链表的知识,为入队列和删除队列问题。对于入队列,那么当队列Q不为空时,P的队尾t要指向原Q的队尾指向的元素,即:P->next=Q->rear->next,Q的队尾要指向p,即:Q→rear→next=p。当队列Q为空时,插入p元素,则p的队尾指向p自身,即:p→next=p,且整个队列Q的队尾也是p,即:Q→rear=p。对于队列删除元素p,先判断Q是否为空,为空队列则返回 ERROR;If(0==q->size) //是空队列Return ERROR;另p指向队头元素结点,队头元素结点可用Q→rear→next表示。此时,p转化为头结点,p出列,则需要Q的队尾指向p的下一个元素,因此第4空填:p→next。最后,判断被删除的队头结点是否是队列中的唯一结点,可采用:Q→rear==p 或 Q→rear→next==p→next 或 Q→size==1 等表示方法。

  • 第5题:

    阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】 某文件管理系统中定义了类OfficeDoc和DocExplorer,当类OfficeDoc发生变化时,类DocExplorer的所有对象都要更新其自身的状态,现采用观察者(Observer)设计模式来实现该需求,所设计的类图如图6-1所示。



    答案:
    解析:
    1: void update()2: Observer3: obs.update()4: Subject5: Attach(this)