📄 队列的应用.txt
字号:
// exp_2_2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "malloc.h"
#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int status;
typedef int QElemType;
#define MAXQSIZE 4
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
status InitQueue(SqQueue &Q) //构造空队列
{
Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
if(!Q.base) exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
int QueueLength(SqQueue Q) //求队队列中的元素个数
{
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
status EnQueue(SqQueue &Q,QElemType e) //入队
{
if((Q.rear+1)%MAXQSIZE==Q.front)
return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
return OK;
}
status DeQueue(SqQueue &Q,QElemType &e)// 出队
{
if(Q.front==Q.rear)
return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return OK;
}
void Visit_Q(SqQueue Q)
{
int i=Q.front;
printf("\n\t当前队列状态: ");
if(Q.front==Q.rear)
printf("队列为空\n\n");
else
{
printf("队首->");
while(i!=Q.rear)
{
printf("%3d",Q.base[i]);
i=(i+1)%MAXQSIZE;
}
printf("\t<-队尾\n\n");
}
}
void main(int argc, char* argv[])
{
SqQueue QQ;
InitQueue(QQ);
QElemType e;
int c;
Visit_Q(QQ);
printf("入队选1,出队选2,结束选3\n");
scanf("%d",&c);
while(1)
{
if(c==1)
{
printf("请输入入队元素的值(十进制整数)\n");
scanf("%d",&e);
if(EnQueue(QQ,e))
printf("元素%d入队,当队列中已有%d个元素\n",e,QueueLength(QQ));
else
printf("!!!队列已满,%d入队失败,请先执行出队\n",e);
}
else
if(c==2)
{
if(DeQueue(QQ,e))
printf("元素%d出队,当前队列中还有%d个元素\n",e,QueueLength(QQ));
else
printf("!!!队列已空,无法执行出队操作,请先执行入队\n");
}
else
if(c==3)
break;
else
printf("重新选择\n");
Visit_Q(QQ);
printf("入队选1,出选2,结束3\n");
scanf("%d",&c);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -