链队列与链表相似,不同的是它具有对列的运算方法,存储结构和链表相同,下面是其结构示意图
对于队列相关操作的介绍和认识
1.新建队列
实现队列的新建,只要把队列的队头指针和队尾指针都指向空
2.判断队列是否为空
判断队头指针和队尾指针是否都为空,如果都为空,说明队列为空
3.实现取得队首元素
前提条件:队列不为空,然后实现取得队头所在指针的数据域
4.展示队列
遍历队列,将一个所创建的结点放在队首,遍历取数
5.插入数字
6.前提条件:判断队列是否为空,判断队列如果只有一个元素的情况,将某个结点放在队尾处
(尾插法:将尾结点的后继给予,然后移动尾指针)
出队
前提条件:判断队列是否为空·,判断队列只有一个元素的情况,然后将队首指针后移
#include#include #define Datatype int typedef struct node{ Datatype data; struct node *next; }QueueNode; typedef struct{ QueueNode *front; QueueNode *rear; }LinkQueue; void InitQueue(LinkQueue *Q) { Q->front = Q->rear = NULL; } int QueueEmpty(LinkQueue *Q) { return(Q->front == NULL && Q->rear == NULL); } void EnLinkQueue(LinkQueue *Q, Datatype v) { QueueNode *p; p = (QueueNode *)malloc(sizeof(QueueNode)); p->data = v; p->next = NULL; if(QueueEmpty(Q)) Q->front = Q->rear = p; else { Q->rear->next = p; Q->rear = p; } } Datatype DeLinkQueue(LinkQueue *Q) { Datatype i; QueueNode *s; if(QueueEmpty(Q)) printf("Error,the linkqueue is empty!"); s = Q->front; i = s->data; if(Q->front == Q->rear) Q->front = Q->rear = NULL; else Q->front = s->next; free(s); return i; } Datatype ReadLinkQueue(LinkQueue *Q) { Datatype i; if(QueueEmpty(Q)) printf("Error,the linkqueue is empty!"); i = Q->front->data; return i; } int main() { LinkQueue Q; Datatype i = 1; InitQueue(&Q); while(i<=6) { EnLinkQueue(&Q,i); i++; } printf("DeLinkQueue: %dn", DeLinkQueue(&Q)); printf("DeLinkQueue: %dn", DeLinkQueue(&Q)); printf("ReadLinkQueue: %dn", ReadLinkQueue(&Q)); printf("ReadLinkQueue: %dn", ReadLinkQueue(&Q)); EnLinkQueue(&Q,9); printf("The all number of the linkqueue:n"); while(!QueueEmpty(&Q)) printf("%dt",DeLinkQueue(&Q)); return 0; }
运行结果如下:
循环队列
循环队列是队列的顺序存储结构,用判断是否为空利用 Q.front=Q.rear
循环队列头指针始终指向队列头元素,而尾指针始终指向队列尾元素的下一个位置
按照队列的定义,队头删除,队尾插入,在这里插入图片描述会导致队头之前可能有空余的内存空间,为了解决该问题,提出循环队列的解决方案
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#define maxQueueSize 5
typedef int QElemType;
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
bool InitQueue(SqQueue *Q)
{
Q->base=(QElemType*)malloc(maxQueueSize*sizeof(QElemType));
if (!Q->base)
{
return false;
}
Q->front=0;
Q->rear=0;
printf("循环队列初始化完成!n");
return true;
}
bool EnQueue(SqQueue *Q,QElemType e)
{
if ((Q->rear+1)%maxQueueSize==Q->front)//队列满
{
return false;
}
Q->base[Q->rear]=e;
Q->rear=(Q->rear+1)%maxQueueSize;
printf("%d 入队完成!n",e);
return true;
}
bool DeQueue(SqQueue *Q,QElemType *e)
{
if (Q->front==Q->rear)//队列空
{
return false;
}
*e=Q->base[Q->front];
Q->front=(Q->front+1)%maxQueueSize;
printf("%d 出队完成!n",*e);
return true;
}
void printfQueue(SqQueue Q)
{
printf("打印循环队列如下n");
while (Q.front!=Q.rear)
{
if (Q.front==maxQueueSize &&(Q.rear+1)%maxQueueSize!=Q.front)
{
Q.front=0;
}
printf("%d ",Q.base[Q.front]);
Q.front++;
}
printf("n");
}
int _tmain(int argc, _TCHAR* argv[])
{
SqQueue *Q=(SqQueue*)malloc(sizeof(SqQueue));
InitQueue(Q);
EnQueue(Q,6);
EnQueue(Q,8);
EnQueue(Q,10);
EnQueue(Q,12);
printfQueue(*Q);
QElemType *e=(QElemType*)malloc(sizeof(QElemType));
DeQueue(Q,e);
DeQueue(Q,e);
EnQueue(Q,14);
EnQueue(Q,16);
printfQueue(*Q);
system("pause");
return 0;
}
运行结果:



