数据结构之循环队列(C语言实现)

运用场景:接收或发送数据缓存

具体程序实现如下:
/**
 * 缓存队列长度
 */
#define CIRQUEUE_LEN                200  

typedef unsigned char u8;

typedef struct{
	u8 buf[CIRQUEUE_LEN];
	volatile u8 writeFull:1;   // 接收指针超过读指针一轮
	volatile u8 writeOverFlow:1;  // 溢出一轮
	volatile u8 readOverFlow:1;  // 溢出一轮
	u8 res:5;
	u8 waitReadLen;
	u8 *pRead;
	u8 *pWrite;
}CIRQUEUE;   // 循环队列

static CIRQUEUE cirQueue;

/**
 * 循环队列初始化
 */
void cqInit(void)
{
	cirQueue.pRead = cirQueue.buf;
	cirQueue.pWrite = cirQueue.buf;
	cirQueue.waitReadLen = 0;
	cirQueue.readOverFlow = 0;
	cirQueue.writeOverFlow = 0;
	cirQueue.writeFull = 0;
}

/**
 * 检查队列是否已满
 */
void checkFull(void)
{
	if((cirQueue.pWrite == cirQueue.pRead) && cirQueue.writeOverFlow)
	{ // 接收指针追上读指针,防止数据被覆盖,停止接收
		cirQueue.writeFull = 1;
	}
	else
	{
		cirQueue.writeFull = 0;
	}
}

/**
 * 检查队列长度
 */
void checkLen(void)
{
	if(cirQueue.writeFull)
	{
		cirQueue.waitReadLen = CIRQUEUE_LEN;
		return;
	}
	cirQueue.waitReadLen = (cirQueue.pRead <= cirQueue.pWrite) ? (u8)(cirQueue.pWrite - cirQueue.pRead) : (u8)(cirQueue.pRead - cirQueue.pWrite + CIRQUEUE_LEN);
}

/**
 *  * 读取循环队列
 * @param rByte 读取的字节
 * @return  1:读取成功     0:读取失败
 */
int cqRead(u8 *rByte)
{
	checkLen();
	if(!cirQueue.waitReadLen)
	{  // 
		return 0;
	}

	*rByte = *cirQueue.pRead++;
	cirQueue.writeFull = 0;
	if(cirQueue.pRead > (cirQueue.buf + CIRQUEUE_LEN - 1))
	{
		cirQueue.pRead = cirQueue.buf;
		cirQueue.writeOverFlow = 0;
	}
	return 1;
}

/**
 * 写入循环队列
 * @param wByte [description]
 * @return  1:写入成功     0:写入失败
 */
int cqWrite(u8 *wByte)
{
	checkFull();
	if(cirQueue.writeFull)
	{
		return 0;
	}

	*cirQueue.pWrite++ = *wByte;
	if(cirQueue.pWrite > (cirQueue.buf + CIRQUEUE_LEN - 1))
	{  // 超过一轮,回buf头
		cirQueue.pWrite = cirQueue.buf;
		cirQueue.writeOverFlow = 1;
	}
	return 1;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#include #include #include //队列最大长度 #define MAX_QUEUE 1024 //偷懒,就用静态队列了 static int mQueue[MAX_QUEUE]; //队列插入 void InsertData(int **Front, int **Rear) { if (*Rear + 1 == *Front && (*Rear + 1 - MAX_QUEUE != *Front)) { //当队列数据已满,返回 puts("Queue Size Overflow!\n"); return; } else if (*Rear - mQueue > MAX_QUEUE) { //实现的是类似循环队列,但由于是静态线性队列(数组) //而不是用链表来实现的,所以到静态队列(数组)尾部,尾指针自动指向(数组)头部 *Rear = mQueue; } puts("Input Data:"); scanf("%d", *Rear); //输入数据后,尾指针后移 *Rear += 1; } //从头指针删除一个队列中的数据 void DeleteData(int **Front, int **Rear) { if (*Front == *Rear) { //头指针指针重合,队列空,不能删除,返回 puts("Queue Empty!\n"); return; } else if (*Front - mQueue > MAX_QUEUE) { //参考 Rear *Front = mQueue; } //从头指针删除一个数据 *Front += 1; } //显示队列数据 void ShowData(int **Front, int **Rear) { int *temp; for (temp=*Front; temp!=*Rear; temp++) { printf("%d --> ", *temp); } puts("\n"); } void usage(void) { puts("1. Insert Data"); puts("2. Delete Data"); puts("3. Show Data"); } int main(int argc, char **argv) { //头指针,尾指针 //队列一个特性 First in first out FIFO int *pFront, *pRear; int op_code; //初始化队列,头指针和尾指针此时指向的地址相同 pFront = pRear = mQueue; while (1) { usage(); scanf("%d", &op_code); switch (op_code) { case 1: printf("%p\n", pFront); printf("%d\n", *pFront); InsertData(&pFront, &pRear); break; case 2: DeleteData(&pFront, &pRear); break; case 3: ShowData(&pFront, &pRear); break; default: break; } } return 0; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值