循环队列实现(链表,数组)
#include <stdlib.h>#include <memory.h>#include "queue.h"#ifdef __cplusplusextern "C"{#endif#ifdef QUEUE_LINKtypedef struct queue{int num;struct queue *next;}QUEUE;QUEUE *g_queue = NULL;QUEUE *g_lastOne = NULL;int enqueue(int i){QUEUE *tempAdd = (QUEUE*)malloc(sizeof(QUEUE));if(tempAdd == NULL){return -1;}memset(tempAdd,0,sizeof(QUEUE));tempAdd->num = i;if(g_queue == NULL){g_queue = tempAdd;g_lastOne = g_queue;}else{g_lastOne->next = tempAdd;g_lastOne = g_lastOne->next;}return 0;}int dequeue(int *i){QUEUE *tempde = NULL;if(i == NULL){return -1;}if(g_queue == NULL){return -1;}*i = g_queue->num;tempde = g_queue;g_queue = g_queue->next;free(tempde);return 0;}int queue_empty(){return (g_queue == NULL)?1:0;}#endif#ifdef QUEUE_ARRAY/* 使用哨兵 *//* head指向应该插入的位置,若队列满则指向哨兵 *//* end指向队列尾,若队列空则指向head*/int g_queue[MAX_QUEUE + 1] = {0};unsigned int g_queueHead = 1;unsigned int g_guard = 0;unsigned int g_queueEnd = 1;int enqueue(int i){if(queue_full()){return -1;}g_queue[g_queueHead++] = i;if(g_queueHead == MAX_QUEUE + 1){g_queueHead = 0;}if(g_queueEnd == g_guard){++g_queueEnd;}return 0;}int dequeue(int *i){if(i == NULL){return -1;}if(queue_empty()){return -1;}*i = g_queue[g_queueEnd++];if(g_queueEnd == MAX_QUEUE + 1){g_queueEnd = 0;}++g_guard;if(g_guard == MAX_QUEUE + 1){g_guard = 0;}return 0;}int queue_full(){return (g_queueHead == g_guard);}int queue_empty(){return (g_queueEnd == g_queueHead);}#endif#ifdef __cplusplus}#endif?