注意循环队列输出函数中 q.front不能改变是什么意思
代码可以完整运行#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define MAXQSIZE 100
typedef int Status;
typedef int QElemType;
typedef struct
{ //结点类型QNode,指针类型QueuePtr
QElemType *base; //初始化的动态分配存储空间
int front; //头指针,若队列不空,指向队列头元素
int rear;//尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue; //循环队列
typedef struct QNode
{ //链队列类型
QElemType data;
struct QNode *next;
} QNode,*QueuePtr;//链队
typedef struct
{
QueuePtr front; //队头指针
QueuePtr rear; //队尾指针
}LinkQueue;//声明链队的类型
Status InitQueue_S( SqQueue &SQ )//初始化循环队列
{//构造一个空队列 SQ
SQ.base = ( QElemType * ) malloc (MAXQSIZE * sizeof ( QElemType ) );
if ( !SQ.base )
return OVERFLOW;//存储分配失败
SQ.front = SQ.rear = 0;
return OK;
}
Status EnQueue_S( SqQueue &SQ , QElemType e )//入列
{//插入元素e为Q的新的队尾元素
if( ( SQ.rear + 1 ) % MAXQSIZE == SQ.front )
{
printf("队列已满!");
return ERROR;
}
SQ.base[ SQ.rear ] = e;
SQ.rear = ( SQ.rear+1 ) % MAXQSIZE;
return OK;
}
Status DeQueue_S( SqQueue &SQ , QElemType &e )//出列
{//若队列不空,则删除SQ的队头元素,用e返回其值,并返回OK;否则返回ERROR
if( SQ.front == SQ.rear )
{
printf("队列空!");
return ERROR;
}
e = SQ.base[ SQ.front ];
SQ.front = ( SQ.front+1 ) % MAXQSIZE;
return OK;
}
Status QueueEmpty_S( SqQueue SQ )//判断队空
{
return SQ.front == SQ.rear;
}
void PrintQueue_S( SqQueue SQ )//遍历队列
{
if( SQ.rear == SQ.front )
printf("ERROR!");
while( !QueueEmpty_S(SQ) )
{
printf(" %d ",SQ.base[ SQ.front ] );
SQ.front = ( SQ.front + 1 ) % MAXQSIZE;
}
printf("\n");
}
Status InitQueue_L( LinkQueue &LQ )//初始化链队
{//构造一个空队列LQ
LQ.front = LQ.rear = ( QueuePtr ) malloc( sizeof( QNode ) );
if( !LQ.front )
return OVERFLOW;//存储分配失败
LQ.front->next = NULL;
return OK;
}
Status EnQueue_L( LinkQueue &LQ , QElemType e )//入列
{ //插入元素e为Q的新的队尾元素
QueuePtr p;
p = ( QueuePtr ) malloc ( sizeof (QNode) );
if( !p )
return OVERFLOW;//存储分配失败
p->data = e;
p->next = NULL;
LQ.rear->next = p;
LQ.rear = p;
return OK;
}
Status DeQueue_L( LinkQueue &LQ , QElemType &e )//出队
{//若队列不空,则删除LQ的队头元素,用e返回其值,并返回OK,否则返回ERROR
QueuePtr p;
if( LQ.front == LQ.rear )
return ERROR;
p = LQ.front->next;
e = p->data;
LQ.front->next = p->next;
if( LQ.rear == p )
LQ.rear = LQ.front;
free(p);
return OK;
}
Status QueueEmpty_L( LinkQueue LQ )//判断队空
{
return LQ.front->next == NULL;
}
void PrintQueue_L( LinkQueue LQ )//遍历队列
{
QueuePtr p;
p = LQ.front->next;
while( p )
{
printf(" %d ",p->data );
p = p->next;
}
printf("\n");
}
void menu1()//程序操作菜单
{
printf("|==================================|\n");
printf("| ****---- 选择式菜单 ----**** |\n");
printf("|==================================|\n");
printf("| |\n");
printf("| 1-循环队列 |\n");
printf("| |\n");
printf("| 2-链队 |\n");
printf("| |\n");
printf("| 0-退出菜单 |\n");
printf("| |\n");
printf("|==================================|\n");
}//menu1
void menu2()//程序操作菜单
{
printf("|==================================|\n");
printf("| ****---- 选择式菜单 ----**** |\n");
printf("|==================================|\n");
printf("| |\n");
printf("| 1-初使化队 |\n");
printf("| |\n");
printf("| 2-入队操作 |\n");
printf("| |\n");
printf("| 3-出队操作 |\n");
printf("| |\n");
printf("| 4-判断队空 |\n");
printf("| |\n");
printf("| 5-遍历队列 |\n");
printf("| |\n");
printf("| 0-返回上一层 |\n");
printf("| |\n");
printf("|==================================|\n");
}//menu2
int main()
{
int i,k,e;
SqQueue SQ;
LinkQueue LQ;
int exitflag1=1,exitflag2;
while(exitflag1)
{
menu1();
printf("请输入菜单选项(0-2):");
scanf("%d",&i);
switch(i){
case 0:
system("cls");
exitflag1=0;
break;
case 1:
exitflag2=1;
system("cls");
while(exitflag2)
{
menu2();
printf("请输入菜单选项(0-5):");
scanf("%d",&k);
switch(k){
case 0:
system("cls");
exitflag2=0;
break;
case 1:
system("cls");
if( InitQueue_S(SQ) )
printf("循环队列初使化成功!\n");
else
printf("循环队列初使化失败!\n");
break;
case 2:
system("cls");
printf("请输入入队元素: ");
scanf("%d",&e);
if( !QueueEmpty_S(SQ) )
{
printf("元素 %d 入队前,此队列为:",e);
PrintQueue_S(SQ);
EnQueue_S(SQ,e);
printf("元素 %d 入队后,此队列为:",e);
PrintQueue_S(SQ);
}
else
{
printf("元素 %d 入队前,此队列为空队!\n",e);
EnQueue_S(SQ,e);
printf("元素 %d 入队后,此队列为:",e);
PrintQueue_S(SQ);
}
break;
case 3:
system("cls");
if( !QueueEmpty_S(SQ) )
{
printf("元素出队前,");
PrintQueue_S(SQ);
DeQueue_S(SQ,e);
printf("队首元素 %d 出队,出队后",e);
PrintQueue_S(SQ);
}
else
printf("操作失败,此队列是非空队!\n");
break;
case 4:
system("cls");
if( QueueEmpty_S(SQ) )
printf("队列为空队!\n");
else
printf("队列是非空队!\n");
break;
case 5:
system("cls");
if( !QueueEmpty_S(SQ) )
{
printf("当前循环队列为:");
PrintQueue_S(SQ);
}
else
printf("操作失败,此队列是非空队!\n");
break;
default:
system("cls");
printf("\n你输入的操作号非法,请重试!\n");
}
}
break;
case 2:
exitflag2=1;
system("cls");
while(exitflag2)
{
menu2();
printf("请输入菜单选项(0-5):");
scanf("%d",&k);
switch(k){
case 0:
system("cls");
exitflag2=0;
break;
case 1:
system("cls");
if(InitQueue_L(LQ))
printf("链队初使化成功!\n");
else
printf("链队初使化失败!\n");
break;
case 2:
system("cls");
printf("请输入入队元素: ");
scanf("%d",&e);
if( !QueueEmpty_L(LQ) )
{
printf("元素 %d 入队前,此队列为:",e);
PrintQueue_L(LQ);
EnQueue_L(LQ,e);
printf("元素 %d 入队后,此队列为:",e);
PrintQueue_L(LQ);
}
else
{
printf("元素 %d 入队前,此队列为空队!\n",e);
EnQueue_L(LQ,e);
printf("元素 %d 入队后,此队列为:",e);
PrintQueue_L(LQ);
}
break;
case 3:
system("cls");
if( !QueueEmpty_L(LQ) )
{
printf("元素出队前,");
PrintQueue_L(LQ);
DeQueue_L(LQ,e);
printf("队首元素 %d 出队,出队后",e);
PrintQueue_L(LQ);
}
else
printf("操作失败,此队列是非空队!\n");
break;
case 4:
system("cls");
if( QueueEmpty_L(LQ) )
printf("队列是空队!\n");
else
printf("队列是非空队!\n");
break;
case 5:
system("cls");
if( !QueueEmpty_L(LQ) )
{
printf("当前链队为:");
PrintQueue_L(LQ);
}
else
printf("操作失败,此队列是非空队!\n");
break;
default:
system("cls");
printf("\n你输入的操作号非法,请重试!\n");
}
}
break;
default:
system("cls");
printf("\n你输入的操作号非法,请重试!\n");
}
}
return 0;
}