简单数据结构之数组栈(C++实现)
来源:互联网 发布:超级淘宝txt全集下载 编辑:程序博客网 时间:2024/05/19 17:58
/* ============================================================================ Name : stack_array.cpp Author : ntsk13 beijiwei@qq.com Version : Copyright : GPL Description : stack array study, complement by C++ Date : 2015.06.17 ============================================================================ */#include <iostream>using namespace std;#define STACK_CAPACITY 10typedef struct {int data;}Elem_t;class stack {public:Elem_t array[STACK_CAPACITY];int top;int capacity;int cur_len;void init();void clear();bool is_empty();Elem_t get_top_elem();bool push(Elem_t e);bool pop(Elem_t &e);int get_len();void traverse();};int main(void) {stack S;Elem_t zero,one,two,three,four,five,six;zero.data=0;one.data=1;two.data=2;three.data=3;four.data=4;S.init();cout<<"S is empty ? "<<( (S.is_empty()) ?"Yes":"No")<<endl;S.pop(six);S.push(zero);S.push(one);S.push(two);S.push(three);S.push(four);cout<<"S is empty ? "<< (S.is_empty() ?"Yes":"No")<<endl;five=S.get_top_elem();S.traverse();cout<<"len is "<<S.get_len()<<endl;cout<<"============================================="<<endl;S.pop(six);S.pop(five);S.push(four);S.push(four);S.push(four);S.push(four);S.push(four);S.push(four);S.push(four);S.push(four);S.push(four);S.push(four);S.push(four);S.push(four);S.push(four);cout<<"len is "<<S.get_len()<<endl;S.traverse();return 0;}void stack::init(){capacity=STACK_CAPACITY;cur_len=0;top=-1;}void stack::clear(){cur_len=0;top=-1;}bool stack::is_empty(){return (top==-1)?true:false;}Elem_t stack::get_top_elem(){return array[top];}bool stack::push(Elem_t e){if( top==capacity-1){cout<<"It is full, can not push !!!"<<endl;return false;}array[top+1]=e;top++;cur_len++;return true;}bool stack::pop( Elem_t & e){int tmp=top;if( is_empty() ){cout<<"It is empty, can not pop !!!"<<endl;return false;}top--;cur_len--;e=array[tmp];return true;}int stack::get_len(){return cur_len;}void stack::traverse(){int t=top;for(int i=0;i<cur_len;i++)cout<<"The "<<i<<"th elem is "<<(array[t--]).data<<endl;}
0 0
- 简单数据结构之数组栈(C++实现)
- 简单数据结构---数组实现栈
- c数据结构之顺序栈(数组实现1)
- C数据结构之顺序栈(数组实现2)
- 【数据结构】栈学习之数组实现(C语言)
- 数据结构--队列之C数组实现
- 数据结构之数组(C语言实现)
- 数据结构C语言之栈的简单实现
- 数据结构C语言之单链表简单实现
- 简单数据结构之顺序表(C实现)
- 简单数据结构之链式表(C实现)
- 1.数据结构(C语言)---自定义数组的简单实现
- 数据结构之栈(C实现)
- 数据结构之数组实现
- C语言------数据结构(栈操作,数组实现)
- C语言数据结构-栈-数组实现
- [数据结构]栈的C语言简单实现
- 数据结构C语言实现之循环队列----数组形式---简单化
- Ext Ajax 使用encodeURI 乱码
- C#中的“\”、@、break、continue
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- 提高你的Java代码质量吧:推荐使用枚举定义常量
- GitHub 优秀的 Android 开源项目
- 简单数据结构之数组栈(C++实现)
- 第一次小有成就心得
- Oracle常用的命令中如何查看表的结构
- z-index在css中怎么用?
- 移动网络应用市场趋势分析
- Bluetooth--- Android 蓝牙开发基本流程
- Java之this与super
- 解决 yum安装出现This system is not registered with RHN
- java.util.zip.DataFormatException: invalid distance too far back