一、顺序队列
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端进行删除操作,而在表的后端进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
队列的数据元素又称为队列元素。在队列中插入一个队列元素称为入队,从队列中删除一个队列元素称为出队。因为队列只允许在一端插入,在另一端删除,所以只有最早进入队列的元素才能最先从队列中删除,故队列又称为先进先出线性表。
队列的特性是先进先出。
C语言实现:
1、队列的存储结构以及一些需要实现的方法
typedef char ElemType;
#define SIZE 10
#define TRUE 1
#define FALSE 0
typedef struct queue
{
ElemType *data;
int head;
int tail;
}queue, *pQueue;
void InitQueue(pQueue que); //队列的初始化
int PushQue(pQueue que, ElemType val); //入队列
int PopQue(pQueue que, ElemType *res); //出队列
void DestroyQue(pQueue que); //销毁队列
2、队列的判空判满以及初始化
{
assert(que != NULL);
if ((que->tail + 1) % SIZE == que->head)
{
return TRUE;
}
return FALSE;
}
static int IsEmpty(pQueue que) //判空
{
assert(que != NULL);
if (que->head == que->tail)
{
return TRUE;
}
return FALSE;
}
void InitQueue(pQueue que) //初始化
{
assert(que != NULL);
que->data = (ElemType*)malloc(sizeof(ElemType)* SIZE);
assert(que->data != NULL);