环形队列 - c语言实现

本文提供了一组C语言代码实现环形队列,包括`loopqueue.c`的环形链表实现,`loopqueue.h`的头文件定义,以及`main.c`的测试用例,展示了环形队列的基本操作。
摘要由CSDN通过智能技术生成

本文包含了三个文件
1. loopqueue.c //环形链表的C语言
2. loopqueue.h //环形链表的头文件
3. main.c //测试代码

loopqueue.c

#include "stdlib.h"
#include "loopQueue.h"
#include "stdio.h"


typedef struct{
    char *buf;
    char head;
    char tail;
    uint32 size;
    uint32 capacity;
}LoopQueue_Handle_T;

PUBLIC LP_HANDLE loopQueue_create(uint32 len){
    LoopQueue_Handle_T* handle = (LoopQueue_Handle_T*)malloc(sizeof(LoopQueue_Handle_T));
    if(!handle){
        printf("loopQueue out of memory\n");
        return SCI_FALSE;
        }

    handle->buf = (char*)malloc(len);
    if(!handle->buf){
        printf("loopQueue out of memory\n");
        return SCI_FALSE;
        }

    handle->tail=0;
    handle->head=0;
    handle->size=0;
    handle->capacity = len;

    return (LP_HANDLE)handle;
}

PUBLIC void loopQueue_delete(LP_HANDLE handle){
    LoopQueue_Handle_T *lp_ptr = (LoopQueue_Handle_T *)handle;

    if(!lp_ptr) 
        return;
    free(lp_ptr->buf);
    free(lp_ptr);
}
//½«len³¤µÄbuf¼ÓÈëµ½lpÖÐ
PUBLIC int loopQueue_push(const LP_HANDLE handle,char *buf,uint32 len){
    uint32 i;
    LoopQueue_Handle_T *lp_ptr = (LoopQueue_Handle_T *)handle;

    if((lp_ptr->size+len)>lp_ptr->capacity){
        printf("loopQueue overflow\n");
        return SCI_FALSE;
        }

    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值