数据结构基础(一)栈的实现
2014-04-23 09:44
246 查看
class stack { private: int stackTopIndex; int *stackBase; int size; public: stack(int num) { if (num>=1) { size=num; stackBase=new int[size]; stackTopIndex=-1; } else { stackBase=NULL; //典型的空栈模式 stackTopIndex=-1; size=0; } }; ~stack() { if (stackBase!=NULL) { delete []stackBase; } }; bool push(int data) { //0-size-1 if (stackTopIndex<size-1) { stackTopIndex++; stackBase[stackTopIndex]=data; return true; } cout<<"Error:Full of Stack"<<endl; return false; }; bool pop(int &data) { if (stackTopIndex>=0) { data=stackBase[stackTopIndex]; stackTopIndex--; return true; } cout<<"Pop Error:Empty Stack"<<endl; return false; }; void print() { for (int i=0;i<=stackTopIndex;i++) { cout<<stackBase[i]<<"\t"; } }; void clear() { stackTopIndex=-1; } };
相关文章推荐
- 二叉树应用-最小堆类模板的实现(数据结构基础 第6周)
- 数据结构基础7_链队的实现
- 数据结构基础(7) --循环队列的设计与实现
- 数据结构基础(5)--C语言实现循环队列--静态
- 数据结构基础(10) --单链表迭代器的设计与实现
- 算法与数据结构基础4:C++二叉树实现及遍历方法大全
- 数据结构基础 算法实现
- 数据结构基础系列——单链表的实现
- 【数据结构】基础排序算法(Java实现)
- 数据结构基础(11) --循环链表的设计与实现
- 数据结构基础系列——链栈的实现
- 算法与数据结构基础3:C++单链表类的简单实现
- 数据结构基础(6) --顺序栈的设计与实现
- 数据结构基础 —— 快速排序 java 实现
- 二叉树基础-二叉树类模板的实现(数据结构基础 第5周)
- 数据结构基础(3)---C语言实现单链表
- 数据结构基础(22)--红黑树的设计与实现(上)
- 数据结构基础——循环队列的C++实现
- 数据结构基础(9) --单链表的设计与实现(2)之高级操作
- 数据结构基础(8) --单链表的设计与实现(1)之基本操作