# 1. Concept

Contrary to a stack, a queue is a `先进先出(first in first out = FIFO)`structure. Is FIFO the FIFO of the microcontroller? It is a linear table that only allows inserting elements at one end of the table and deleting elements at the other end. In the queue, the end that can be inserted is called `队尾`, and the end that is allowed to be deleted is called `队头`.

# 2. Principle realization

## 1. Node

Because our queue is still a linear table, the nodes of our queue should still be composed of two parts: `数据区`and `指向下一个节点的指针`.

## 2. Queue

If we want to implement a queue, we only need two pointers and point to it `队头和队尾`.
It is best to use chain storage, because it `队头`will often change, if it adopts a sequential structure, it may waste space or frequently modify a large number of elements.
At the time of initialization, we can `队头队尾`point all to the same node, and the next node to which the node points is `NULL`.

## 3. Join the team

For enqueue, we only need to insert the element after the end of the team, and then point the end of the team to it.

## 4. Get out of the team

Save the address of the next node pointed to by the head of the team, record it as address A, point the head of the team to the next node of address A, and then `free`drop A.

## 5. Destroy

Destroy a queue, just start over from the team, followed by all the nodes `free`off

# Three. Code

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

#define OK      1
#define ERROR   0

typedef struct dataType{

int data;
}dataType;

typedef struct nodeType{

dataType data;
struct nodeType *next;
}nodeType;

typedef struct queue{

nodeType *tail;
}queue;

queue* QueueInit(void);
int QueuePush(queue* q, dataType data);
int QueueGet(queue* q, dataType *datain);
int QueuePop(queue* q);
int QueueDistroy(queue *q);

int main()
{

// 主函数内容随便改
queue *Q = QueueInit();
dataType test;
int i = 0;
for (i = 0; i < 5; ++i)
{

test.data = i;
QueuePush(Q, test);
}
{

QueueGet(Q, &test);
printf("%d\n", test.data);
QueuePop(Q);
}
for (i = 5; i >= 0; --i)
{

test.data = i;
QueuePush(Q, test);
}
{

QueueGet(Q, &test);
printf("%d\n", test.data);
QueuePop(Q);
}
return 0;
}

queue* QueueInit(void)
{

queue* q = (queue*)malloc(sizeof(queue));
// 动态生成一个队列和它里面的一个节点

if (q != NULL && qhead != NULL)
{

return q;
}
exit(0);
}

int QueuePush(queue* q, dataType datain)
{

nodeType *node = (nodeType*)malloc(sizeof(nodeType));

if (node != NULL)
{

node->data = datain;    // 更新节点数据
node->next = NULL;
q->tail->next = node;   // 更新队列
q->tail = node;         // 跟新队尾指针
return OK;
}
return ERROR;
}

int QueueGet(queue* q, dataType *datain)
{

{

return OK;
}
return ERROR;
}

int QueuePop(queue *q)
{

nodeType *dnode = NULL;

{

free(dnode);    // 释放空间
return OK;
}
return ERROR;
}

int QueueDistroy(queue* q)
{

nodeType *dnode = NULL;
{