《大话数据结构》学习笔记之顺序队列及其源码

/********************************知识点:************************************
队列是只允许在一端(队头)进行插入操作,在另一端(队尾)进行删除操作的线性表(FIFO结构)

循环队列:首尾相连的队列就是循环队列。

循环队列满的条件:(rear + 1) % QueueSize = front    (强调是循环队列,队列满时,保留一个元素,以区别为空的时候)

循环队列为空的条件:rear = front

循环队列队长公式: (rear - front + QueueSize) % QueueSize

***************************************************************************/



#include "declear.h"
#include <stdio.h>

#define MAXSIZE 10

typedef struct  
{
	QElemType data[MAXSIZE];
	int front;
	int rear;
}SqQueue;

Status InitQueue(SqQueue *Q)
{
	Q->front = 0;
	Q->rear = 0;

	return OK;
}

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

Status isFullQueue(SqQueue Q)
{
	if ((Q.rear + 1) % MAXSIZE == Q.front)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

Status InQueue(SqQueue *Q, QElemType elem)
{
	if (isFullQueue(*Q) == TRUE)
	{
		return ERROR;
	}

	Q->data[Q->rear] = elem;
	Q->rear = (++Q->rear) % MAXSIZE;	//循环队列,rear后移一个位置

	return OK;
}

Status DeQueue(SqQueue *Q, QElemType *elem)
{
	if ((*Q).front == (*Q).rear)		//队列为空
	{
		return ERROR;
	}
	
	*elem = Q->data[Q->front];
	Q->front = (++Q->front) % MAXSIZE;	//循环队列,front后移一个位置

	return OK;
}

Status visit(QElemType elem)
{
	printf("%d\n", elem);
	return OK;
}

int main ()
{
	SqQueue queue;
	int len;
//	printf("length = %d\n", QueueLength(queue));
	InitQueue (&queue);
	InQueue (&queue, 4);
	len = QueueLength(queue);

	for (int index = 0; index < len; index++)
	{
		visit(queue.data[(queue.front + index) % MAXSIZE]);
	}
	printf("length = %d\n", QueueLength(queue));

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值