循环队列操作

来源:互联网 发布:美空网怎么约 知乎 编辑:程序博客网 时间:2024/05/22 06:44
/* 循环队列操作 */#define MAX_QUEUE_SIZE 100#define TRUE 1#define FALSE 0#define IS_QUEUE_EMPTY(front, rear) (front == rear ? TRUE: FALSE)#define IS_QUEUE_FULL(rear) (rear == MAX_QUEUE_SIZE - 1 ? TRUE: FALSE)typedef struct info{int key;/* other fields */}element;element queue[MAX_QUEUE_SIZE];void add_cycle_queue(int front, int *rear, element item){*rear = (*rear + 1) % MAX_QUEUE_SIZE;if(IS_QUEUE_FULL(front, *rear)){queue_full(*rear);return ;}queue[*rear] = item;}void queue_full(int *rear){printf("Full.\n");/* other operations */*rear = front;return ;}element delete_cycle_queue(int *front, int rear){element item;if(IS_QUEUE_EMPTY(*front, rear))return queue_empty();*front = (*front + 1) % MAX_QUEUE_SIZE;return queue[*front];}element queue_empty(void){printf("Empty.\n");/* other operations */return queue[rear];}