📄 队列.cpp
字号:
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<stdlib.h>
#define max 100
typedef struct
{
int *base;
int front;
int rear;
}queue;
queue Q;
void make()
{
Q.base=(int*)malloc(max*sizeof(int));
if(!Q.base)printf("分配空间失败!\n");
else Q.front=Q.rear=0;
}
void empty()
{
if((Q.front==Q.rear)&&((Q.rear+1)%max!=Q.front))printf("队列为空!\n");
else printf("队列不为空!\n");
}
int length()
{
return (Q.rear-Q.front+max)%max;
}
void enqueue()
{
int e;
printf("请输入入列元素值:");
scanf("%d",&e);
if((Q.rear+1)%max==Q.front)printf("队列已满!\n");
else
{
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%max;
}
}
void delqueue()
{
int e;
if(Q.front==Q.rear)printf("队列为空!\n");
else
{
e=Q.base[Q.front];
Q.front=(Q.front+1)%max;
printf("旧元素%d出列\n",e);
}
}
void gettop()
{
int e;
if(Q.front==Q.rear)printf("队列为空!\n");
else
{
e=Q.base[Q.front];
printf("队列头元素为%d\n",e);
}
}
void print()
{
int k=0,l;l=length();
if(Q.front==Q.rear)printf("队列为空!\n");
while(Q.rear!=Q.front&&k<l)
{
printf("%-5d",Q.base[Q.front+k]);
k++;
}
printf("\n");
}
void main()
{
int choice;
make();
while(1)
{
printf("1.判断是否为空\n2.新元素入列\n3.旧元素出列\n4.输出队列头元素\n5.输出队列所有元素\n0-终止程序\n");
choice=getch();
if (choice=='1')empty();
else if(choice=='2')enqueue();
else if(choice=='3')delqueue();
else if(choice=='4')gettop();
else if(choice=='5')print();
else if(choice=='0')break;
printf("按任意键返回!\n");
getch();system("cls");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -