首页 > 编程语言 > 详细

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

时间:2020-05-05 09:40:42      阅读:88      评论:0      收藏:0      [点我收藏+]

  最近学习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

技术分享图片

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

原文:https://www.cnblogs.com/xuelanga000/p/12829197.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!