当前位置:网站首页>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);
}
边栏推荐
- Mysql database underlying foundation column
- 当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
- CEF download, compile project
- All processes of top ten management in project management
- Qt QComboBox QSS样式设置
- 嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
- Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
- C language enumeration type
- 2021-10-28
- 嵌入式系统没有特别明确的定义
猜你喜欢

Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)

Fundamentals of Electronic Technology (III)__ Fundamentals of circuit analysis__ Basic amplifier operating principle

Interruption system of 51 single chip microcomputer

STM32 port multiplexing and remapping

Code word in NR

Working mode of 80C51 Serial Port

Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
![[male nanny style] teach you to open the first wechat applet](/img/a1/a571609ee846adf75506a88a629906.png)
[male nanny style] teach you to open the first wechat applet

没有多少人能够最终把自己的兴趣带到大学毕业上

uniapp 实现微信小程序全局分享及自定义分享按钮样式
随机推荐
一个可执行的二进制文件包含的不仅仅是机器指令
在三线城市、在县城,很难毕业就拿到10K
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
2021-01-03
Education is a pass and ticket. With it, you can step into a higher-level environment
MySQL的简单使用(增删改查)
Getting started with JMX, MBean, mxbean, mbeanserver
CEF下载,编译工程
An executable binary file contains more than machine instructions
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
[graduation successful] [1] - tour [Student Management Information System]
[keil5 debugging] warning:enumerated type mixed with other type
Successful graduation [2] - student health management system function development...
Code word in NR
嵌入式系统没有特别明确的定义
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Stm32f407 key interrupt