VS2005和Dev均可以运行通过,如有问题,请各位大牛指正。
/*静态数组实现双向栈:两栈底在数组两端,栈顶相向,迎面增长,栈顶指针指向栈顶元素
栈空:左栈空 top[0]=-1; 右栈空top[0]=maxSize; 全栈空top[0]=-1&&top[0]=maxSize
栈满:栈顶相邻为栈满。top[1]-top[0]=1
栈顶初始值:两栈全空:左栈空 top[0]=-1; 右栈空top[0]=maxSize;
栈顶:总是指向刚刚压入的值
入左栈: data[++top[0]] = NewItem;
入右栈:data[--top[0]] = NewItem
出左栈:newItem = data[top[0]--];
出右栈:newItem = data[top[0]++];
*/
#include <iostream>
using namespace std;
const int MaxSize = 50;
template<class Type>
class DoubleStack
{
private:
Type data[MaxSize];
int top[2];
public:
DoubleStack();
DoubleStack(const DoubleStack<Type>& otherDoubleStack);
DoubleStack<Type> operator=(const DoubleStack<Type>& otherDoubleStack);
void initDoubleStack();
bool isEmptyLStack() const;
bool isEmptyRStack() const;
bool IsFullDoubleStack() const;;
void destoryDoubleStack();
void pop(int i,Type& data);
void push(int i,Type data);
};
template<class Type>
DoubleStack<Type>::DoubleStack()
{
top[0]=-1;
top[1]=MaxSize;
}
template<class Type>
DoubleStack<Type>::DoubleStack(const DoubleStack<Type>& otherDoubleStack)
{
top[0] = otherDoubleStack.top[0];
top[1] = otherDoubleStack.top[1];//栈顶的赋值应该写在if之外,因为要避免空栈时,栈顶得不到值,为随机数
if (!otherDoubleStack.isEmptyLStack() && !otherDoubleStack.isEmptyRStack())
{
memcpy(data,otherDoubleStack.data,MaxSize);
}
}
template<class Type>
DoubleStack<Type> DoubleStack<Type>::operator=(const DoubleStack<Type>& otherDoubleStack)
{
if (this != &otherDoubleStack)
{
if (!otherDoubleStack.isEmptyLStack() && !otherDoubleStack.isEmptyRStack())
{
top[0] = otherDoubleStack.top[0];
top[1] = otherDoubleStack.top[1];//栈顶的赋值可以写在if内外,因为创建对象时,已经调用构造函数,为栈顶赋值了
memcpy(data,otherDoubleStack.data,MaxSize);
}
}
return *this;
}
template<class Type>
void DoubleStack<Type>::initDoubleStack()
{
top[0] = -1;
top[1] = MaxSize;
}
template<class Type>
bool DoubleStack<Type>::isEmptyLStack() const
{
return (top[0]==-1);
}
template<class Type>
bool DoubleStack<Type>::isEmptyRStack() const
{
return (top[0]==MaxSize);
}
template<class Type>
bool DoubleStack<Type>::IsFullDoubleStack() const
{
return (top[1]-top[0]==1);//top表示指针位置,最大为Max-1
}
template<class Type>
void DoubleStack<Type>::destoryDoubleStack()
{
top[0] = -1;
top[1] = MaxSize;
}
template<class Type>
void DoubleStack<Type>::pop(int i,Type& newItem)
{
if (i<0 || i>1)
{
cout<<"栈号输入不对!"<<endl;
return;
}
switch(i)
{
case 0:
if (top[0]==-1)
{
cout<<"栈空!"<<endl;
return;
}
else
{
newItem = data[top[0]--];
}
break;
case 1:
if (top[1]==MaxSize)
{
cout<<"栈空!"<<endl;
return;
}
else
{
newItem = data[top[1]++];//出栈,栈顶往上走
}
break;
}
}
template<class Type>
void DoubleStack<Type>::push(int i,Type newItem)
{
if (i<0 || i>1)
{
cout<<"栈号错误!"<<endl;
return;
}
if (IsFullDoubleStack())
{
cout<<"栈满!"<<endl;
return;
}
else
{
switch(i)
{
case 0:
data[++top[0]]=newItem;
break;
case 1:
data[--top[1]]=newItem;//入栈,栈顶往下走
break;
}
}
}
int main()
{
DoubleStack<int> s;
DoubleStack<int> s1;
s1=s;
int a[4]={1,2,3,4};
for (int i=0;i<4;i++)//入左栈
{
s1.push(0,a[i]);
}
for (int i=0;i<4;i++)//入右栈
{
s1.push(1,a[i]);
}
for (int i=0;i<4;i++)//出左栈
{
int temp;
s1.pop(0,temp);
cout<<temp<<endl;
}
for (int i=0;i<4;i++)//出右栈
{
int temp;
s1.pop(1,temp);
cout<<temp<<endl; //由于没做限定,栈满时,会连续data
}
system("pause");
return 0;
}