#include<stdio.h>
#define maxsize 10
typedef int ElemType;
typedef struct quene{
ElemType data[maxsize];
int top;
int tail;
}quene;
void init_quene(quene *q){
q->top = -1;
q->tail = 0;
}
void create_quene(quene *q){
int n,i;
printf("请输入你想创建几个数据\n");
scanf("%d",&n);
if(n<=0||n>maxsize){
printf("您想创建的队列总数有误\n");
}else{
for(i=1;i<=n;i++){
printf("请输入第%d个数字\n",i);
scanf("%d",&q->data[q->top+1]);
q->top++;
}
}
}
int judge_full(quene *q){
if(q->top == maxsize-1&&q->tail == 0){
return 1;
}else{
return 0;
}
}
int judge_null(quene *q){
if(q->top == q->tail){
return 1;
}else{
return 0;
}
}
void insert_quene(quene *q){
if(judge_full(q)){
printf("该队已满\n");
}else{
printf("请输入你想插入的元素\n");
scanf("%d",&q->data[q->top+1]);
q->top++;
printf("插入成功\n");
}
}
void delete_quene(quene *q){
if(judge_null(q)){
printf("该队为空\n");
}else{
q->tail++;
printf("删除成功\n");
}
}
void get_top(quene *q){
if(judge_null(q)){
printf("该队为空\n");
}else{
printf("该队列首元素为:");
printf("%d\n",q->data[q->tail]);
}
}
void print_quene(quene *q){
int i;
if(judge_null(q)){
printf("该队为空\n");
}else{
printf("该队列元素为:");
for(i=q->tail;i<=q->top;i++){
printf("%d ",q->data[i]);
}
}
printf("\n");
}
void mune(){
printf("1.代表初始化队\n");
printf("2.代表创建队\n");
printf("3.代表往队内插入元素\n");
printf("4.代表删除队内元素\n");
printf("5.代表取队顶元素\n");
printf("6.打印队内元素\n");
printf("7.退出系统\n");
}
int main(){
quene q;
int select,flag=1;
while(flag){
mune();
printf("请输入你想进行的操作\n");
scanf("%d",&select);
switch(select){
case 1:
init_quene(&q);
break;
case 2:
create_quene(&q);
break;
case 3:
insert_quene(&q);
break;
case 4:
delete_quene(&q);
break;
case 5:
get_top(&q);
break;
case 6:
print_quene(&q);
break;
case 7:
flag = 0;
break;
}
}
return 0;
}
顺序队(数组实现)
于 2022-10-21 13:47:07 首次发布