[题目]
e.循环队列(即队列的顺序存储结构)实现。
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20 /* 数组最大界限 */
typedef int ElemType; /* 数据元素类型 */
typedef struct
{ ElemType a[MAXSIZE]; /* 一维数组子域 */
int front,rear; /* 头、尾指针子域 */
}SqQueue; /* 循环队列的结构体类型 */
SqQueue Q1;
/* 函数声明 */
void init_Q(SqQueue *Q);
void out_Q(SqQueue Q);
void EnQueue(SqQueue *Q,ElemType e);
ElemType DeQueue(SqQueue *Q);
/* 主函数 */
main()
{ int k; ElemType e,x; char ch;
init_Q( &Q1); /* 初始化一个空循环队列 */
do { printf("\n\n\n");
printf("\n\n 1. 数据元素e进队列 ");
printf("\n\n 2. 出队一个元素,返回其值");
printf("\n\n 3. 结束程序运行");
printf("\n======================================");
printf("\n 请输入您的选择 (1,2,3)");
scanf("%d",&k);
switch(k)
{ case 1:{ printf("\n 进队 e=?"); scanf("%d",&e);
EnQueue(SqQueue &Q1,e); out_Q(Q1);
} break;
case 2:{ x= DeQueue(&Q1);
printf("\n出队元素 : %d", x);
out_Q(Q1 );
} break;
case 3: exit(0);
} /* switch */
printf("\n ----------------");
}while(k>=1 && k<3);
printf("\n 再见!");
printf(“\n 打回车键,返回。“); ch=getch();
} /* main */
/* 初始化空队列 * /
/* 输出队列的内容 */
/* 不能修改队列头、尾指针 */
/ * 进队函数 */
/* 出队函数 */
c代码实现
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20 /* 数组最大界限 */
#include <string.h>
#include <conio.h>
typedef int ElemType; /* 数据元素类型 */
typedef struct
{
ElemType a[MAXSIZE]; /* 一维数组子域 */
int front,rear; /* 头、尾指针子域 */
}SqQueue; /* 循环队列的结构体类型 */
SqQueue Q1;
/* 函数声明 */
void init_Q(SqQueue *Q);
void out_Q(SqQueue Q);
void EnQueue(SqQueue *Q,ElemType e);
ElemType DeQueue(SqQueue *Q);
int main()
{
int k;
ElemType e,x;
char ch;
init_Q( &Q1); /* 初始化一个空循环队列 */
do { printf("\n\n\n");
printf("\n\n 1. 数据元素e进队列 ");
printf("\n\n 2. 出队一个元素,返回其值");
printf("\n\n 3. 结束程序运行");
printf("\n======================================");
printf("\n 请输入您的选择 (1,2,3)");
scanf("%d",&k);
switch(k)
{ case 1:{ printf("\n 进队 e=?"); scanf("%d",&e);
EnQueue(&Q1,e);
out_Q(Q1);
} break;
case 2:{ x= DeQueue(&Q1);
printf("\n出队元素 : %d", x);
out_Q(Q1 );
} break;
case 3: exit(0);
} /* switch */
printf("\n ----------------");
}while(k>=1 && k<3);
printf("\n 再见!");
printf("\n 打回车键,返回。");
ch=getch();
return 0;
}
void init_Q(SqQueue *Q){
memset(Q->a,0,sizeof(Q->a));
Q->front=0;
Q->rear=0;
}
void out_Q(SqQueue Q){
printf("\n");
for(int i=Q.front;i<Q.rear;i++){
printf("%d,",Q.a[i]);
}
}
void EnQueue(SqQueue *Q,ElemType e){
Q->a[Q->rear]=e;
(Q->rear)++;
}
ElemType DeQueue(SqQueue *Q){
int t;
if(Q->front!=Q->rear){
t=Q->a[Q->front];
Q->front++;
return t;
}
printf("无元素");
exit(-1);//非正常退出 队列中无元素
}
转载:https://blog.csdn.net/weixin_42970433/article/details/102489384
查看评论