#include "sbuffer.h"
int sbuffer_init(sbuffer_t ** buffer)
{
*buffer = malloc(sizeof(sbuffer_t));
if (*buffer == NULL) return SBUFFER_FAILURE;
(*buffer)->head = NULL;
(*buffer)->tail = NULL;
return SBUFFER_SUCCESS;
}
int sbuffer_free(sbuffer_t ** buffer)
{
if ((buffer==NULL) || (*buffer==NULL))
{
return SBUFFER_FAILURE;
}
while ( (*buffer)->head )
{
sbuffer_node_t * dummy = (*buffer)->head;
(*buffer)->head = (*buffer)->head->next;
free(dummy);
}
free(*buffer);
*buffer = NULL;
return SBUFFER_SUCCESS;
}
int sbuffer_remove(sbuffer_t * buffer)
{
sbuffer_node_t * dummy;
if (buffer == NULL) return SBUFFER_FAILURE; //case 1, the buffer is NULL
if (buffer->head == NULL) return SBUFFER_NO_DATA; //case 2, the buffer does not have any node inside, it is empty
dummy = buffer->head; //get the head(node), remove and free it later
if (buffer->head == buffer->tail) //the buffer has only one node
{
buffer->head = buffer->tail = NULL;
}
else //case 3, buffer has more than one node
{
buffer->head = buffer->head->next; //delete the first node
}
free(dummy);
return SBUFFER_SUCCESS;
}
int sbuffer_insert(sbuffer_t * buffer, sensor_data_t * data)
{
sbuffer_node_t * dummy;
if (buffer == NULL) return SBUFFER_FAILURE;
dummy = malloc(sizeof(sbuffer_node_t));
if (dummy == NULL) return SBUFFER_FAILURE;
dummy->element.data = *data;
dummy->next = NULL;
if (buffer->tail == NULL) //the buffer is empty
{
buffer->head = buffer->tail = dummy;
}
else //the buffer is not empty
{
buffer->tail->next = dummy;
buffer->tail = buffer->tail->next;
}
return SBUFFER_SUCCESS;
}
sensor_data_t* sbuffer_get_sensor_data_at_index(sbuffer_t * buffer, int index)
{
int counter ;
sbuffer_node_t *dummy;
if(buffer->head == NULL) return (sensor_data_t*)0; // the list is empty, (void *)0 is returned.
else if (index <= 0) return &(buffer->head->element.data); // if 'index' is 0 or negative, the element of the first list node is returned.
for(dummy = buffer->head, counter = 0; dummy->next != NULL ; dummy = dummy->next, counter++)
{
if (counter >= index) return &(dummy->element.data);
}
return &(dummy->element.data); // if 'index' exceeds the size of the list, the element of the last list node is returned.
}
int sbuffer_get_size(sbuffer_t * buffer)
{
int count = 0;
sbuffer_node_t *dummy;
if(buffer->head == NULL) return 0;
for(dummy = buffer->head, count = 1; dummy->next != NULL ; dummy = dummy->next)
{
count++;
}
return count;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
sbuffer.zip (3个子文件)
sbuffer.c 3KB
config.h 2KB
sbuffer.h 2KB
共 3 条
- 1
小码哥_Code
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0