自己写个简单的栈
来源:互联网 发布:spss数据分析论文 编辑:程序博客网 时间:2024/04/25 21:05
/*************************************************************************
> File Name: Stack.h
> Author:keson
> Mail:keson@bupt.edu.cn
> Created Time: 2014年11月21日 星期五 17时43分19秒
************************************************************************/
#ifndef _STACK_H
#define _STACK_H
template<typename Object>
class Stack
{
public:
explicit Stack(int initCapacity=0):theCapacity{initCapacity}
{objects=new Object[theCapacity];}
//Deconstructor
~Stack()
{
delete[]objects;
}
Object &operator[](int index)
{
return objects[index];
}
int capacity()const
{return theCapacity;}
int size() const
{
return theSize;
}
bool Empty() const
{
return theSize==0;
}
void push(Object x)
{
if(top>=theCapacity-1)
throw"overflow_error";
top++;
theSize++;
objects[top]=x;
}
Object back()
{
if(Empty())
throw"下溢出";
return objects[top];
}
Object pop()
{
if(Empty())
throw"下溢出";
top--;
theSize--;
return objects[top+1];
}
private:
int theSize=0;
int theCapacity;
int top=-1;
Object *objects;
};
#endif
> File Name: Stack.h
> Author:keson
> Mail:keson@bupt.edu.cn
> Created Time: 2014年11月21日 星期五 17时43分19秒
************************************************************************/
#ifndef _STACK_H
#define _STACK_H
template<typename Object>
class Stack
{
public:
explicit Stack(int initCapacity=0):theCapacity{initCapacity}
{objects=new Object[theCapacity];}
//Deconstructor
~Stack()
{
delete[]objects;
}
Object &operator[](int index)
{
return objects[index];
}
int capacity()const
{return theCapacity;}
int size() const
{
return theSize;
}
bool Empty() const
{
return theSize==0;
}
void push(Object x)
{
if(top>=theCapacity-1)
throw"overflow_error";
top++;
theSize++;
objects[top]=x;
}
Object back()
{
if(Empty())
throw"下溢出";
return objects[top];
}
Object pop()
{
if(Empty())
throw"下溢出";
top--;
theSize--;
return objects[top+1];
}
private:
int theSize=0;
int theCapacity;
int top=-1;
Object *objects;
};
#endif
0 0
- 自己写个简单的栈
- 自己写个简单的天空盒子
- 写个自己的
- 写个自己的
- 自己写了个简单的供求系统
- <源码>自己写个简单的单播SNTP客户端
- 自己写的第一个Gating简单例子
- 自己写简单的SqlHelper
- 自己写简单的LinkedList
- 自己写的简单计算器
- 自己写的简单HttpClient
- 苦于E63没有秒表,网上找的不好用,就自己写个简单的
- 写个简单的makefile
- 自己用C可变参数写个简单的printf函数
- makefile太难了,学不会,自己用Python写个简单的构建工具
- C++算法之 自己写一个简单的栈Stack
- 写个自己的调试器
- 写个自己的调试器
- Objective - C 1. 类和对象
- 使用Myeclipse实现快速调试
- Linux makefile 教程 非常详细,且易懂
- STL中用ERASE()方法遍历删除元素
- 指针和引用的区别
- 自己写个简单的栈
- java 面向对象笔记(一)
- Matlab中各种有用函数
- 第十三周项目1——3 3的倍数
- 文本编辑器Scite源代码阅读过程(二)
- VISIO中带宽的调整。。。我点了取消组合后,显示“由于形状保护和或图层设置的问题,无法完全执行此命令
- Tomcat全攻略
- 数值统计_JAVA_杭电ACM2008
- 中缀表达式转后缀表达式