#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;
}
#效果展示