数据结构:队列的基本操作

队列是一种先进先出(First in First Out)的线性表,简称FIFO。与栈不同,栈是一种后进先出(先进后出)的线性表。在队列中,允许插入的一端称为队尾,允许删除的一端称为队头。假设队列是q=(a1,a2,…,an),那么a1就是队头元素,而an是队尾元素。这样我们就可以删除时,总是从a1开始,而插入时,列在最后。这也比较符合我们通常生活中的习惯,排在第一个的优先出列,最后来的当然在队伍的最后。队列分为顺序队列和循环队列。顺序队列我们可以利用数组或者链表实现。这里,我们选择用链表实现顺序队列。

链队列

链队列的入队和出队

在这里插入图片描述

链队列的入队操作
Status EnQueue(LinkQueue *Q,QElemType e)
{
	QNode *p;
	p=(QNode *)malloc(sizeof(QNode));
	if(!p) exit(OVERFLOW);
	p->data=e;
	p->next=NULL;
	Q->rear->next=p;
	Q->rear=p;
	return OK;
}
链队列的出队操作
Status DeQueue(LinkQueue *Q,QElemType *e)
{
	QNode *p;
	if(Q->front==Q->rear)
		return ERROR;
	p=Q->front->next;
	*e=p->data;
	Q->front->next=p->next;
	if(Q->rear==p)///防止出现野指针
		Q->rear=Q->front;
	free(p);
	return OK;
}
链队列的基本操作代码(C语言版)
#include "stdio.h"
#include "stdlib.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2

typedef int QElemType;
typedef int Status;

typedef struct QNode
{
	QElemType data;
	struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;

Status InitQueue(LinkQueue *Q)
{
	
	Q->front=(QueuePtr)malloc(sizeof(QNode));///给头尾指针分配内存,让队列中的头尾指针指向同一个内存
	Q->rear=Q->front;
	if(!Q->front) exit(OVERFLOW);
	Q->front->next=NULL;
	return OK;
}
Status EnQueue(LinkQueue *Q,QElemType e)
{
	QNode *p;
	p=(QNode *)malloc(sizeof(QNode));
	if(!p) exit(OVERFLOW);
	p->data=e;
	p->next=NULL;
	Q->rear->next=p;
	Q->rear=p;
	return OK;
}

void ShowQueueElement(LinkQueue Q)
{
	QNode *p;
	p=Q.front->next;
	while(p!=NULL)
	{
		printf("%d  ",p->data);
		p=p->next;
	}
	printf("\n");
}

int QueueLength(LinkQueue Q)
{
	int count=0;
	QNode *p;
	p=Q.front->next;
	while(p!=NULL)
	{
		count++;
		p=p->next;
	}
	return count;
}

Status DeQueue(LinkQueue *Q,QElemType *e)
{
	QNode *p;
	if(Q->front==Q->rear)
		return ERROR;
	p=Q->front->next;
	*e=p->data;
	Q->front->next=p->next;
	if(Q->rear==p)
		Q->rear=Q->front;
	free(p);
	return OK;
}

Status GetHead(LinkQueue Q,QElemType *e)
{
	QNode *p;
	p=Q.front;
	if(Q.front==Q.rear)
		return ERROR;
	*e=p->next->data;
	return OK;
}

Status ClearQueue(LinkQueue *Q)
{
	QNode *p,*q;
	p=Q->front->next;
	Q->front->next=NULL;
	while(p)
	{
		q=p->next;
		free(p);
		p=q;
	}
	Q->front=Q->rear;
	return OK;
}

Status QueueEmpty(LinkQueue *Q)
{
	if(Q->front->next==NULL)
		return TRUE;
	else
		return FALSE;
}

Status DestroyQueue(LinkQueue *Q)
{
	while(Q->front)
	{
 		Q->rear=Q->front->next;///队尾指针始终指向队头指针的下一个元素
		free(Q->front);
		Q->front=Q->rear;///调整队头指针
	}
	return OK;
}

void main()
{
	LinkQueue Q;
	int choice=1;
	if(InitQueue(&Q)==OK)
		printf("OK\n");
	else
		printf("error!\n");
	while(choice!=0)
	{
		system("cls");
		printf("1.InitQueue         2.EnQueue        3.ShowQueueElement\n");
		printf("4.DeQueue           5.GetHead        6.QueueLength    7.ClearQueue \n");
		printf("8.QueueEmpty        9.DestroyQueue     0.exit\n");
		printf("please input your choice:\n");
		scanf("%d",&choice);
		switch(choice)
		{
		case 1:
			{
				if(InitQueue(&Q)==OK)
					printf("OK\n");
				break;
			}
		case 2:
			{
				int n,i;
				QElemType num;
				printf("请输入要入队的总数:\n");
				scanf("%d",&n);
				for(i=0;i<n;i++)
				{
					scanf("%d",&num);
					if(EnQueue(&Q,num)==OK)
					printf("%d成功入队\n",num);
				}
				break;
			}
		case 3:
			{
				printf("您输入的数为:\n");
				ShowQueueElement(Q);
				break;
			}
		case 4:
			{
				QElemType e;
				int index,i,num;
				printf("请输入您想出队几个数:\n");
				scanf("%d",&num);
				index=QueueLength(Q);
				if(num>index)
					printf("越界了!\n");
				else
				{
					for(i=0;i<num;i++)
					{
						if(DeQueue(&Q,&e)==OK)
						printf("第%d次出队的元素为:%d\n",i+1,e);
					}
				}
				break;
			}
		case 5:
			{
				QElemType e;
				if(GetHead(Q,&e)==OK)
					printf("the head number is %d\n",e);
				break;
			}
		case 6:
			{
				int length;
				length=QueueLength(Q);
				printf("The queue's length is %d\n",length);
				break;
			}
		case 7:
			{
				if(ClearQueue(&Q)==OK)
					printf("成功清空队列!\n");
				else
					printf("清空队列失败!\n");
				break;
			}
		case 8:
			{
				if(QueueEmpty(&Q)==TRUE)
					printf("队列为空!\n");
				else
					printf("队列不为空!\n");
				break;
			}
		case 9:
			{
				if(DestroyQueue(&Q)==OK)
					printf("队列已销毁!\n");
				else
					printf("队列销毁失败!\n");
				break;
			}
		}
		system("pause");
	}
}

循环队列

循环队列就是将队列存储空间的最后一个位置绕到第一个位置,形成逻辑上的环状空间,供队列循环使用。在循环队列结构中,当存储空间的最后一个位置已被使用而再要进入队运算时,只需要存储空间的第一个位置空闲,便可将元素加入到第一个位置,即将存储空间的第一个位置作为队尾。循环队列可以更简单防止伪溢出的发生,但队列大小是固定的。
在循环队列中,当队列为空时,有front=rear,而当所有队列空间全占满时,也有front=rear。为了区别这两种情况,规定循环队列最多只能有MaxSize-1个队列元素,当循环队列中只剩下一个空存储单元时,队列就已经满了。因此,队列判空的条件是front=rear,而队列判满的条件是front=(rear+1)%MaxSize。

循环队列的入队和出队

在这里插入图片描述

循环队列的入队操作
Status EnQueue(SqQueue *Q,QElemType e)
{
	if((Q->rear+1)%MAXQSIZE==Q->front)
		return ERROR;
	Q->base[Q->rear]=e;
	Q->rear=(Q->rear+1)%MAXQSIZE;
	return OK;
}
循环队列的出队操作
Status DeQueue(SqQueue *Q,QElemType *e)
{
	if(Q->front==Q->rear)
		return ERROR;
	*e=Q->base[Q->front];
	Q->front=(Q->front+1)%MAXQSIZE;
	return OK;
}
循环队列的基本操作代码(C语言版)
#include "stdio.h"
#include "stdlib.h"

#define MAXQSIZE 100

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
typedef int QElemType;
typedef int Status;

typedef struct 
{
	QElemType *base;
	int front;
	int rear;
}SqQueue;

Status InitQueue(SqQueue *Q)
{
	Q->base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
	if(!Q->base) 
		exit(OVERFLOW);
	Q->front=0;
	Q->rear=0;
	return OK;
}

Status EnQueue(SqQueue *Q,QElemType e)
{
	if((Q->rear+1)%MAXQSIZE==Q->front)
		return ERROR;
	Q->base[Q->rear]=e;
	Q->rear=(Q->rear+1)%MAXQSIZE;
	return OK;
}

void ShowQueueElement(SqQueue Q)
{
	int i=0;
	for(i=0;i<(Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;i++)
		printf("%d ",Q.base[i]);
		printf("\n");
}

int QueueLength(SqQueue Q)
{
	return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}

Status DeQueue(SqQueue *Q,QElemType *e)
{
	if(Q->front==Q->rear)
		return ERROR;
	*e=Q->base[Q->front];
	Q->front=(Q->front+1)%MAXQSIZE;
	return OK;
}

Status IsFull(SqQueue *Q)
{
	return Q->front==(Q->rear+1)%MAXQSIZE?TRUE:FALSE;
}

Status IsEmpty(SqQueue *Q)
{
	return Q->front==Q->rear?TRUE:FALSE;
}

Status ClearQueue(SqQueue *Q)
{
	Q->front=0;
	Q->rear=0;
	return OK;
}

Status GetHead(SqQueue Q,QElemType *e)
{
	if(Q.front==Q.rear)
		return ERROR;
	*e=Q.base[Q.front];
	return OK;
}

Status DestroyQueue(SqQueue *Q)
{
	if(Q->base)
		free(Q->base);
	Q->base=NULL;
	Q->front=0;
	Q->rear=0;
	return OK;
}
void main()
{
	SqQueue Q;
	QElemType e;
	int choice=1,i;
	if(InitQueue(&Q)==OK)
		printf("OK\n");
	else
		printf("ERROR!\n");
	while(choice!=0)
	{
		system("cls");
		printf("1.InitQueue    2.EnQueue       3.ShowQueueElement \n");
		printf("4.QueueLength   5.DeQueue       6.GetHead  \n");
		printf("7.IsFull        8.IsEmpty       9.ClearQueue \n");
		printf("10.DestroyQueue     0.exit\n");
		printf("please input your choice:\n");
		scanf("%d",&choice);
		switch(choice)
		{
		case 1:
			{
				if(InitQueue(&Q)==OK)
					printf("成功创建一个队列!\n");
				break;
			}
		case 2:
			{
				int n;
				printf("请输入要入队的总数:\n");
				scanf("%d",&n);
				for(i=0;i<n;i++)
				{
					printf("请输入第%d个入队的元素:\n",i+1);
					scanf("%d",&e);
					if(EnQueue(&Q,e)==OK)
						printf("%d入队成功!\n",e);
					else
						printf("队列已满!\n");
				}
				break;
			}
		case 3:
			{
				printf("队列中的元素为:\n");
				ShowQueueElement(Q);
				break;
			}
		case 4:
			{
				int index;
				index=QueueLength(Q);
				printf("队列的长度为:%d\n",index);
				break;
			}
		case 5:
			{
				int num,index,i;
				printf("请输入您想出队几个数:\n");
				scanf("%d",&num);
				index=QueueLength(Q);
				if(num>index)
					printf("越界了!\n");
				else
				{
					for(i=0;i<num;i++)
					{
						if(DeQueue(&Q,&e)==OK)
							printf("第%d次删除的队头元素为:%d\n",i+1,e);
						else
							printf("队列为空!\n");
					}
				}
				break;
			}
		case 6:
		{
			if(GetHead(Q,&e)==OK)
				printf("队头元素为:%d\n",e);
			else
				printf("队列为空!\n");
			break;
		}
		case 7:
			{
				if(IsFull(&Q)==TRUE)
					printf("队列已满!\n");
				else
					printf("队列未满!\n");
				break;
			}
		case 8:
			{
				if(IsEmpty(&Q)==TRUE)
					printf("空队列!\n");
				else
					printf("不是空队列!\n");
				break;
			}
		case 9:
			{
				if(ClearQueue(&Q)==OK)
					printf("清空队列成功!\n");
				else
					printf("清空队列失败!\n");
				break;
			}

		case 10:
			{
				if(DestroyQueue(&Q)==OK)
					printf("销毁队列成功!\n");
				else
					printf("销毁队列失败!\n");
				break;
			}
		}
		system("pause");
	}
}
  • 18
    点赞
  • 182
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值