循环队列的实现
来源:互联网 发布:windows exec函数 编辑:程序博客网 时间:2024/05/23 15:41
需求描述:实现一个固定长度的字符缓存管理结构,实现字符串的写入和读出。
1)队列加数据,向cb尾追加长为length的字符串,存在putting中; 当缓冲剩余空间remain<length时,追加长为remain的字符串,函数返回实际有效存放到缓冲中的字符串;
2)队列取数据,在cb中读取长度为length的字符串,存于getting中;当队列存放的字符数used<length时,只读取长为used的字符串,读取字符串后,队列中相应缓冲不再使用。
特别声明,本文源码在VC6.0上测试通过。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
// 定义队列缓冲长度,实际长度为SIZE-1
#define SIZE 10
// 定义数组循环队列
typedef struct cb_s
{
char *queue;
unsigned long front;
unsigned long rear;
}cb_t;
void cbInit(cb_t **cb, unsigned long size); // 队列初始化函数
unsigned long cbPuts(cb_t *cb, char *putting, unsigned long length); // 队列加数据函数
char *cbGets(cb_t *cb, unsigned long length, char *getting); // 队列取数据函数
void cbDestroy(cb_t *cb); // 队列释放函数
void main(void)
{
cb_t *pcb = NULL;
char *putting = "a+b#c*d-e!f&g";
char getting[SIZE] = {0};
cbInit(&pcb, SIZE);
printf("Test of [cbPuts] operation>>\n");
printf("%d\n\n", cbPuts(pcb, putting, 12));
printf("Test of [cbGets] operation>>\n");
printf("%s\n\n", cbGets(pcb, 5, getting));
printf("Test of [cbPuts] operation>>\n");
printf("%d\n\n", cbPuts(pcb, putting, 4));
printf("Test of [cbGets] operation>>\n");
printf("%s\n\n", cbGets(pcb, 5, getting));
printf("Test of [cbGets] operation>>\n");
printf("%s\n\n", cbGets(pcb, 5, getting));
cbDestroy(pcb);
}
/* 队列初始化函数,cb为队列缓冲,size是缓冲大小 */
void cbInit(cb_t **cb, unsigned long size)
{
*cb = (cb_t *)malloc(sizeof(cb_t));
if ((NULL == *cb) || (NULL == cb))
{
printf("cbInit:Insufficient Memory!\n");
exit(0);
}
(*cb)->queue = (char *)malloc(size * sizeof(char));
if (NULL == (*cb)->queue)
{
printf("cbInit:Insufficient Memory!\n");
exit(0);
}
memset((*cb)->queue, 0, size * sizeof(char));
(*cb)->front = 0;
(*cb)->rear = 0;
}
/* 队列加数据函数,向cb尾追加长为length的字符串,存在putting中;
当缓冲剩余空间remain<length时,追加长为remain的字符串,函数返回实际有效存放到缓冲中的字符串 */
unsigned long cbPuts(cb_t *cb, char *putting, unsigned long length)
{
unsigned long index = 0;
if ((NULL == cb) || (NULL == putting))
{
printf("cbPuts:Insufficient Memory!\n");
exit(0);
}
if ((cb->rear +1) % SIZE == cb->front) // 队列已满
{
printf("Queue is full!\n");
return 0;
}
else // 队列未满
{
for (index = 0; index < length; index++)
{
if ((cb->rear + 1) % SIZE != cb->front) // 队列未满,逐字符入队列
{
cb->rear = (cb->rear + 1) % SIZE;
*(cb->queue + cb->rear) = *(putting + index);
}
else
{
break; // 队列剩余空间<length,队列满时跳出循环;否则,跳过该语句
}
}
return index;
}
}
/* 队列取数据函数,在cb中读取长度为length的字符串,存于getting中;
当队列存放的字符数used<length时,只读取长为used的字符串,读取字符串后,队列中相应缓冲不再使用 */
char *cbGets(cb_t *cb, unsigned long length, char *getting)
{
unsigned long index = 0;
if (NULL == cb)
{
printf("cbGets:Insufficient Memory!\n");
exit(0);
}
if (cb->rear == cb->front) // 队列为空
{
printf("Queue is empty!\n");
return NULL;
}
else // 队列非空
{
for (index = 0; index < length; index++)
{
if (cb->rear != cb->front) // 队列非空,逐字符出队列
{
cb->front = (cb->front + 1) % SIZE;
*(getting + index) = *(cb->queue + cb->front);
*(cb->queue + cb->front) = 0; // 出队列缓存清零
}
else
{
break; // 队列存放字节数<length,队列空时跳出循环;否则,跳过该语句
}
}
*(getting + index) = '\0';
return getting;
}
}
/* 队列释放函数,释放cb占用的内存 */
void cbDestroy(cb_t *cb)
{
if (NULL == cb)
{
printf("cbDestroy:Insufficient Memory/Queue isnot Existing!\n");
exit(0);
}
free(cb->queue);
cb->queue = NULL;
free(cb);
}
- 队列----循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 循环队列的实现
- 关闭spring boot的banner
- hpu 1267 Cafeteria (01背包)
- B
- Codeforces803F Coprime Subsequences
- LeetCode-520. Detect Capital (JAVA)大写字母的合法性
- 循环队列的实现
- android 四大组件之Activity
- Dom3
- 业精于勤荒于嬉
- Shell编程(1)
- 前端经典面试题: 从输入URL到页面加载发生了什么?
- Excel 报表导入导出
- 剑指offer-28.数组中出现次数超过一半的数字
- E