队列的链式存储结构及基本操作


#include <stdlib.h>
#include <stdio.h>

#define bool char
#define True 1
#define False 0

typedef int ElemType;

typedef struct node {
    ElemType data;
    struct node *next;
} Node;


Node *front, *rear;

void initQueue() {
    Node *p = malloc(sizeof(Node));
    p->next = NULL;
    front = rear = p;
}

bool isEmpty() {
    if (front == rear) {
        return True;
    } else {
        return False;
    }
}

void appendQueue(ElemType x) {
    Node *p;
    p = malloc(sizeof(Node));
    p->data = x;
    p->next = NULL;
    rear->next = p;
    rear = p;
}

ElemType shiftQueue() {
    if (isEmpty()) {
        printf("队列下溢错误!");
        exit(1);
    }
    Node *p = front->next;
    ElemType x = p->data;
    front->next = p->next;
    if (p->next == NULL) rear = front;
    free(p);
    return x;
}