当前位置:网站首页>Circular queue related design and implementation reference 1
Circular queue related design and implementation reference 1
2022-07-03 09:59:00 【Wukong is so timid】
Circular queue related design and implementation reference 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define QueueSize 200
typedef char datatype;
// Data elements of the queue
typedef struct
{
int front;
int rear;
int count; // Counter , Used to record the number of elements
// datatype data[QueueSize]; // The data content
char* pdata;
int size;
}cirqueue;
cirqueue cq={
0};
// Empty team
int InitQueue(cirqueue *q,int size)
{
q->front = q->rear = 0;
q->count = 0;
q->pdata = (char*)malloc(size);
q->size = size;
if(q->pdata == NULL)
return -1;
return 0;
}
void DeInitQueue( cirqueue *q)
{
if (q->pdata)
{
free(q->pdata);
memset(q,0,sizeof(cirqueue));
}
}
// Judge that the team is full
int QueueFull(cirqueue *q)
{
return (q->count == q->size);
}
// Judge team empty
int QueueEmpty(cirqueue *q)
{
return (q->count == 0);
}
int Queue_Data_len( cirqueue *q)
{
return q->count;
}
int Queue_Left_Space( cirqueue *q)
{
return (q->size-q->count);
}
// The team
void EnQueue(cirqueue *q, datatype x)
{
assert(QueueFull(q) == 0); //q full , To terminate the program
q->count++;
q->pdata[q->rear] = x;
q->rear = (q->rear + 1)%q->size; // Cyclic queue design , Prevent memory waste
}
void en_queue_multi(cirqueue *cq, unsigned char *data, int len )
{
int copy_len;
int left_len;
int idx = 0;
left_len = Queue_Left_Space(cq);
if (left_len < len) {
printf( "%s circle_queue space left,not enough,need=%d\n", __func__,len);
copy_len = left_len;
}
else
copy_len = len;
while(copy_len--)
{
EnQueue(cq,*(data+idx));
idx ++ ;
}
}
// Out of the team
datatype DeQueue(cirqueue *q)
{
datatype temp;
assert(QueueEmpty(q) == 0);//q empty , Then terminate the procedure , Print error messages
temp = q->pdata[q->front];
q->count--;
q->front = (q->front + 1)%q->size;
return temp;
}
int de_queue_multi( cirqueue *cq, unsigned char *data, int len)
{
int copy_len;
int total_len;
int idx = 0;
total_len = Queue_Data_len(cq);
if (total_len < len) {
printf( "%s circle_queue data only=%d ,need=%d\n", __func__,total_len,len);
copy_len = total_len;
}
else
copy_len = len;
while(copy_len--)
{
*(data+idx)=DeQueue(cq);
idx ++ ;
}
}
void Peek_Queue( cirqueue *q, unsigned char *data)
{
if (QueueEmpty(q)) {
printf("%s circle_queue is empty, data is invalid\n", __func__);
return;
}
*data = q->pdata[q->front];
}
void cirqueue_test(void)
{
printf("cirqueue_test=%s\n",__DATE__);
InitQueue(&cq,QueueSize);
EnQueue(&cq,0x12);
EnQueue(&cq,0x34);
unsigned char data[5]={
0x12,0x78,0x89,0x68,0x59};
en_queue_multi(&cq,data,5);
printf("Queue_Data_len=%d\n",Queue_Data_len(&cq));
printf("Queue_Left_Space=%d\n",Queue_Left_Space(&cq));
int len = Queue_Data_len(&cq);
int i = 0;
unsigned char data_temp = 0;
Peek_Queue(&cq,&data_temp);
printf("data_temp=0x%x\n",data_temp);
for(i=0;i<len;i++)
printf("Queue data[%d]=0x%x\n",i,DeQueue(&cq)&0xff);
printf("Queue_Data_len=%d\n",Queue_Data_len(&cq));
printf("Queue_Left_Space=%d\n",Queue_Left_Space(&cq));
DeInitQueue(&cq);
}
边栏推荐
- Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
- Working mode of 80C51 Serial Port
- 内存数据库究竟是如何发挥内存优势的?
- Basic knowledge of communication interface
- Simple use of MySQL (addition, deletion, modification and query)
- Runtime.getRuntime().gc() 和 Runtime.getRuntime().runFinalization() 的区别
- Project cost management__ Cost management technology__ Article 8 performance review
- 【順利畢業】[1]-遊覽 [學生管理信息系統]
- Timer and counter of 51 single chip microcomputer
- Project cost management__ Topic of comprehensive calculation
猜你喜欢
[22 graduation season] I'm a graduate yo~
03 fastjason solves circular references
Vector processor 9_ Basic multilevel interconnection network
Education is a pass and ticket. With it, you can step into a higher-level environment
ADS simulation design of class AB RF power amplifier
[CSDN] C1 training problem analysis_ Part II_ Web Foundation
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
UCI and data multiplexing are transmitted on Pusch - placement of data and UCI positions (Part III)
03 fastjason solves circular references
随机推荐
嵌入式系统没有特别明确的定义
Application of external interrupts
Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
Project cost management__ Cost management technology__ Article 8 performance review
內存數據庫究竟是如何發揮內存優勢的?
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Design of charging pile mqtt transplantation based on 4G EC20 module
SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
Fundamentals of Electronic Technology (III)_ Integrated operational amplifier and its application__ Basic arithmetic circuit
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
JMX、MBean、MXBean、MBeanServer 入门
Interruption system of 51 single chip microcomputer
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
[22 graduation season] I'm a graduate yo~
Basic knowledge of communication interface
Mysql database underlying foundation column
03 FastJson 解决循环引用
GPIO port details, Hal library operation keys