填空题
以下程序的功能是;实现一个队列的简单管理,每一个队列结点包括一个学生的基本信息(姓名,数学、物理和英语成绩)。成员函数add1建立一个新结点,并将该结点加入队尾。成员函数remove2从队首取下一个结点,并通过参数将所取结点中的数据返回。成员函数Print3输出队列中各个结点的值。当队列非空时,析构函数~Queue4依次删除队列上的各个结点。
[程序] (4分)
#include
#include
class Node{
public:
Node(char nam[],int m,int p,int e)
{strcpy(name,nam);math=m;phy=p;eng=e;}
Node5{}
private:
char name[10]; //姓名
int math,phy,eng; //数学,物理,英语成绩
Node *nextItem;
friend class Queue;
};
class Queue{
public:
Queue6{front=NULL;back=NULL;)
~Queue7;
void remove(char *,int &,int &,int &);
void add(char [],int,int,int);
int is_empty8{return back==NULL;}
//输出队列中各个结点的值
void Print9
{
Node *p=front;
while(p){
coutnextItem;
}
}
private:
Node *front;
Node *back;
};
//Queue的成员remove10实现从队列头取下一个结点,并返回该结点的值
void Queue::remove(char n[],int &m,int &p,int &e)
{
Node *pFront; //指向头结点的临时指针
pFront=front;
strcpy(n,front->name);
m=front->math;p=front->phy;e=front->eng;
11(27) 12 ;
delete pFront;
}
//Queue的函数成员add13实现在队列中增加一个项,即增加一个结点
void Queue::add(char n[],int m,int P,int e)
{
Node *pNew=new Node;
strcpy(pNew->name,n);
pNew->math=m;pNew->phy=p;pNew->eng=e;
pNew->nextItem=NULL;
if(front==NULL) 14(28) 15;
else{
16(29) 17 ;
back=pNew;
}
}
Queue::~Queue18
{
Node *p=front,*q;
while(p!=NULL){
19(30) 20;
delete P;
p=q;
}
}
//主函数完成对各成员函数的简单测试
void main(void)
{
Queue q1;
int m,p,e;
char str[10];
cout<<"输入队列中学生的姓名、数学、物理,英语成绩(以0结束);";
cin>>str>>m>>p>>e;
while(strcmp(str,"O")!=0){
q1.add(str,m,P,e);
cout<<"输入队列中一个项的值(以0结束);";
cin>>str>>m>>p>>e;
}
cout<<"队列中各项为;/n";
q1.Print 21;
q1.remove(str,m,p,e);
cout<<"队列中移下项的值为;"<
cout<<"队列中各项为;/n";
q1.Print22;
}
【正确答案】
1、(27)front=front->nextItem
(28)front=back=pNew
(29)back->nextItem=pNew
(30)q=p->nextItem
【答案解析】