循环队列的相关设计实现参考1
【摘要】
循环队列的相关设计实现参考1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include ...
循环队列的相关设计实现参考1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define QueueSize 200
typedef char datatype;
//队列的数据元素
typedef struct
{
int front;
int rear;
int count; //计数器,用来记录元素个数
// datatype data[QueueSize]; //数据内容
char* pdata;
int size;
}cirqueue;
cirqueue cq={0};
//置空队
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));
}
}
//判断队满
int QueueFull(cirqueue *q)
{
return (q->count == q->size);
}
//判断队空
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);
}
//入队
void EnQueue(cirqueue *q, datatype x)
{
assert(QueueFull(q) == 0); //q满,终止程序
q->count++;
q->pdata[q->rear] = x;
q->rear = (q->rear + 1)%q->size; //循环队列设计,防止内存浪费
}
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 ++ ;
}
}
//出队
datatype DeQueue(cirqueue *q)
{
datatype temp;
assert(QueueEmpty(q) == 0);//q空,则终止程序,打印错误信息
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
文章来源: blog.csdn.net,作者:悟空胆好小,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/xushx_bigbear/article/details/122905166
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)