1 回答

TA贡献8条经验 获得超2个赞
伪代码:
while getEleFromStream -> ele
if ele != -1
if fullStack
logout
else
push ele
else
if emptyStack
logout
else
print pop
C++:
#include <stack>
#include <iostream>
using namespace std;
typedef stack<int> Stack;
const int stack_limit = 20;
void logout(const char *info);
int getEleFromStream(Stack &stk); // return errno_t [0 == normal, 1 == full, -1 == empty]
// process will exit while the empty condition take place
int main()
{
Stack stk;
while (getEleFromStream(stk) != -1);
return 0;
}
void logout(const char *info)
{
cout << info << endl;
}
int getEleFromStream(Stack &stk)
{
int ele = 0;
cin >> ele;
if (ele != -1)
if (stk.size() == limit)
{
logout("Full Stack!");
return 1;
} else
stk.push_back(ele);
else
if (stk.size() == 0)
{
logout("Empty Stack!");
return -1;
} else
cout << stk.pop() << endl;
return 0;
}
- 1 回答
- 0 关注
- 1110 浏览
添加回答
举报