//@author:Archer
//@email:583460553@qq.com || jame_peng@sina.com
//模仿+练习+改进
//改进:
//1、增加队列实验的可操作性改进
//2、增强代码的健壮性
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
typedef struct queue
{
int * pBase;
int front;//队头
int rear;//队尾
int length;//队列最大长度
int real_length;//队列实际长度
}QUEUE,*PQUEUE;
void init_queue(PQUEUE);//初始化队列
bool entry_queue(PQUEUE);//入队操作
void traverse_queue(PQUEUE);//查看队伍排列状况
bool out_queue(PQUEUE);//出队操作
bool full_queue(PQUEUE);//检查队列是否为满
bool empty_queue(PQUEUE);//检查队列是否为空
void output_reallength(PQUEUE);//输出队列中实际的元素个数
int main(void)
{
QUEUE queue;//定义一个queue的队列
PQUEUE pqueue = &queue;
pqueue->real_length = 0;//初始化实际长度值
int choose = 0;
int i = 0;
while(choose!=6)
{
printf("\n菜单操作:\n\n");
printf("1.初始化队列\n2.入队操作\n3.出队操作\n4.输出队列中的元素\n5.查看队列中实际元素个数\n6.退出\n\n");
if(i==0)
{
printf("第一次操作请先初始化队列!\n");
}
printf("请输入操作序号:");
scanf("%d",&choose);
if(choose ==1)
{
init_queue(pqueue);
}
else if(choose ==2)
{
entry_queue(pqueue);
}
else if(choose ==3)
{
out_queue(pqueue);
}
else if(choose ==4)
{
traverse_queue(pqueue);
system("pause");
}
else if(choose ==5)
{
output_reallength(pqueue);
system("pause");
}
else//防止用户误操作,增强代码健壮性
{
system("cls");
continue;
}
i++;
system("cls");
}
return 0;
}
void init_queue(PQUEUE pqueue)//初始化队列
{
printf("请输入要生成的队列长度:");
scanf("%d",&pqueue->length);
pqueue->pBase = (int *)malloc(sizeof(int) * (pqueue->length));
pqueue->front = pqueue->rear = 0;
return;
}
bool entry_queue(PQUEUE pqueue)//入队操作
{
if(full_queue(pqueue))
{
printf("\n入队操作失败!\n");
return false;
}
else
{
int count = 0;
int len = 0;
int value;
printf("请输入要入队的个数(大于零小于%d):",len);
scanf("%d",&len);
while(count<len)
{
printf("请输入第%d个入队元素:",count+1);
scanf("%d",&value);
pqueue->pBase[pqueue->rear] = value;
pqueue->rear = ((pqueue->rear) +1) % pqueue->length;
pqueue->real_length++;
count++;
}
return true;
}
}
void traverse_queue(PQUEUE pqueue)//查看队列排列情况
{
int i =0;
int count = pqueue->front;
while(count!=pqueue->rear)
{
i++;
printf("队列第%d个元素为:%d\n",i,pqueue->pBase[count]);
count = (count+1) % pqueue->length;
}
printf("\n\n");
return;
}
bool out_queue(PQUEUE pqueue)//出队操作
{
int i;
int count = 0;
int len =pqueue->real_length;
int temp;
if(empty_queue(pqueue))
{
printf("\n出队操作失败!\n");
return false;
}
else
{
printf("\n\n请输入要出队的个数:");
scanf("%d",&count);
if(count<0)
{
printf("出队操作失败");
return false;
}
if(count>len)
{
count = len;
}
for(i=0;i<count;i++)
{
printf("\n要出队的元素为:%d\n",pqueue->pBase[pqueue->front]);
temp = pqueue->pBase[pqueue->front];
pqueue->front = ((pqueue->front) +1) % pqueue->length;
printf("【%d】元素已出队!\n",temp);
pqueue->real_length--;
}
return true;
}
}
bool full_queue(PQUEUE pqueue)//检查队列是否为满
{
if((pqueue->rear+1)%(pqueue->length)==pqueue->front)//或者pqueue->real_length == pqueue->length(但是容易忘了去计算real_queue,也就容易出错)
{
printf("\n警告,队列已满!\n");
return true;
}
else
{
return false;
}
}
bool empty_queue(PQUEUE pqueue)//检查队列是否为空
{
if(pqueue->front==pqueue->rear)
{
return true;
}
else
{
return false;
}
}
void output_reallength(PQUEUE pqueue)
{
printf("\n队列中实际有%d个元素\n\n",pqueue->real_length);
}