soln7_8.cpp
来自「Visual C++ 2005的源代码」· C++ 代码 · 共 82 行
CPP
82 行
// Soln7_8.cpp
#include <iostream> // For stream input/output
using std::cout;
using std::endl;
class CStack
{
public:
CStack() : next(0) {}
void push(int i);
int pop();
void print();
int peek();
private:
int list[100];
int next;
};
void CStack::push(int i)
{
if (next < 99)
list[next++] = i;
else
cout << "Error! Stack overflow" << endl;
}
int CStack::pop()
{
if (next == 0)
{
cout << "Error! Stack underflow" << endl;
return 0;
}
else
return list[--next];
}
void CStack::print()
{
cout << '[';
for(int i=next-1 ; i>=0 ; i--)
cout << ' '<< list[i];
cout << " ]\n";
}
int CStack::peek()
{
if (next == 0)
{
cout << "Error! Stack underflow" << endl;
return 0;
}
else
return list[next-1];
}
int main()
{
CStack s;
s.print();
s.push(5);
s.push(10);
s.push(8);
s.print();
cout << "peek at top of stack = " << s.peek() << endl;
s.print();
cout << "pop top of stack = " << s.pop() << endl;
cout << "pop top of stack = " << s.pop() << endl;
s.print();
cout << "pop top of stack = " << s.pop() << endl;
cout << "pop top of stack = " << s.pop() << endl;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?