typedef struct _queue
{
int head;
int tail;
int len;
int capacity;
int * arr;
}QUEUE, *PQUEUE;
int Queue_Create(QUEUE ** que, int _len)
{
if (_len < 0 || _len > 10000)
{
printf("len is err !\n");
return -1;
}
PQUEUE temp = (PQUEUE)malloc(sizeof(QUEUE));
if (NULL == temp)
{
printf("malloc err !\n");
return -2;
}
memset(temp, 0, sizeof(QUEUE));
temp->arr = (int *)malloc(sizeof(int)* _len);
if (NULL == temp->arr)
{
printf("malloc err !\n");
return -3;
}
memset(temp->arr, 0, sizeof(int)* _len);
temp->head = 0;
temp->tail = 0;
temp->len = 0;
temp->capacity = _len;
*que = temp;
return 0;
}
int Queue_En(PQUEUE que, int num)
{
if (NULL == que)
{
printf("que is NULL\n");
return -1;
}
que->arr[que->tail] = num;
que->len++;
que->tail = (que->tail + 1) % que->capacity;
return 0;
}
int Queue_IsFull(PQUEUE que)
{
if (NULL == que)
{
printf("que is NULL\n");
return 0;
}
if ((que->tail + 1) % que->capacity == que->head)
{
return 1;
}
return 0;
}
int Queue_Empth(PQUEUE que)
{
if (NULL == que)
{
printf("que is NULL\n");
return 0;
}
if (0 == que->len)
{
return 1;
}
return 0;
}
int Queue_Out(PQUEUE que, int * num)
{
if (Queue_Empth(que))
{
return -1;
}
*num = que->arr[que->head];
que->len--;
que->head = (que->head + 1) % que->capacity;
return 0;
}
int Queue_Free(PQUEUE * que)
{
if (NULL == que || NULL == *que)
{
return -1;
}
PQUEUE temp = *que;
if (temp->arr != NULL)
{
free(temp->arr);
}
free(temp);
*que = NULL;
printf("free success !\n");
return 0;
}
void main()
{
PQUEUE queue = NULL;
int ret = Queue_Create(&queue, 10);
if (0 == ret)
{
printf("create success !\n");
}
int num = 0;
Queue_En(queue, 99);
Queue_En(queue, 88);
Queue_En(queue, 77);
Queue_En(queue, 9);
Queue_En(queue, 8);
Queue_En(queue, 7);
for (int i = 0; i < queue->len; i++)
{
printf("%d\n", queue->arr[i]);
}
Queue_Out(queue, &num);
printf("%d\n", num);
Queue_Free(&queue);
system("pause");
}