# C语言链队列的实现

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int ElemType;

//链队列的结点定义
typedef struct node{
ElemType val;
struct node* next;
}QueueNode;

//链队列的定义，包含队头指针和队尾指针
typedef struct queue {
QueueNode* front;
QueueNode* rear;

//初始化队列
queue->front = (QueueNode*)malloc(sizeof(QueueNode));
queue->front->next = NULL;
queue->rear = queue->front;
}

//元素入队
void enQueue(LinkedQueue* queue, ElemType elem) {
QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
node->val = elem;
node->next = NULL;
queue->rear->next = node;
queue->rear = node;
}

//队列是否为空
return queue->front == queue->rear;
}

//元素出队
if(!isQueueEmpty(queue)) {
QueueNode* p = queue->front;
queue->front = p->next;
ElemType e = queue->front->val;
free(p);
return e;
}
return NULL;
}

int main()
{
int i;
for(i = 0; i < 20; i++) {
enQueue(queue, i);
}
while(!isQueueEmpty(queue)) {
printf("deQueue: %d\n", deQueue(queue));
}
return 0;
}

1. 初始化队列时，队头和队尾都指向同一个结点（头结点，不存储数据）；
2. 判断队列是否为空，即判断队头指针和队尾指针是否相同；
3. 队头元素是当前front指针的next结点中的值