/**为了充分利用空间,将两个栈共同存储在长度为年n的一唯数组中,共享数组空间。
设计两个栈共享一维数组的存储表示方式,画出示意图。给出定义该双栈的结构类新DStack,
并实现ADT3-1中定义的各栈运算,允许在各栈运算定义中,增加用以指明当前对双栈中哪一
个栈进行操作的参数**/
#include
using namespace std;
enum ResultCode{UnderFlow,OverFlow};
template
class Stack
{
public:
virtual bool IsEmpty(bool i) const=0;
virtual bool IsFull() const=0;
virtual T Top(bool i) const=0;
virtual void Pop(bool i)=0;
virtual void Push(const T &x,bool i)=0;
};
template
class DStack:public Stack
{
public:
DStack(int mSize);
~DStack(){delete[] s;}
bool IsEmpty(bool i) const
{
if(i==LOW)
return (Lt==-1);
else
return (Ht==maxSize);
}
bool IsFull() const
{
return (Lt==Ht);
}
T Top(bool i) const;
void Pop(bool i);
void Push(const T &x,bool i);
private:
bool LOW,RIGHT;
int maxSize;
int Lt,Ht;
T* s;
};
template
DStack::DStack(int mSize)
{
maxSize=mSize;
Lt=-1,Ht=maxSize;
LOW=false,RIGHT=true;
s=new T[maxSize];
}
template
void DStack::Pop(bool i)
{
if(i==LOW)
{
if (Lt==-1) throw UnderFlow;
Lt--;
}
else
{
if (Ht==maxSize) throw UnderFlow;
Ht++;
}
}
template
T DStack::Top(bool i) const
{
if(i==LOW)
{
if(IsEmpty(i)) throw UnderFlow;
return s[Lt];
}
else
{
if(IsEmpty(i)) throw UnderFlow;
return s[Ht];
}
}
template
void DStack::Push(const T &x,bool i)
{
if(IsFull()) throw OverFlow;
if(i==LOW)
s[++Lt]=x;
else
s[--Ht]=x;
}
int main()
{
try
{
DStack temp(10);
for(int i=0;i<8;i++)
temp.Push('c',0);
temp.Push('b',1);
temp.Push('b',1);
cout<<"the list is Full?:"< cout<<"low of list is:"< cout<<"Hight of list is:"< for(i=0;i<8;i++)
temp.Pop(0);
cout<<"The low list is empty?:"< cout<<"The low list is empty?:"< }
catch(ResultCode err)
{
switch(err)
{
case UnderFlow:cout<<"UnderFlow"< case OverFlow: cout<<"OverFlow"< }
}
}
阅读(1608) | 评论(0) | 转发(0) |