C++共享栈的模板实现
#pragma once
//共享栈的实现
#include <iostream>
using namespace std;
const int StackSize11 = 100;
template<typename DataType>
class BothStack
{
public:
BothStack(){ top1 = -1; top2 = StackSize11; }
~BothStack(){}
void Push(int i, DataType x);
DataType Pop(int i);
DataType GetTop(int i);
int Empty(int i);
private:
DataType data[StackSize11];
int top1, top2;
};
template<typename DataType>
void BothStack<DataType>::Push (int i, DataType x)
{
if (top1 == top - 1)
{
throw"上溢";
}
if (i == 1)
{
data[++top1] = x;
}
if (i == 2)
{
data[--top2] = x;
}
}
template<typename DataType>
DataType BothStack<DataType>::Pop (int i)
{
if (i == 1)
{
if (top1 == -1)
{
throw"下溢";
}
return data[top1 - 1]
}
if (i == 2)
{
if (top2 == StackSize11)
{
throw"下溢";
}
return data[top2++];
}
}
template<typename DataType>
DataType BothStack<DataType>::GetTop(int i)
{
if (i == 1)
{
if (top1 != -1)
{
return data[top1];
}
}
if (i == 2)
{
if (top2 != StackSize11)
{
return data[top2];
}
}
}
template<typename DataType>
int BothStack<DataType>::Empty (int i)
{
if (i == 1)
{
top1 == -1 ? return 1 : return 0;
```
}
if (i == 2)
{
top2 == StackSize11 ? return 1 : return 0;
}
```
}