C++(命名空间实现简单的入栈和出栈)

  最近学习C++觉得一定要理论联系实际,于是就想用C++的名空间实现数据的隐藏,

为用户提供数据接口,我就想这实现一个简单的出栈和入栈的操作。这个程序由于设计的

很简单,我没有添加很多容错判断。大家可以提出一些完善的建议。

 1 #include <iostream>
 2 using namespace std;
 3 
 4 namespace Stack { //实现
 5     const int max_size = 200;
 6     char v[max_size];
 7     int top=0;
 8     int bottom=0;
 9     void push(char c);
10     char pop();
11 
12 };
13 
14 void Stack::push(char c) {
15     if (top < max_size) {
16         v[top++] = c;
17     }
18     
19 }
20 
21 char Stack:: pop() {
22     char ret;
23     if (top >= bottom) {
24         ret = v[--top];
25     }
26     return ret;
27 }
28 
29 int main()
30 {
31     Stack::push('z');
32     Stack::push('L');
33     cout << "push1:" << Stack::v[0] << endl;
34     cout << "push2:" << Stack::v[1] << endl;
35     cout << "pop1:" << Stack::pop() << endl;
36     cout << "pop2:" << Stack::pop() << endl;
37 }
stack

posted @ 2020-05-05 01:30  flyingswallow  阅读(468)  评论(0编辑  收藏  举报