数据结构:实验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
posted @ 2017-03-27 21:42  SEC.VIP_网络安全服务  阅读(73)  评论(0编辑  收藏  举报