队列 (Queue) :也是运算受限的线性表。是一种先进先出 (First In First Out ,简称 FIFO) 的线性表。只允许在表的一端进行插入,而在另一端进行删除。
队首 (front) :允许进行删除的一端称为队首。
队尾 (rear) :允许进行插入的一端称为队尾。
代码:
#include <stdio.h>
#include <stdlib.h>
//定义节点
typedef struct node{
char data;
struct node * next;
}node;
//定义队列(保存队首和队尾指针)
typedef struct queue_link{
node * front;
node * rear;
}que;
//初始化队列
que * InitQueue()
{
que * q = (que*)malloc(sizeof(que));
q->front = q->rear = NULL;
return q;
}
//判断队列是否为空
int EmptyQueue(que * q)
{
return q->front == NULL;
}
//入队
void InsertQueue(que *q, char data)
{
node * n = (node *)malloc(sizeof(node));
if(n == NULL)//内存分配失败
return ;
n->data = data;
n->next = NULL;//尾插法,插入元素指向空
if(q->rear == NULL)
{
q->front = n;
q->rear = n;
}
else{
q->rear->next = n;//让n成为当前的尾部