循环队列 sqe_queue

本文介绍了顺序队列的创建、判断空/满状态、入队/出队操作以及解决假溢出问题的方法,通过循环队列来改进顺序队列的性能。
摘要由CSDN通过智能技术生成

顺序队列

存在问题:假溢出——解决办法:-------------->>循环队列

空队列、满队列如何判断?

满队列:rear + 1 = front

空队列:rear = front

/*顺序队列 圆*/
#include "head.h"
#include "sqe_queue.h"
/**/
/*创建顺序队列*/
SQE_QUE *create_sqe_queue(int maxSize)
{
	SQE_QUE *psqe = malloc(sizeof(SQE_QUE));
	if(NULL == psqe)
	{
		perror("fail malloc");
		return NULL;
	}
	psqe->pbase = malloc(maxSize*sizeof(DATA_TYPE));
	if(NULL == psqe)
	{
		perror("fail malloc");
		return NULL;
	}	
	psqe->front = 0;
	psqe->rear = 0;
	psqe->maxSize = maxSize;
	
	return psqe;
}
/*判断空*/
int is_empty_sqequeue(SQE_QUE *psqe)
{
	if(psqe->front ==psqe->rear)
	{
		return 0;
	}
	else
	{
		return 1;
	}

}
/*判断满*/
int is_pull_sqequeue(SQE_QUE *psqe)
{
	if((psqe->rear + 1)%(psqe->maxSize) == psqe->front)
	{
		return 0;
	}
}
/*入队*/
int push_sqe_queue(SQE_QUE *psqe,DATA_TYPE data)
{
//	psqe->pbase = malloc(sizeof(DATA_TYPE));
	if(is_pull_sqequeue(psqe))
	{
		return 0;
	}
	psqe->pbase[psqe->rear] = data;
    psqe->rear = (psqe->rear + 1)%(psqe->maxSize);
}
/*遍历*/
void Show_sqe_queue(SQE_QUE *psqe)
{
	for (int i=psqe->front ; i != psqe->rear ; i=(i+1)%(psqe ->maxSize)) 
	{
		printf("%d ", psqe->pbase[i]);
	}
	putchar('\n');
}
/*出队*/
int pop_sqe_queue(SQE_QUE *psqe)
{
	if(!is_empty_sqequeue(psqe))
	{
		return 0;
	}
	else
	{
		psqe->front = (psqe->front +1)%(psqe->maxSize);
	}
}
/*获取队头数据*/
int sqe_queue_top_msg(SQE_QUE *psqe)
{
	SQE_QUE *p = malloc(sizeof(SQE_QUE));
	if(!is_empty_sqequeue(psqe))
	{
		return 0;
	}
	else
	{
		psqe->pbase[psqe->front] ;
		
		printf("队头数据:%d \n",psqe->pbase[psqe->front]);
	}
}

/*清空队列*/
int clear_sqe_queue(SQE_QUE *psqe)
{
	if(!is_empty_sqequeue(psqe))
	{
		return 0;
	}
	while(1)
	{
		pop_sqe_queue(psqe);
		if(psqe->front == psqe->rear)
		{
			break;
		}
	}
/*
 * psqe->front = psqe->rear =0
 * */

}
/*销毁队列*/
int destory_sqe_queue(SQE_QUE *psqe)
{
	clear_sqe_queue(psqe);
	free(psqe);
	free(psqe->pbase);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值