问题描述:
该队列只有一个队头指针front,不设队尾指针rear,而设置一个计数器count用以记录队列中结点的个数。编写算法实现队列的
基本运算:判空、入队和出队等。
基本要求及提示:
(1)从键盘输入字符以$结束,建立循环队列,并显示结果。
(2)从键盘输入1个元素,执行入队操作,并显示结果。
(3)将队头元素出队,并显示结果。
(4)判断队列是否为空。
(5)判断队列是否为满。
要求程序通过一个主菜单进行控制,在主菜单界面通过选择菜单项的序号来调用各功能函数。
代码实现
#include<stdio.h>
#include <malloc.h>
#define TRUE 0
#define FALSE 1
#define MAXSIZE 5
int count = 0;
//结构体定义
typedef struct {
int data[MAXSIZE];
int front;
} CycQueue;
//函数声明
int menu_select();
void InitQueue(CycQueue **);
int EnQueue(CycQueue *, int);
void EnterCycQueue(CycQueue *);
int DeCycQueue(CycQueue *, int *);
void DeleteCycQueue(CycQueue *);
int IFCycQueueEmpty();
int IFCycQueueFull();
void PrintCycQueue(CycQueue *);
// main()函数
int main() {
CycQueue *Q;
InitQueue(&Q);
for (;;) {
switch (menu_select()) {
case 1:
printf("1.循环队列的入队\n");
EnterCycQueue(Q);
break;
case 2:
printf("2.循环队列的出队\n");
DeleteCycQueue(Q);
break;
case 3:
printf("3.循环队列的判空\n");
IFCycQueueEmpty();
break;
case 4:
printf("4.循环队列的判满\n");
IFCycQueueFull();
break;
case 5:
printf("5.循环队列的输出\n");
PrintCycQueue(Q);
break;
case 0:
printf("再见!\n");
free(Q);
return 0;
}
}
} // main()函数结束
//菜单驱动程序
int menu_select() //菜单
{
int sn;
printf(" 欢迎来到队列管理系统!\n");
printf("===========================\n");
printf(" 1.循环队列的入队\n");
printf(" 2.循环队列的出队\n");
printf(" 3.循环队列的判空\n");
printf(" 4.循环队列的判满\n");
printf(" 5.循环队列的输出\n");
printf(" 0.退出系统\n");
printf("===========================\n");
printf("请输入你的选择(0~5):");
for (;;) {
scanf("%d", &sn);
getchar();
if (sn < 0 || sn > 5)
printf("\n您的选择有误,请重新选择(0~5):");
else
break;
}
return sn;
}
/*
TODO:
功能描述:初始化队列操作
1、构造一个空队列(CycQueue *) malloc(sizeof(CycQueue))
,将front指针均指向 0
参数:(CycQueue **Q) 队列结构的指针的指针
返回值:void
*/
void InitQueue(CycQueue **Q) {
*Q=(CycQueue *) malloc(sizeof(CycQueue));
(*Q)->front=0;
}
/*
TODO:
功能描述:执行入队操作
1、判断队满 返回:return FALSE
2、实现队列一个元素入队操作 返回:TRUE
参数:(CycQueue *q, int e) 队列结构的指针与入队元素值
返回值:int (TRUE或FALSE)
*/
int EnQueue(CycQueue *q, int e) {
q->data[count]=e;
count++;
}
void EnterCycQueue(CycQueue *q) {
int i, e;
char end;
for (;;) {
if (count == MAXSIZE) {
printf("队列上溢出!\n");
} else{
e=0;
printf("请输入e的值:");
scanf("%d", &e);
}
if (getchar() != '$') {
EnQueue(q,e);
for (i = 0; i < count; i++)
printf("%4d", q->data[i]);
printf(">>>队列中元素数:%d\n", count);
} else {
break;
}
}
return;
}
/*
TODO:
功能描述:执行出队操作
1、判断队空 返回:return FALSE
2、实现队列一个元素出队操作 返回:return TRUE
参数:(CycQueue *q, int *e) 队列结构的指针与出队元素值
返回值:int (TRUE或FALSE)
*/
int DeCycQueue(CycQueue *q, int *e) {
if(0==count)return FALSE;
else{
*e=q->data[q->front];
q->front++;
count--;
return TRUE;
}
}
void DeleteCycQueue(CycQueue *q) {
int i, j, e, rear;
if (q->front == count) {
printf("队列为空!\n");
return;
}
rear = count;
for (i = count; i > 0; i--) {
if (0 == count) {
printf("队空下溢出!\n");
q->front = 0;
return;
} else {
DeCycQueue(q, &e);
printf("出对元素:%d\n", e);
}
if (q->front != rear)
printf("队列中的元素:");
for (j = q->front; j < rear; j++) {
printf("%4d", q->data[j]);
}
printf("\n");
}
return;
}
/*
TODO:
功能描述:判断队列是否为队空。
通过程序上下文分析添加队空条件
队空,返回 return FALSE
非空,返回 return TRUE
参数:void
返回值:int (TRUE或FALSE)
*/
//判断队列是否为空
int IFCycQueueEmpty() {
if ( count==0) { //TODO:添加条件
printf("队列为空!\n");
return FALSE;
} else {
printf("队列非空!\n");
return TRUE;
}
}
/*
TODO:
功能描述:判断队列是否为队满。
通过程序上下文分析添加队满条件
队满,返回 return FALSE
非满,返回 return TRUE
参数:void
返回值:int (TRUE或FALSE)
*/
//判断队列是否为满
int IFCycQueueFull() {
if (count>=MAXSIZE) { //TODO:添加条件
printf("队列已满!\n");
return FALSE;
} else {
printf("队列非满!\n");
return TRUE;
}
}
//输出
void PrintCycQueue(CycQueue *q) {
int i;
if (0 == count) {
printf("队列为空!\n");
return;
} else {
for (i = 0; i < count; i++) {
printf("%4d", q->data[i]);
}
printf(">>>队列中的元素数:%d\n", count);
}
}