顺序表创建队列

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
typedef struct {
	int*data;
	int front;//头指针 
	int rear;//尾指针 
}Squeue;
//初始化队列
void InitSqueue(Squeue* L){
	L->data=(int*)malloc(MAXSIZE*sizeof(int));
	L->front=L->rear=0;
} 
/*销毁队列*/
void Destroy(Squeue* L){
	free(L->data);
	L->front=L->rear=0; 
}
/*清空队列*/
void clear(Squeue* L){
  L->front=L->rear=0;
}
/*在队尾插入一个数据*/
void Jion(Squeue* L){
	int x;
	//求余数的方法判断队列中的位置,满了就循环 
	if(L->front==(L->rear+1)%MAXSIZE){//判断队列是否满,这里我们空出一个我位置来标识队列是否满 
		printf("队列已满\n");
	}
	else {
		printf("请输入插入的数据\n");
		scanf("%d",&x);
		L->data[L->rear]=x; 
		L->rear=(L->rear+1)%MAXSIZE;
	}	
}
/*出队输出队头数据*/
void OutLine(Squeue* L){
	if(L->front==L->rear){
	 printf("队列为空\n");	
	}	
   else{
   	   printf("输出队头为:");
       printf("[%d]",L->data[L->front]);
       L->front=(L->front+1)%MAXSIZE; 
   }
   printf("\n");	
}
/*查看队头数据*/
void Showfront(Squeue* L){
	if(L->front==L->rear){
	  printf("队列为空\n");	
	}
	else{	
	  printf("队头为:[%d]\n",L->data[L->front]);	
	}
}
/*查看队列中的数据*/
void printfList(Squeue* L){
	int i;
	i=L->front;
	if(L->front==L->rear){
	printf("队列为空\n");	
	}
	else{
	printf("队列中的数据为:");
  while(i!=L->rear){
  	printf("[%d]",L->data[i]);
  	i=(i+1)%MAXSIZE;
  }
  printf("\n");
}
}
/*一次性入队多个数据*/
void manyInsert(Squeue* L){
	 int i,x;
	if(L->front==(L->rear+1)%MAXSIZE){//判断队列是否满,这里我们空出一个我位置来标识队列是否满 
		printf("队列已满\n");
	}
	else{
	  printf("请输入要入队的数据个数,再输入相应个数的数据\n");
	  scanf("%d",&i);
	  if(i>MAXSIZE-1){
	  	printf("输入的数据范围超出存储范围\n");
	  }
	  else{
	  while(L->front!=(L->rear+1)%MAXSIZE && i){
	  	   scanf("%d",&x);
	  	   L->data[L->rear]=x;
	  	   L->rear=(L->rear+1)%MAXSIZE;
	  	 	if(L->front==(L->rear+1)%MAXSIZE){//判断队列是否满,这里我们空出一个我位置来标识队列是否满 
		    printf("队列已满\n"); 
           	}
		i--;   
	  }		
	}
}
} 
/*一次性出队多个数据*/
void manyprintf(Squeue* L){
   int i;
   if(L->front==L->rear){
	 printf("队列为空\n");	
	}
	else{
	  printf("请输入要出队个数\n");	
	  scanf("%d",&i);
	  printf("出队的数据为:");	
	 while(L->front!=L->rear&& i){
	 	printf("[%d]",L->data[L->front]);
	 	L->front++;
	 	i--;
	 }
	 printf("\n");	
	}
}
/*队列中的数据个数*/
void Number(Squeue* L){
	int i;
	if(L->front==L->rear){
	printf("队列为空\n");
	}
	else{
	  i=(L->rear-L->front+MAXSIZE)%MAXSIZE;
	  printf("队列中的数据个数为:%d\n",i);	
	}
} 
int main(int argc, char *argv[]) {
	int x;
	Squeue L;
	printf("当前队列的存储容量大小为:%d\n",MAXSIZE);
	InitSqueue(&L);
	manyInsert(&L);
	Showfront(&L);
	Number(&L);
	printfList(&L);
	Jion(&L);
	Number(&L);
	printfList(&L);
	manyInsert(&L);
	Number(&L);
	printfList(&L);
	Number(&L);
	manyprintf(&L);
	printfList(&L);
	Showfront(&L);
	Number(&L);
	Destroy(&L);
	return 0;
}

#效果展示
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr pan cheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值