目录
一.队列的原理示意图
二.队列的代码实现
1.列表的创建
typedef int QElemType;
struct node
{
QElemType data;
struct node *next;
};
typedef struct LinkedQueue
{
struct node *front;//指向队列的第一个结点
struct node *rear;//指向队列的最后一个结点
int nodeNum;
}LinkedQueue;
2.基本方法的实现
#include <stdio.h>
#include <stdlib.h>
#include "LinkedQueue.h"
/*初始化一个队列*/
LinkedQueue *InitQueue()
{
LinkedQueue *lq = malloc(sizeof(*lq));
lq->front = NULL;
lq->rear = NULL;
lq->nodeNum = 0;
return lq;
}
/*判断一个队列是否为空。如果为空返回1,否则返回0*/
int QueueIsEmpty(LinkedQueue *lq)
{
return lq->nodeNum==0;
}
/*返回一个队列的长度,即结点数目*/
int QueueLen(LinkedQueue *lq)
{
return lq->nodeNum;
}
/*获取队列的第一个结点的指针,并不是出队*/
struct node *GetHead(LinkedQueue *lq)
{
return lq->front;
}
/*出队,返回的是出队结点的指针,只是把链条切断,还没有释放*/
struct node *DeQueue(LinkedQueue *lq)
{
struct node *p = lq->front;
lq->front = lq->front->next;
p->next = NULL;
lq->nodeNum--;
if(lq->nodeNum == 0)//注意,只有一个结点时,出队后,front和rear都要为NULL
{
lq->rear = NULL;
}
return p;
}
/*入队*/
void EnQueue(LinkedQueue *lq, struct node *pnew)
{
if(lq->front == NULL)
{
lq->front = pnew;
lq->rear = pnew;
}
else
{
lq->rear->next = pnew;
lq->rear = pnew;
}
lq->nodeNum++;
}
/*销毁一个队列*/
void DestroyQueue(LinkedQueue *lq)
{
struct node *p = lq->front;
while(p)
{
lq->front = lq->front->next;
p->next = NULL;
free(p);
p = lq->front;
}
free(lq);
}
/*清空一个队列*/
void ClearQueue(LinkedQueue *lq)
{
struct node *p = lq->front;
while(p)
{
lq->front = lq->front->next;
p->next = NULL;
free(p);
p = lq->front;
}
lq->nodeNum = 0;
}
三,循环队列
循环列表示意图
四,循环队列及代码实现
循环队列:
循环队列本质上还是用一个数组来实现的,但是需要用front和rear两个变量来保存
队列的队首元素和队尾元素的下标。
#define MAX_LEN 1024
typedef int QElemType;
typedef struct CircleQueue
{
QElemType Elems[MAX_LEN];
int front;//队首元素的下标
int rear;//队尾元素的下标
int ElemNum;//元素的数量
}CircleQueue;
算法分析:
1)在入队和出队时,队首和队尾往后走到数组末尾之后,要自动跳转到开头。
代码为:
front = (front+1)%MAX_LEN;
rear = (rear+1)%MAX_LEN;
2)空队列和满队列的判断条件
空队: ElemNum==0
满队: ElemNum==MAX_LEN
3)没有元素时,若入队,则front和rear都要同时往后走
只有一个元素时,若出队,则front和rear都要同时往后走
1.创建循环列表
#define MAX_LEN 5
typedef int QElemType;
typedef struct CircleQueue
{
QElemType Elems[MAX_LEN];
int front;//队首元素的下标
int rear;//队尾元素的下标
int ElemNum;//元素的数量
}CircleQueue;
2.基本方法实现
#include <stdio.h>
#include <stdlib.h>
#include "CircleQueue.h"
CircleQueue *InitQueue()
{
CircleQueue *cq = malloc(sizeof(*cq));
cq->ElemNum = 0;
cq->front = -1;
cq->rear = -1;
return cq;
}
int QueueIsEmpty(CircleQueue *cq)
{
return cq->ElemNum == 0;
}
int QueueIsFull(CircleQueue *cq)
{
return cq->ElemNum == MAX_LEN;
}
int QueueLen(CircleQueue *cq)
{
return cq->ElemNum;
}
QElemType GetHead(CircleQueue *cq)
{
return cq->Elems[cq->front];
}
/*出队*/
QElemType DeQueue(CircleQueue *cq)
{
QElemType tmp = cq->Elems[cq->front];
cq->ElemNum--;
if(cq->ElemNum == 0)
{
cq->front = (cq->front+1)%MAX_LEN;
cq->rear = (cq->rear+1)%MAX_LEN;
}
else
{
cq->front = (cq->front+1)%MAX_LEN;
}
return tmp;
}
void EnQueue(CircleQueue *cq, QElemType data)
{
cq->ElemNum++;
if(cq->ElemNum == 1)
{
cq->front = (cq->front+1)%MAX_LEN;
cq->rear = (cq->rear+1)%MAX_LEN;
}
else
{
cq->rear = (cq->rear+1)%MAX_LEN;
}
cq->Elems[cq->rear] = data;
}
void DestroyQueue(CircleQueue *cq)
{
free(cq);
}
void ClearQueue(CircleQueue *cq)
{
cq->front = -1;
cq->rear = -1;
cq->ElemNum = 0;
}
五.优先队列的实现
#include <stdio.h>
#include <stdlib.h>
#include "LinkedQueue.h"
/*初始化一个队列*/
LinkedQueue *InitQueue()
{
LinkedQueue *lq = malloc(sizeof(*lq));
lq->front = NULL;
lq->rear = NULL;
lq->nodeNum = 0;
return lq;
}
/*判断一个队列是否为空。如果为空返回1,否则返回0*/
int QueueIsEmpty(LinkedQueue *lq)
{
return lq->nodeNum==0;
}
/*返回一个队列的长度,即结点数目*/
int QueueLen(LinkedQueue *lq)
{
return lq->nodeNum;
}
/*获取队列的第一个结点的指针,并不是出队*/
struct node *GetHead(LinkedQueue *lq)
{
return lq->front;
}
/*出队,返回的是出队结点的指针,只是把链条切断,还没有释放*/
struct node *DeQueue(LinkedQueue *lq)
{
struct node *p = lq->front;
lq->front = lq->front->next;
p->next = NULL;
lq->nodeNum--;
if(lq->nodeNum == 0)//注意,只有一个结点时,出队后,front和rear都要为NULL
{
lq->rear = NULL;
}
return p;
}
/*入队,使值最小的结点放在队首*/
void EnQueue(LinkedQueue *lq, struct node *pnew)
{
if(lq->front == NULL)
{
lq->front = pnew;
lq->rear = pnew;
}
else
{
struct node *p = lq->front;
struct node *pre;
while(p)
{
if(p->data > pnew->data)
break;
pre = p;
p = p->next;
}
if(p != NULL)
{
if(p == lq->front)
{
pnew->next = lq->front;
lq->front = pnew;
}
else
{
pre->next = pnew;
pnew->next = p;
}
}
else
{
lq->rear->next = pnew;
lq->rear = pnew;
}
}
lq->nodeNum++;
}
/*销毁一个队列*/
void DestroyQueue(LinkedQueue *lq)
{
struct node *p = lq->front;
while(p)
{
lq->front = lq->front->next;
p->next = NULL;
free(p);
p = lq->front;
}
free(lq);
}
/*清空一个队列*/
void ClearQueue(LinkedQueue *lq)
{
struct node *p = lq->front;
while(p)
{
lq->front = lq->front->next;
p->next = NULL;
free(p);
p = lq->front;
}
lq->nodeNum = 0;
}