1 Star 0 Fork 0

macroan/stack

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ArrayStack.cpp 1.61 KB
一键复制 编辑 原始数据 按行查看 历史
macroan 提交于 2018-04-18 11:58 +08:00 . 新建 ArrayStack.cpp
#include <iostream>
using namespace std;
template <typename T>
class arrayStack
{
public:
arrayStack(int size);
~arrayStack();
public:
bool isEmpty();
bool isFull();
int getCount();
int getSize();
T top();
T pop();
bool push(T data);
private:
T *data;
int ptop;
int count;
int size;
};
template <typename T>
arrayStack<T>::arrayStack(int size)
{
data = new T[size];
memset(data, 0, sizeof(T)*size);
ptop = size;
count = 0;
this->size = size;
}
template <typename T>
arrayStack<T>::~arrayStack()
{
delete data;
data = NULL;
}
template <typename T>
bool arrayStack<T>::isEmpty()
{
return count == 0;
}
template <typename T>
bool arrayStack<T>::isFull()
{
return count == size;
}
template <typename T>
int arrayStack<T>::getCount()
{
return count;
}
template <typename T>
int arrayStack<T>::getSize()
{
return size;
}
template <typename T>
T arrayStack<T>::top()
{
return data[ptop];
}
template <typename T>
T arrayStack<T>::pop()
{
if (isEmpty())
return (T)-1;
T d = data[ptop++];
count--;
return d;
}
template <typename T>
bool arrayStack<T>::push(T d)
{
if (isFull())
return false;
data[--ptop] = d;
count++;
return true;
}
int main() {
arrayStack<int> stack(10);
for(int i=0; i<10; i++)
stack.push(i);
cout<<"stack count:"<<stack.getCount()<<endl;
for(int i=0; i<10; i++)
cout<< "stack item:"<<stack.pop()<<endl;
system("pause");
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/macroan/stack.git
git@gitee.com:macroan/stack.git
macroan
stack
stack
master

搜索帮助