栈的C++实现
来源:互联网 发布:xy苹果助手for mac版 编辑:程序博客网 时间:2024/05/17 18:40
栈的C++实现
栈的特点是先进后出(LIFO),就像一叠盘子一样,你只能从最上面取。
这里介绍几个基本的功能:是否为空栈(empty),添加一个元素(push),取最顶元素(top),弹出一个元素(pop),输出栈中的元素(out)。C++中有栈容器,用到时只需#include即可。这里,我们自己创建一个栈类,一个int型的栈,再采用模板形式将其扩展。
- 创建一个栈类
该类应该包含的数据成员有指向栈顶元素的索引—mytop,以及一个数组用来存储栈中的元素。函数成员有上述几个功能empty…代码如下:
#include <iostream>using namespace std;const int STACK_CAPACITY = 128;typedef int StackElement;class Stack{ /******函数成员******/public: Stack(); bool empty() const; void push(const StackElement &value); StackElement top() const; void pop(); void out(ostream & o) const; /******数据成员******/private: int mytop; StackElement myarray[STACK_CAPACITY];};
具体函数实现在Stack.cpp中:
#include "Stack.h"Stack::Stack(){ mytop = -1;}bool Stack::empty() const{ return (mytop == -1);}void Stack::push(const StackElement &value){ if (mytop==STACK_CAPACITY-1) { cout << "Stack is full" << endl; } mytop++; myarray[mytop] = value;}StackElement Stack::top()const{ if (empty()) { cout << "Stack is empty!" << endl; } else return myarray[mytop];}void Stack::pop(){ if (empty()) { cout << "Stack is empty!" << endl; } else mytop--;}void Stack::out(ostream & o)const{ if (empty()) { o << "Stack is empty!" << endl; } else for (int i = mytop; i >= 0; i--) { o << myarray[i] << endl; }}
测试程序:
#include "Stack.h"int main(){ Stack s; if (s.empty()) { cout << "empty!" << endl; } else cout << "not empty!" << endl; for (int i = 0; i < 5;i++) { s.push(i); } s.out(cout); s.pop(); s.out(cout); return 0;}
- 改为模板的形式
模板简单理解就是前面我们那个栈是int型的,但是我们想让它可以根据我们的定义变化,比如我前面用到了一个int型的栈,后面又想用一个char型的,又想用个double型的,怎么办呢,用模板来就方便多了,每次根据自己的需要定义,例如Stack s,就OK了。
Stack.h代码:
#include <iostream>using namespace std;const int STACK_CAPACITY = 128;template <typename StackElement>class Stack{ /******函数成员******/public: Stack(); bool empty() const; void push(const StackElement &value); StackElement top() const; void pop(); void out(ostream & o) const; /******数据成员******/private: int mytop; StackElement myarray[STACK_CAPACITY];};template <typename StackElement>Stack<StackElement>::Stack(){ mytop = -1;}template <typename StackElement>bool Stack<StackElement>::empty() const{ return (mytop == -1);}template <typename StackElement>void Stack<StackElement>::push(const StackElement &value){ if (mytop == STACK_CAPACITY - 1) { cout << "Stack is full" << endl; } mytop++; myarray[mytop] = value;}template <typename StackElement>StackElement Stack<StackElement>::top()const{ if (empty()) { cout << "Stack is empty!" << endl; } else return myarray[mytop];}template <typename StackElement>void Stack<StackElement>::pop(){ if (empty()) { cout << "Stack is empty!" << endl; } else mytop--;}template <typename StackElement>void Stack<StackElement>::out(ostream & o)const{ if (empty()) { o << "Stack is empty!" << endl; } else for (int i = mytop; i >= 0; i--) { o << myarray[i] << endl; }}
测试代码:
#include "Stack.h"int main(){ Stack<int> s; if (s.empty()) { cout << "empty!" << endl; } else cout << "not empty!" << endl; for (int i = 0; i < 5;i++) { s.push(i); } s.out(cout); s.pop(); s.out(cout); Stack<char> s2; if (s2.empty()) { cout << "empty!" << endl; } else cout << "not empty!" << endl; for (char i = 'a'; i < 'e'; i++) { s2.push(i); } s2.out(cout); s2.pop(); s2.out(cout); return 0;}
测试结果:
添加一个应用,将十进制数转换为二进制:例如26转换为2进制的过程是:26/2=13余0,13/2=6余1,6/2=3余0,3/2=1余1,1/2=0余1,那么将所有的余数倒着写就是最终的结果:11010,先得到的余数最后写出来,最后面的余数第一个写出来,这就符合了栈的特点,后进先出。利用我们上面写的Stack类来写这个应用程序,代码如下:
#include "Stack.h"int main(){ Stack<int> s; int x = 26; int y; while (x!=0) { y = x % 2; s.push(y); x = x / 2; } s.out(cout); return 0;}
0 0
- 栈的C实现
- 【c++】栈的实现
- 栈的C语言实现
- 顺序栈的实现 c
- 栈的C语言实现
- C语言栈的实现
- 栈的实现(C++)
- C/C++栈的实现
- 栈的C语言实现
- c链式栈的实现
- 顺序栈的C实现
- 链式栈的C实现
- 栈的C数组实现
- 栈的C语言实现
- 栈的c语言实现
- [c++]栈模板的实现
- 栈的C语言实现
- 动态栈的实现(C++)
- char 和 varchar 区别与分析
- Linux MySQL重要目录与登录密码
- bzoj 1911 特别行动队 APIO2010
- 【POJ 1664】【递推】放苹果
- iOS之UITabBar 的相关属性
- 栈的C++实现
- OSI七层网络模型与TCP/IP四层网络模型
- Unexpected Exception caught setting 'house.floorage' on 'class action.HouseAction
- 第二章:在HTML中使用JavaScript
- c++ 子类虚函数、友元函数、复构、构造、赋值=的重写方式 - 练习题
- 归并排序_java实现
- 用VS2013搭建前端开发环境的心得
- 第九章:Shiro的WEB
- Linux静态库和动态库的使用