//
// main.c
// Queue 循环队列
//
//
// Created by Jacobs.Guo on 2018/5/7.
// Copyright © 2018年 yage guo. All rights reserved.
//
#include <stdio.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 10
typedef int Status;
typedef int QElemType;
typedef struct
{
QElemType data[MAXSIZE];
int head;
int tail;
}Queue;
//初始化队列
Status InitQueue(Queue *Q)
{
Q->head = 0;
Q->tail = 0;
return OK;
}
//队列是否已经满员
Status FullQueue(Queue *Q)
{
if ( (Q->tail+1)%MAXSIZE == Q->head )
return OK;
else
return FALSE;
}
//入队列操作
Status EnQueue(Queue *Q,QElemType e)
{
if (FullQueue(Q)) return ERROR;
Q->data[Q->tail] = e;
Q->tail = (Q->tail+1)%MAXSIZE;
return OK;
}
//队列长度
int LengthQueue(Queue Q)
{
return (Q.tail-Q.head+MAXSIZE)%MAXSIZE;
}
//出队列操作
Status DeQueue(Queue *Q,QElemType *e)
{
if (Q->head == Q->tail) return ERROR;
*e = Q->data[Q->head];
Q->head = (Q->head+1)%MAXSIZE;
return OK;
}
int main() {
Queue Q;
int i;QElemType e;
printf("初始化队列Q...\n");
InitQueue(&Q);
if (FullQueue(&Q)) printf("队列Q为空...\n");
else printf("队列Q非空...\n");
printf("队列Q的长度为:%d\n",LengthQueue(Q));
printf("将1 2 3 4 5 做顺序入队列操作...\n");
for (i = 0;i<5;i++)
{
EnQueue(&Q,i+1);
}
printf("队列Q的长度为:%d\n",LengthQueue(Q));
DeQueue(&Q, &e);
printf("%d已经出队列...\n",e);
return 0;
}