阅读以下技术说明和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;
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 (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 ("队列已空!" );
}
}
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
[说明]
这是一个模拟渡口管理的算法。某汽车轮渡口,过江渡船每次能载10辆车过江。过江车辆分为客车类和火车类,上船有如下规定:同类车先到先上船,客车先于货车上渡船,且每上4辆客车,才允许上一辆货车;若等待客车不足4辆,则以货车代替,若无货车等待则允许客车都上船。
程序中用到的函数有enqueue(queue*sq,elemtype*x)在队列sq中入队一个元素x;outqueue(queue*sq,elemtype*x)在队列sq中出队一个元素,并将其值赋给x;empty(queue*sq)判断队列sq是否为空队,若为空,返回1;否则返回0。
[C程序]
include<stdio.h>
void pass(){
queue bus,truct; /*bus表示客车队列,truck表示货车队列*/
char ch;
int n,tag; /* ]n为车号,tag为标志,tag=0表示客车,tag=1表示货车*/
intcount=0,countbus=0,counttruck=0; /*分别表示上渡船汽车数、客车数、货车数*/
while(1){
printf("输入命令: \n");
Scanf("%c",&ch);
switch(ch){
case'e':
case'E': printf("车号: \n");
Scanf("%d",&n);
printf("客车\货车(0\1): \n");
scanf("%d",&tag);
if((1) )
enqueue(&bus,n);
else
enqueue(&truck,n);
break;
case'i':
case'I': while(count<10){
if((2) && empty(&bus)==0){ /*客车出队*/
outqueue(&bus,&n);
printf("上船的车号为: \n");
count++;
(3) ;
}
eise if((4) ){ /*货车出队*/
countbus=0;
outqueue(&truck,&n);
printf("上船的车号为: \n");
count++;
counttruck++;
}
else if(empty(&bus)==0){
(5);
outqueue(&truck,&n);
printf("没有10辆车排队轮渡\n");
count++;
countbus++;
}
else{
printf("没有10辆车排队轮渡\n");
retUrn;
}
break;
}
case'q':
case'Q':break;
}
if(ch=='q' || ch=='Q')
break;
}
}
写出以下程序段的输出结果(队列中的元素类型QElemType为char)。 void main() { Queue Q; InitQueue(Q); char x= ‘e’, y= ‘c’; EnQueue(Q, ‘h’); EnQueue(Q, ‘r’); EnQueue(Q, y); DeQueue(Q, x); EnQueue(Q, x); DeQueue(Q, x); EnQueue(Q, ‘a’); While(!QueueEmpty(Q)) { DeQueue(Q,y); cout<<y; }>
A.hrcha
B.rchah
C.rcha
D.char
阅读下列算法,并回答问题:
(1)Q、Q1和Q2都是队列结构,设队列Q=(1,0,-5,2,-4,-6,9),其中1为队头元素,写出执行f31(&Q,&Q1,&Q2)之后队列Q、Q1和Q2的状态;
(2)简述算法f31的功能。
(注:InitQueue、EnQueue、DeQueue和QueueEmpty分别是队列初始化、入队、出队和判队空的操作)
void f31(Queue*Q,Queue*Q1,Queue*Q2){
int e;
InitQueue(Q1);
InitQueue(Q2);
while(!QueueEmpty(Q)){
e=DeQueue(Q);
if(e>=0)EnQueue(Q1,e);
else EnQueue(Q2,e);
}
}
请完成算法填空,实现带表头结点的单链表形式实现的队列上的元素入队与出队操作,队列和元素结点结构体定义如下: typedef struct node { ElemType element; struct node* link; }Node; typedef struct queue { Node* front; //注意front指向表头结点,非头结点,请对视频中提供的代码进行修改 Node* rear; //指向尾结点 }Queue; void EnQueue(Queue *Q, ElemType x) { Node* p= (Node*)malloc(sizeof(Node)); ____________ = x; p->link = NULL; ____________=p; Q->rear=p; } void DeQueue(Queue *Q) { //若队列为空,直接返回 if(___________ ==NULL) return; Node *p=_____________; Q->front->link=___________; free(p); //若出队后,队列为空,则需重置rear if(______________==NULL) Q->rear=Q->front;//指向表头结点 }
为了保护您的账号安全,请在“简答题”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!