include <stdio.h>
include <stdlib.h>
define MAX 10
define OK 0
define ERROR -1
typedef struct queue{
int Queue[MAX];
int front;
int rear;
}Queue;
Queue Init_Queue(Queue *Q){
Q->front = Q->rear = 0;
return *Q;
}
void Print(Queue Q){
while( Q.rear != Q.front){
printf("%d ", Q.Queue[Q.front]);
Q.front++;
}
printf("\n");
}
void CreadQueue(Queue Q, int n){
printf("请输入%d个元素:", n);
int i = 0;
while(i < n){
scanf("%d", &Q->Queue[i]);
Q->rear++;
i++;
}
Print(Q);
}
int QueueLength(Queue Q){
int i = 0;
while(Q.front != Q.rear){
Q.front++;
i++;
}
printf("length: %d\n", i);
return i;
}
int InsertQueue(Queue Q, int num){
if((Q->rear+1)%MAX == Q->front)
return ERROR;
Q->Queue[Q->rear] = num;
Q->rear = (Q->rear+1)%MAX;
Print(Q);
return OK;
}
int DeleQueue(Queue Q){
int num = 0;
if(Q->front == Q->rear)
return ERROR;
num = Q->Queue[Q->front];
Q->front = (Q->front+1)%MAX;
printf("首元素:%d\n", num);
printf("剩余元素:");
Print(Q);
return OK;
}
int Show(){
int n;
printf("******************\n");
printf("1、获取队列长度。\n");
printf("2、插入元素。\n");
printf("3、删除首元素。\n");
printf("4、退出。\n");
printf("-------------------\n");
printf("请选择要操作的序号:");
scanf("%d", &n);
return n;
}
int main()
{
Queue Q;
int num;
Init_Queue(&Q);
CreadQueue(&Q, 5);
int n;
n = Show();
while(n != 4){
switch(n){
case 1:
QueueLength(Q);
n = Show();
break;
case 2:
printf("输入要插入的数字:");
scanf("%d", &num);
InsertQueue(&Q, num);
n = Show();
break;
case 3:
DeleQueue(&Q);
n = Show();
break;
case 4:
break;
}
}
return 0;
}