顺序栈stack[0..m],栈底在stack[0]处。用top指向栈顶元素之后的空位置,判断栈空的条件是()
A.top= = -1
B.top= =0
C.top= =1
D.top=n-1
A.top= = -1
B.top= =0
C.top= =1
D.top=n-1
A、top = =-1
B、top = =m
C、top = =0
D、top = =m-1
A、一定是 top = 0
B、一定是 top = -1
C、top = 0 或 top = -1 都可以,与之相对应的,栈满也有两种不同的表示方法
D、其它三个说法都不正确
class Stack{
private:
int pList[100]; ∥int数组,用于存放栈的元素
int top; ∥栈顶元素(数组下标)
public:
Stack():top(0){}
void Push(const int &item); ∥新元素item
【说明】已知类LinkedList表示列表类,该类具有4个方法:addElement()、lastElement()、numberOfElement()以及removeLastElement()。4个方法的含义分别如下。voidaddElement(Obect):在列表尾部添加一个对象。ObjectlastElement():返回列表尾部对象。intnumberOfElement():返回列表中对象的个数。voidremoveLastElement():删除列表尾部的对象。现需要借助LinkedList来实现一个Stack栈类,C++代码1和C++代码2分别采用继承和组合的方式来实现。【C++代码1】
【C++代码2】
【问题】若类LinkedList新增加了一个公有的方法removeElement(intindex),用于删除列表中第index个元素,则在用继承和组合两种实现栈类Stack的方式中,哪种方式下Stack对象可访问方法removeElement(intindex)?__(5)__(A.继承B.组合)
阅读以下说明和C++程序,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
以下程序的功能是设计一个栈类stack,并建立一个整数栈。
【程序】
#include
#include
B.h>
const int Max=20;∥栈大小
template
class stack{∥栈元素数组
T s[Max];∥栈顶下标
int top;
public:
stack()
{
top=-1;∥栈顶初始化为-1
}
void push(const T &item);∥item入栈
T pop();∥出栈
int stackempty()const;∥判断栈是否为空
};
template
void stack::push(const T &item)
{
if(top== (1) )
{
cout$amp;exit (1) ;
}
top++;
s[top]=item;
}
template
T stack::pop()
{
T temp;
if(top== (2) )
{
cout$amp;exit (1) ;
}
temp=s[top];
top--;
return temp;
}
template
int stack::stackempty()const
{
return top==-1;
}
void main()
{
stackst;
int a[]={1,2,3,4,5 };
cout$amp;cout$amp;for(int i=0;i<4;i++)
{
cout$amp; (3) ;
}
cout$amp;while((4) )
cout$amp;cout$amp;}
A、010
B、0120
C、012
D、0 1 0
E、0 1 2 0
F、0 1
G、0 1 2
#include
/*此处为栈类型及其基本操作的定义,省略*/
int main(){
STACK station;
int state[1000];
int n; /*车厢数*/
int begin, i, j, maxNo; /*maxNo为A端正待入栈的车厢编号*/
printf("请输入车厢数:");
scanf("%d",&n);
printf(“请输入需要判断的车厢编号序列(以空格分隔):”);
if(n<1)return-1;
for (i=0; i
scanf("%d",&state[i]);
(1) ; /*初始化栈*/
maxNo=1;
for(i=0; i<n; ){ /*检查输出序列中的每个车厢号state[i]是否能从栈中获取*/
if((2) ){ /*当栈不为空时*/
if (state[i]=Top(station)) { /*栈顶车厢号等于被检查车厢号*/
printf("%d",Top(station));
Pop(&station);i++;
}
else
if ((3) ) {
printf(“error\n”);
return 1;
}
else{
begin= (4) ;
for(j=begin+l;j <=state [i];j++){
Push(&station, j);
}
}
}
else{ /*当栈为空时*/
begin=maxNo;
for(j=begin; j<=state[i];j++) {
Push(&station, j);
}
maxNo= (5) ;
}
}
printf("OK");
return 0;
}
define MAXSIZE 100
template <class T>
class Stack
{
T s[MAXSIZE];
int top;
public:
stack()
{
top=1;
}
void push(T newValue)
{
if(top<MAXSIZE)
{
top=top+1;
s[top]=newValue;
}
else
cout<<"堆栈满,无法进栈"<<end1;
}
void pop();
};
【 】
{
if(top>1)
{
cout<<s[top]<<end1;
top=top-1;
}
else
cout<<"堆栈空! "<<end1;
}
为了保护您的账号安全,请在“简答题”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!