回 帖 发 新 帖 刷新版面

主题:[讨论]栈中元素个数怎么计算

请教大家:假设用长度为50的数组作为栈的存储空间,栈底指针bottom指向栈底元素,栈顶指针top指向栈顶元素,bottom=49.top=30(数组下标),则栈中有多少元素?如果bottom<top的话,又怎样计算?

回复列表 (共2个回复)

沙发

循环栈?

// 代码环境:C++Builder 2009:)
int 取栈使用空间(int 顶,int 底)
{
    int 栈总大小=50;
    int 元素个数=顶-底;
    if (元素个数<0) 元素个数+=栈总大小;
    return 元素个数;
}

板凳

(top-bottom+1+50)%50;

我来回复

您尚未登录,请登录后再回复。点此登录或注册