2. 队列
2.1. 队列简介
- 队列也分成两种:
静态队列(数组实现)
动态队列(链表实现) - 队列常用三个判断因式
- 队满判断因式:(rear + 1) % QueueSize == front
- 队空的判断因式:rear == front
- 通用队长计算因式:(QueueSize - front + rear) % QueueSize
2.2. 静态队列
typedef struct
{
data_t data[MAXSIZE];
int front;//队头位置
int rear;//队尾位置
}SqQueue;
front:指向队头的位置
rear:指向队尾元素的下一个位置
当front = rear是队列为空
2.2.1. 创建队列
SqQueue *CreateEmptyQueue()//创建队列
{
SqQueue *sq = (SqQueue*)malloc(sizeof(SqQueue));
if(sq==NULL)
{
printf("CreateEmptyQueue Error\n");
return NULL;
}
sq->front=0;
sq->rear=0;
return sq;
}
2.2.2. 队列插入
假设队列长MAXSIZE = 10,即f ront=0, rear=9,
int EmptyQueue(SqQueue *Q)//判断队是否为空
{
if(Q==NULL)
{
printf("EmptyQueue Error\n");
return -1;
}
if(Q->rear==Q->front)
return OK;
else
return ERROR;
}
int FullQueue(SqQueue *Q)//判断队是否已满
{
if(Q==NULL)
{
printf("EmptyQueue Error\n");
return -1;
}
if((Q->rear+1)%MAXSIZE==Q->front)
return OK;
else
return ERROR;
}
int EnQueue(SqQueue *Q,data_t e)//元素e入队
{
if(FullQueue(Q)==OK)
{
printf("Queue is Full\n");
return ERROR;
}
Q->data[Q->rear]=e;
Q->rear=(Q->rear+1)%MAXSIZE;
return OK;
}
2.2.3 求队列长度
int QueueLength(SqQueue *Q)//求当前队列长度
{
return (Q->rear - Q->front + MAXSIZE)%MAXSIZE;
}
int DeQueue(SqQueue *Q,data_t *e)//元素出队,出队元素存储在e中
{
if(EmptyQueue(Q)==OK)
{
printf("Queue is Empty\n");
return ERROR;
}
*e=Q->data[Q->front];
Q->front=(Q->front+1)%MAXSIZE;
return OK;
}
2.3 链式队列
节点结构
typedef struct node_t
{
data_t data;
struct node_t *next;
} linknode_t, *linklist_t;
队列的链表结构
typedef struct
{
linklist_t front, rear;
} linkqueue_t;
2.3.1. 创建队列
linkqueue_t *CreateEmptyLinkqueue()//创建空队列
{
linkqueue_t *queue;
queue = (linkqueue_t *)malloc(sizeof(linkqueue_t));
if (NULL == queue)
{
perror("Create Empty LinkQueue Error");
return NULL;
}
queue->rear = queue->front = NULL;
return queue;
}
2.3.1. 插入队列
int EnQueue(linkqueue_t *queue, data_t x)//入队
{
linknode_t *node_new;
if (!queue)
{
printf("EnQueue Error\n");
return ERROR;
}
node_new = (linknode_t *)malloc(sizeof(linknode_t));
node_new->data = x;
node_new->next = NULL;
if(EmptyLinkqueue(queue)==OK)
{
queue->front = queue->rear = node_new;
} else
{
queue->rear->next = node_new;
queue->rear = node_new;
}
return OK;
}
2.3.2 出队
int DeQueue(linkqueue_t *queue, data_t *x)//出队
{
linknode_t *node_remove;
if(!queue)
{
printf("DeQueue Error\n");
return ERROR;
}
if(EmptyLinkqueue(queue)==OK)
{
printf("queue is Empty\n");
return ERROR;
}
node_remove = queue->front;
queue->front = node_remove->next;
if (NULL == queue->front)
queue->rear = NULL;
if(x)
{
*x = node_remove->data;
}
free(node_remove);
return OK;
}
2.2.3 其他
int DestroyLinkqueue(linkqueue_t *queue)//销毁队列
{
if (queue)
{
ClearLinkqueue(queue);
free(queue);
return OK;
}
else
{
printf("DestroyLinkqueue Error\n");
return ERROR;
}
}
int EmptyLinkqueue(linkqueue_t *queue)//判定队列是否为空
{
if (!queue)
{
printf("EmptyLinkqueue Error\n");
return -1;
}
return queue->front == NULL ? OK : ERROR;
}
int ClearLinkqueue(linkqueue_t *queue)//清空队列
{
linknode_t *node_remove;
node_remove = queue->front;
while (NULL != node_remove)
{
queue->front = queue->front->next;
free (node_remove);
node_remove = queue->front;
}
queue->rear = NULL;
return OK;
}
int VisitQueue(linkqueue_t *queue)//遍历队列
{
linknode_t *node;
printf("aueue = {");
node = queue->front;
if (NULL == node) {
printf("}\n");
return OK;
}
while (NULL != node) {
printf("%d,", node->data);
node = node->next;
}
printf("\b}\n");
return OK;
}