数据结构 阅读算法

来源:百度知道 编辑:UC知道 时间:2024/04/29 13:29:54
void AE(Stack&S){

InitStack(S);
Push(S,3);
push(S,4);
int x=Pop(S)+2*Pop(S);
Push(S,x);
int i,a[5]={1,5,8,12,15};
for(i=0;i<5;i++)Push(S,2*a[i]);
while(!StackEmpty(s)) cout<<Pop(S)<<'';
}

算法被调用后的输出结果是什么.

InitStack(S);
Push(S,3);
push(S,4);
int x=Pop(S)+2*Pop(S);
Push(S,x);
栈底元素==x==2*3+4=10

for(i=0;i<5;i++)Push(S,2*a[i]);
栈元素从底到上分别为:2,10,16,24,30

while(!StackEmpty(s)) cout<<Pop(S)<<'';

弹出栈元素 :
30,24,16,10,2,10

期待中......

void AE(Stack&S){

InitStack(S); // 初始化栈
Push(S,3); // 3 进栈
push(S,4); // 4 进栈
int x=Pop(S)+2*Pop(S); // 先把4出栈,再把3出栈,之后给x赋值,即
// x = 4 + 2 * 3 = 10
Push(S,x); // 10 进栈,此时栈里只有这一个元素
int i,a[5]={1,5,8,12,15};
for(i=0;i<5;i++)Push(S,2*a[i]); //数组里的数的2倍依次进栈,
//进栈顺序为 2,10,16,24,30
while(!StackEmpty(s)) cout<<Pop(S)<<''; //栈里的元素依次出栈,
//输出结果为
//30 24 16 10 2 10
}