#pragma once
#include<iostream>
using std::cout;
using std::endl;
using Position = int;
template<typename T>
class Stack
{
public:
Stack(int MaxSize=1000);
const bool& IsFull();
const bool& IsEmpty();
const bool& Push(const T& X);
T& Pop();
~Stack();
private:
T* Data;
Position Top;
int Maxsize;
};
template<typename T>
Stack<T>::Stack(int MaxSize) :Data{ new T[MaxSize] }, Top(-1), Maxsize{ MaxSize } {}
template<typename T>
Stack<T>::~Stack()
{
delete Data;
}
template<typename T>
const bool& Stack<T>::IsFull()
{
if (Top == Maxsize - 1)
return true;
else
return false;
}
template<typename T>
const bool& Stack<T>::IsEmpty()
{
if (Top == -1)
return true;
else
return false;
}
template<typename T>
const bool& Stack<T>::Push(const T& X)
{
if (IsFull())
return false;
else
Data[++Top] = X;
}
template<typename T>
T& Stack<T>::Pop()
{
if (!IsEmpty())
return Data[Top--];
}
#include"顺序栈.h"
int main()
{
Stack<int> S{ 100 };
cout << "栈是否满" << S.IsFull() << endl;
cout << "栈是否空" << S.IsEmpty() << endl;
S.Push(1234);
S.Push(4399);
cout << S.Pop() << endl;
cout << "栈是否满" << S.IsFull() << endl;
cout << "栈是否空" << S.IsEmpty() << endl;
cout << S.Pop() << endl;
return 0;
}