问答题
[说明]
以下程序的功能是实现堆栈的一些基本操作。堆栈类stack共有三个成员函数:empty判断堆栈是否为空;push进行人栈操作;pop进行出栈操作。
[C++程序]
#include "stdafx. h"
#include <iostream, h>
eonst int maxsize = 6;
class stack {
float data[ maxsize];
int top;
public:
stuck(void);
~ stack(void);
bool empty(void);
void push(float a);
float pop(void);
};
stack: :stack(void)
{ top =0;
cout < < "stack initialized." < < endl;
}
stack:: ~stack(void) {
cout < <" stack destoryed." < < endl;
bool stack:: empty (void) {
return (1) ;
void stack: :push(float a)
if(top= =maxsize) {
cout < < "Stack is full!" < < endl;
return;
data[top] =a;
(2) ;
}
float stack:: pop (void)
{ if( (3) ){
cout< < "Stack is undcrflow !" < < endl;
return 0;
(4) ;
return (5) ;
}
void main( )
{ stack s;
coat < < "now push the data:";
for(inti=l;i< =maxsize;i+ +) {
cout< <i< <" ";
s. push(i);
}
coat < < endl;
cout< < "now pop the data:";
for(i = 1 ;i < = maxsize ;i + + )
cout< <s. pop()< <" ";
}
【参考答案】
(1)top==0 true:false (2)top++(或者top =top+1)(3)top==0 (4)top-......
(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)