栈只允许在末端进行插入和删除的线性表。栈具有后进先出的特性(LIFO,Last In First Out)。
#pragma once
#include<assert.h>
template<class T>
class Stack
{
public:
Stack()
:_array(NULL)
, _capicity(0)
, _topIndex(-1)
{}
~Stack()
{
delete[]_array;
_capicity = 0;
_topIndex = -1;
}
void Push(const T& x)//入栈
{
if (_topIndex + 1 == _capicity)
{
_capicity = 2 * _capicity + 3;
T* tmp = new T[_capicity];
if (tmp == NULL)
{
cout << " New Failed!" << endl;
exit(-1);
}
memcpy(tmp,_array,sizeof(T)*(_topIndex+1));
delete[] _array;
_array = tmp;
}
_array[++_topIndex] = x;
}
void Pop()//出栈
{
assert(_topIndex > -1);
--_topIndex;
}
bool Empty()
{
return _topIndex == -1;
}
bool Full() const
{
return _topIndex == _capicity - 1;
}
const T& Top()
{
return _array[_topIndex];
}
private:
T* _array; //数据块
size_t _capicity; //容量
int _topIndex; //栈顶数据的下标
};
#include<iostream>
#include"Stack.hpp"
using namespace std;
void Test()
{
Stack<int> s;
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Pop();
while (!s.Empty())
{
cout << s.Top() << " ";
s.Pop();
}
cout << endl;
}
int main()
{
Test();
return 0;
}