#include <iostream>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef struct //队列的定义
{
char data[MAXSIZE];
int front;
int rear;
}Queue;
typedef struct //栈的定义
{
char data[MAXSIZE];
int top;
}SqStack;
typedef int status;
status InitQueue(Queue &Q) //队列的初始化
{
Q.front=Q.rear=0;
return OK;
}
bool IsEmptyQ(Queue &Q) //判断队列是否为空
{
if (Q.front==Q.rear)
return true;
else
return false;
}
bool IsOverQ(Queue &Q) //判断队列是否为满
{
if (((Q.rear)%MAXSIZE)==Q.front) //这里就想到钟表来理解
return true;
else
return false;
}
status InQueue(Queue &Q,int x) //入队
{
if (IsOverQ(Q)) //队满不入
return ERROR;
else
Q.data[Q.rear]=x;
Q.rear++;
}
status OutQueue(Queue &Q,int &x) //出队
{
if (IsEmptyQ(Q)) //队空不出
return ERROR;
else
printf("%d\n",Q.front);
Q.front++;
}
status InitSqStack(SqStack &S) //初始化栈
{
S.top=-1;
return OK;
}
bool IsEmptyS(SqStack &S) //判断栈是否为空
{
if (S.top==-1)
return true;
else
return false;
}
bool IsOverS(SqStack &S) //判断栈是否为满
{
if (S.top==MAXSIZE-1)
return true;
else
return false;
}
status InSqStack(SqStack &S,int e) //进栈
{
if (IsOverS(S)) //栈满不进
return ERROR;
else
S.top=e;
S.top++;
}
status OutSqStack(SqStack &S,int &e) //出栈
{
if (S.top==-1) //栈空不出
return ERROR;
else
S.top--;
e=S.top;
}
void ReserveQueue(Queue &Q)
{
SqStack S;
int x;
InitSqStack(S);
while(!IsEmptyQ(Q))
{
OutQueue(Q,x);
InSqStack(S,x);
}
while(!IsEmptyS(S))
{
OutSqStack(S,x);
InQueue(Q,x);
}
}
void Makemenu()
{
printf("\t----------------[队列的逆置]---------\n");
printf("\t\t\t0.退出\n");
printf("\t\t\t1.入队\n");
printf("\t\t\t2.出队\n");
printf("\t\t\t3.逆置\n");
printf("\t--------------------------------------\n");
printf("请输入你的选择(0-3):\n");
}
void Key()
{
}
int main()
{
Makemenu();
int n;
Queue Q;
int x;
while(1){
scanf("%d",&n);
switch(n)
{
case 0:
exit(0);
system("pause");
break;
case 1:
printf("---------入队---------\n");
printf("请输入队列元素:",x);
scanf("%d",&x);
InQueue(Q,x);
break;
case 2:
printf("---------出队---------\n");
OutQueue(Q,x);
break;
case 3:
printf("---------逆置---------\n");
break;
default:
printf("输入有误请重新输入:");
}
printf("请输入你的选择:");
}
ReserveQueue(Q);
return 0;
}
修改处见注释,供参考:
#include <stdio.h>
#include <windows.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef struct //队列的定义
{
char data[MAXSIZE];
int front;
int rear;
}Queue;
typedef struct //栈的定义
{
char data[MAXSIZE];
int top;
}SqStack;
typedef int status;
status InitQueue(Queue& Q) //队列的初始化
{
Q.front = Q.rear = 0;
return OK;
}
bool IsEmptyQ(Queue& Q) //判断队列是否为空
{
if (Q.front == Q.rear)
return true;
else
return false;
}
bool IsOverQ(Queue& Q) //判断队列是否为满
{
if ((Q.rear - Q.front) > MAXSIZE) //修改
//if (((Q.rear) % MAXSIZE) == Q.front) //这里就想到钟表来理解
return true;
else
return false;
}
status InQueue(Queue& Q, int x) //入队
{
if (IsOverQ(Q)) //队满不入
return ERROR;
else{
Q.data[Q.rear] = x; //修改
Q.rear++;
}
return OK;
}
status OutQueue(Queue& Q, int& x) //出队
{
if (IsEmptyQ(Q)) //队空不出
return ERROR;
else{
x = Q.data[Q.front]; //修改
//printf("%d\n", Q.front);
Q.front++;
}
return OK; //修改
}
status InitSqStack(SqStack& S) //初始化栈
{
S.top = -1;
return OK;
}
bool IsEmptyS(SqStack& S)//判断栈是否为空
{
if (S.top == -1)
return true;
else
return false;
}
bool IsOverS(SqStack& S) //判断栈是否为满
{
if (S.top == MAXSIZE - 1)
return true;
else
return false;
}
status InSqStack(SqStack& S, int e) //进栈
{
if (IsOverS(S)) //栈满不进
return ERROR;
else{
S.top++; //修改
S.data[S.top] = e; //修改
}
return OK; //修改
}
status OutSqStack(SqStack& S, int& e) //出栈
{
if (S.top == -1) //栈空不出
return ERROR;
else{
e = S.data[S.top]; //修改
S.top--; //修改
}
return OK; //修改
}
void ReserveQueue(Queue& Q)
{
SqStack S;
int x;
InitSqStack(S);
while (!IsEmptyQ(Q))
{
OutQueue(Q, x);
InSqStack(S, x);
}
while (!IsEmptyS(S))
{
OutSqStack(S, x);
InQueue(Q, x);
}
}
void Makemenu()
{
printf("\t----------------[队列的逆置]---------\n");
printf("\t\t\t0.退出\n");
printf("\t\t\t1.入队\n");
printf("\t\t\t2.出队\n");
printf("\t\t\t3.逆置\n");
printf("\t--------------------------------------\n");
printf("请输入你的选择(0-3):\n");
}
void print(Queue* Q) //修改
{
int i;
for (i = Q->front; i < Q->rear; i++)
printf("%d ", Q->data[i]);
printf("\n");
}
int main()
{
Makemenu();
int n, x;
Queue Q; //修改
InitQueue(Q);
SqStack S; //修改
InitSqStack(S);
while (1) {
scanf("%d", &n);
switch (n)
{
case 0:
exit(0);
system("pause");
break;
case 1:
printf("---------入队---------\n");
printf("请输入队列元素:", x);
scanf("%d", &x);
InQueue(Q, x);
break;
case 2:
printf("---------出队---------\n");
OutQueue(Q, x);
break;
case 3:
printf("---------逆置---------\n");
ReserveQueue(Q);
break;
default:
printf("输入有误请重新输入:");
break;
}
print(&Q); //修改
system("pause"); //修改
Makemenu(); //修改
printf("请输入你的选择:");
}
return 0;
}