源码+注释
#include <stdlib.h>
#define MAXSIZE 1000
typedef enum {
FALSE,
TRUE
} boolean;
typedef int ElemType;
typedef struct {
ElemType *base;
int front;
int rear;
} SqQueue;
boolean InitQueue(SqQueue *queue);
boolean IsFull(SqQueue *queue);
boolean EnQueue(SqQueue *queue, ElemType elem);
boolean IsEmpty(SqQueue *queue);
boolean DeQueue(SqQueue *queue, ElemType *elem);
boolean QueueLength(SqQueue *queue);
ElemType GetHead(SqQueue *queue);
int main() {
return 0;
}
boolean InitQueue(SqQueue *queue) {
queue->base = (ElemType *) malloc(sizeof(ElemType) * MAXSIZE);
if (!(queue->base)) {
return FALSE;
}
queue->front = queue->rear = 0;
return TRUE;
}
boolean IsFull(SqQueue *queue) {
return (queue->rear + 1) % MAXSIZE == queue->front;
}
boolean EnQueue(SqQueue *queue, ElemType elem) {
if (IsFull(queue)) {
return FALSE;
}
queue->base[queue->rear] = elem;
queue->rear = (queue->rear + 1) % MAXSIZE;
return TRUE;
}
boolean IsEmpty(SqQueue *queue) {
return queue->front == queue->rear;
}
boolean DeQueue(SqQueue *queue, ElemType *elem) {
if (IsEmpty(queue)) {
return FALSE;
}
*elem = queue->base[queue->front];
queue->front = (queue->front + 1) % MAXSIZE;
return TRUE;
}
boolean QueueLength(SqQueue *queue) {
return (queue->rear - queue->front + MAXSIZE) % MAXSIZE;
}
ElemType GetHead(SqQueue *queue) {
if (!(IsEmpty(queue))) {
return queue->base[queue->front];
}
}