第3章 栈和队列
3.2 队列
综合应用题 第3题
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define MaxSize 10
using namespace std;
typedef int ElemType;
typedef struct{
ElemType stack[MaxSize];
int top;
}SqStack;
void InitStack(SqStack &S)
{
S.top=-1;
}
int Push(SqStack &S,ElemType x)
{
if(S.top==MaxSize-1)
{
cout<<"栈满"<<endl;
exit(0);
}
S.stack[++S.top]=x;
return 1;
}
int Pop(SqStack &S,ElemType &x)
{
if(S.top==-1)
{
cout<<"栈空"<<endl;
return 0;
}
x=S.stack[S.top--];
return 1;
}
bool StatckEmpty(SqStack &S)
{
if(S.top==-1)
return true;
else
return false;
}
bool StatckOverFlow(SqStack &S)
{
if(S.top==MaMaxSize-1)
return true;
else
return false;
}
int EnQueuel(SqStack &S1,SqStack &S2,ElemType x)
{
if(!StatckOverFlow(S1))
{
Push(S1,x);
return 1;
}
else if(StatckEmpty(S2))
{
while(!StatckEmpty(S1))
{
Pop(S1,x);
Push(S2,x);
}
Push(S1,x);
return 1;
}
else
{
cout<<"队列已满!"<<endl;
return 0;
}
}
void DeQueuel(SqQueue &Q)
{
if(!StatckEmpty(S2))
{
Pop(S2,x);
}
else if(!StatckEmpty(S1))
{
while(!StatckEmpty(S1))
{
Pop(S1,x);
Push(S2,x);
}
Pop(S2,x);
}
else
cout<<"队列为空!"<<endl;
}
bool QueueEmpty(SqQueue &Q)
{
if(StatckEmpty(S1)&&StatckEmpty(S2))
return true;
else
return false;
}
int main() {
SqStack S1,S2;
InitStack(S1);
InitStack(S2);
return 0;
}