代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。