已知Q是一个非空队列,S是一个空栈。仅使用少量工作变量以及对队列和栈的基本操作,编写一个算法,将队列Q中的所有元素逆置。
在这里插入代码片
#include<iostream>
#define MaxSize 1000
using namespace std;
typedef int ElemType;
typedef struct SqStack
{
ElemType date[MaxSize];
int top;
};
void creating(SqStack &SQ,int N)
{
for (i = 0; i < N; i++)
{
cin >> aa;
SQ.top++;
SQ.date[SQ.top] = aa;
}
}
void Display(SqStack &SQ)
{ while (SQ.top != -1)
{
cout << SQ.date[SQ.top]<<' ';
SQ.top--;
}
}
int main()
{ SqStack sq;
sq.top = -1;
int n; cin >> n;
creating(sq,n);
Display(sq); return 0;
}