栈顶奇遇记
请修改编辑器中的代码,完成以下功能:实现一个顺序栈类 SeqStack,该类支持基本的栈操作,包括压栈、弹栈、获取栈顶元素以及判断栈是否为空。初始栈为空,栈的最大容量为100。调用方保证不对空栈取顶或弹出,也不向满栈压入;本题不要求实现越界异常处理。程序中已定义一个整数数组 a,包含元素 [2,4,6,8,10],将这些元素依次压入栈中。然后输出栈顶元素,接着依次弹出所有元素并输出。注意:只能在指定位置填写代码,不要改动其他部分。 不需要输入 输出示例: 10 10 8 6 4 2
#include<iostream>
using namespace std;
class SeqStack {
int data[100]; //存放栈元素的数组
int top; //栈顶元素的下标
public:
//**********found**********
SeqStack():____________{} //把top初始化为-1表示栈空
void push(int n){ data[++top]=n; } //把n值压入栈
//**********found**********
int pop(){ return ___________; } //退栈并返回栈顶元素值
//**********found**********
int topElement()const{ ________________; } //返回栈顶元素值
//**********found**********
bool isEmpty()const{ return _______; } //判栈空,空则返真,否返假
};
int main(){
int a[5]={2,4,6,8,10};
SeqStack stk;
for(int i=0; i<5; i++) stk.push(a[i]);
cout<<stk.topElement()<<' ';
while(!stk.isEmpty()) cout<<stk.pop()<<' ';
cout<<endl;
return 0;
}
控制台
调试终端
