数据结构:实验5(求堆栈元素和)
#include "stdio.h"
#include "SeqStack.h"
int StackSum(SeqStack *s) //求和
{
int x=0;
int sum = 0;
while (StackNotEmpty(*s))
{
StackPop(s, &x);
sum += x;
}
return sum;
}
int main()
{
SeqStack s;
StackInitiate(&s);
StackPush(&s, 1);
StackPush(&s, 2);
printf("sum= %d", StackSum(&s));
return 0;
}
//运行结果:sum=3
网络上志同道合,我们一起学习网络安全,一起进步,QQ群:694839022