【数据结构上机练习】3.栈的简单操作
2012-10-29 21:27
501 查看
//第二次上机第一题,栈的简单操作 1 //============================================================================ // Name : 上机2.1.cpp // Author : menglei // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> using namespace std; template<class T> class mStack{ //call a stack private: int size; T *stackArray; int top; public: mStack(){ size = 100; top = -1; stackArray = new T[100]; } T pop(){ if(top==-1){ cout<<"stack is empty ,can't pop!"<<endl; return NULL; //返回NULL会有问题,但编译能通过 } top--; return stackArray[top]; } int push(T value){ if(top==size){ cout<<"stack is full ! can't push !"<<endl; return -1; } stackArray[top++]= value; return 1; } T getTop(void){ if(top==-1){ cout<<"stack is empty! "<<endl; return NULL; } return stackArray[top-1]; } }; int main() { mStack <char> m; m.push('c'); cout<<"top element is:"<<m.getTop()<<endl; m.push('s'); cout<<"top element is:"<<m.getTop()<<endl; m.push('d'); cout<<"top element is:"<<m.getTop()<<endl; m.push('n'); cout<<"top element is:"<<m.getTop()<<endl; m.pop(); cout<<"top element is:"<<m.getTop()<<endl; return 0; }
运行结果:
相关文章推荐
- 【数据结构上机练习】7. 二叉树的简单操作(2)
- 【数据结构上机练习】4.链表的简单操作(2)
- 【数据结构上机练习】5.栈的简单操作(2)
- 【数据结构上机练习】6. 二叉树的简单操作(1)
- javascript 实现简单的table排序及table操作练习
- [SDUT](1138)数据结构上机测试2-1:单链表操作A ---链表操作(线性表)
- Linux 简单文件操作命令练习
- 上机练习项目二 简单计算问题体验
- 【数据结构】二叉树的简单遍历及基本操作
- 数据结构之链表与数组(二) -单向链表上的简单操作问题
- 数据结构之链表与数组(三)-单向链表上的简单操作
- python编程练习----对文件进行简单的操作(读写)
- 对链表的操作(数据结构线性表算法设计练习)
- 【数据结构和算法】用java简单的实现单链表的基本操作
- 数据结构上机测试2-2:单链表操作B
- Streams 一个操作简单的数据结构,很像数组或链接表
- SDUT1138数据结构上机测试2-1:单链表操作A
- 栈的简单基本操作(数据结构)
- sdut 面向对象程序设计上机练习五(简单类和对象)
- 数据结构:链表的简单操作