利用两个栈s1,s2模拟一个队列,已知栈的三个运算定义如下:
push(ST,x):元素x入st栈
pop(ST,&x):st栈顶元素出栈,赋给变量x
isEmpty(ST):判断ST栈是否为空
//入队列
int enQueue(SqStack &s1,SqStack &s2,int x)
{
int y;
if(s1.top==maxsize-1)
{
if(!isEmpty(s2))
return 0;
else if(isEmpty(s2))
{
while(!isEmpty(s1))
{
pop(s1,y);
push(s2,y);
}
push(s1,x);
return 1;
}
}
else
{
push(s1,x);
return 1;
}
}
//出队列
int deQueue(SqStack &s2,Sqtack &s1,int &x)
{
int y;
if(!isEmpty(s2))
{
pop(s2,x);
return 1;
}
else if(isEmpty(s2))
{
if(isEmpty(s1))
return 0;
else
{
while(!isEmpty(s1))
{
pop(s1,y);
push(s2,y);
}
pop(s2,x);
return 1;
}
}
}
//判断队列是否为空
int isQueueEmpty(SqStack s1,SqStack s2)
{
if(isEmpty(s1)&&isEmpty(s2))
return 1;
else
return 0;
}
3669



被折叠的 条评论
为什么被折叠?



