TA的每日心情 | 开心 2021-12-13 21:45 |
---|
签到天数: 15 天 [LV.4]偶尔看看III
|
循环队列源代码(C语言版)
/*******简单实现了插入,删除,长度,退出等功能********/
- #include<stdio.h>
- #include<stdlib.h>
- #define MAXSIZE 100
- //定义队列结构
- typedef struct Queue{
- int * base;
- int front;
- int rear;
- }SQueue;
- //初始化100个数字的队列
- void InitQueue(SQueue * Q){
- Q->base=(int *)malloc(MAXSIZE*sizeof(int));
- Q->front=Q->rear=0;
- }
- //返回队列长度
- void QueueLength(SQueue * Q){
- printf("此队列的长度为:%d\n",(Q->rear-Q->front+MAXSIZE)%MAXSIZE);
- }
- //插入节点
- void InsertQueue(SQueue * Q,int e){
- if((Q->rear+1)%MAXSIZE==Q->front)
- printf("队列已满!\n");
- else
- {
- Q->base[Q->rear]=e;
- Q->rear=(Q->rear+1)%MAXSIZE;
- }
- }
- //删除节点
- DeleteQueue(SQueue * Q){
- if(Q->rear==Q->front)
- printf("队列已空!\n");
- else{
- Q->front=(Q->front+1)%MAXSIZE;
- printf("删除完成!\n");
- }
- }
- //打印队列
- void PrintQueue(SQueue Q){
- printf("________________________________________________________\n");
- while(Q.front!=Q.rear){ //此处留了一个标志位,所以不用再打印最后一个数。
- printf("%d ",Q.base[Q.front]);
- Q.front=(Q.front+1)%MAXSIZE;
- }
- printf("\n————————————————————————————\n");
- }
- //主函数
- int main(){
- SQueue M;int i,x,v;
- InitQueue(&M);
- for(i=1;i<=12;i++){
- InsertQueue(&M,i);
- }
- printf("十二个数字的队列已经建立完成!\n");
- PrintQueue(M);
- printf("请输入序号进行操作:1.插入 2.删除 3.长度 4.退出\n");
- scanf("%d",&x);
- while(x!=0){
- switch(x){
- case 1:printf("请输入要插入的值:");
- scanf("%d",&v);
- InsertQueue(&M,v);
- PrintQueue(M);
- break;
- case 2:DeleteQueue(&M);
- PrintQueue(M);
- break;
- case 3:printf("正在计算长度......\n");
- QueueLength(&M);
- break;
- case 4:exit(0);
- default:printf("输入有误,请重新输入!\n");
- }
- printf("请输入序号进行操作:1.插入 2.删除 3.长度 4.退出\n");
- scanf("%d",&x);
- }
- return 0;
- }
复制代码
程序截图:



联系邮箱:xhsgg12302@outlook.com
2016_12_28
|
|