【数据结构】C语言实现队列

#include "test_queue.h"
#include <stdio.h>
#include <string.h>
#include "test_queue.c"
#define QUEUE_CACHE_BUFF_SIZE 1024
#define QUEUE_RX_TX_BUF 32
static QUEUE_HandleTypeDef_t txqueue;
QUEUE_DATA_T cache_buff[QUEUE_CACHE_BUFF_SIZE] = {0};
unsigned char txbuf[QUEUE_RX_TX_BUF] = "send buff";
unsigned char rxbuf[QUEUE_RX_TX_BUF] = {0};
int main()
{
    queue_init(&txqueue,cache_buff,QUEUE_CACHE_BUFF_SIZE);
    queue_push(&txqueue,txbuf);
    int count = queue_count(&txqueue);
    printf("count is %d\n",count);
    queue_pop(&txqueue,rxbuf);
    printf("rxbuf :%s\n",rxbuf);
   
    return 0;
}
#ifndef __TEST_QUEUE_H__
#define __TEST_QUEUE_H__

#define QUEUE_DATA_T unsigned char  //队列元素数据类型定义

//类型定义
typedef struct QUEUE_HandleTypeDef
{
    unsigned int head;          //队列头指针
    unsigned int tail;          //队列尾指针
    unsigned int buffer_length; //队列缓存长度(初始化时候赋值)
    QUEUE_DATA_T *buffer;       //队列缓存数组(初始化时候赋值)
}QUEUE_HandleTypeDef_t;

typedef enum
{
    QUEUE_OK        = 0x00U,    //OK
    QUEUE_ERROR     = 0x01U,    //错误
    QUEUE_BUSY      = 0x02U,    //队列忙
    QUEUE_TIMEOUT   = 0x03U,    //队列超时
    QUEUE_OVERLOAD  = 0x04U,    //队列已满
    QUEUE_VOID      = 0x05U     //队列已空
}QUEUE_StatusTypeDef;

//函数声明

void queue_init(QUEUE_HandleTypeDef_t *queue,QUEUE_DATA_T *buff,unsigned int len);
void queue_clear(QUEUE_HandleTypeDef_t *queue);
unsigned int queue_count(QUEUE_HandleTypeDef_t *queue);
QUEUE_StatusTypeDef queue_push(QUEUE_HandleTypeDef_t *queue,QUEUE_DATA_T *data);
QUEUE_StatusTypeDef queue_pop(QUEUE_HandleTypeDef_t *queue,QUEUE_DATA_T *data);
unsigned int queue_push_array(QUEUE_HandleTypeDef_t *queue,QUEUE_DATA_T *data,unsigned int len);
unsigned int queue_pop_array(QUEUE_HandleTypeDef_t *queue,QUEUE_DATA_T *data,unsigned int len);
QUEUE_StatusTypeDef queue_peek(QUEUE_HandleTypeDef_t *queue,QUEUE_DATA_T *data);
unsigned int queue_peek_array(QUEUE_HandleTypeDef_t *queue,QUEUE_DATA_T *data, unsigned int len);
#endif

#include "test_queue.h"
#include <string.h>
#include <stdio.h>
//  函数名称:Queue_Pop
//  函数功能:从队列中弹出数据
//  函数参数:hqueue           队列变量指针
//  函数参数:pdata            待弹出队列的数据缓存地址
//  函数返回:队列状态
QUEUE_StatusTypeDef queue_pop(QUEUE_HandleTypeDef_t *queue,QUEUE_DATA_T *data)
{
    if(queue->head == queue->tail)
    {
        return QUEUE_VOID;
    }
    else
    {
        
        memcpy(data,&queue->buffer[queue->head],32);
        queue->head = (queue->head + 1) % queue->buffer_length;
        return QUEUE_OK;
    }
}
//  函数名称:Queue_Push
//  函数功能:压入数据到队列中
//  函数参数:hqueue           队列变量指针
//  函数参数:data             待压入队列的数据
//  函数返回:队列状态
QUEUE_StatusTypeDef queue_push(QUEUE_HandleTypeDef_t *queue,QUEUE_DATA_T *data)
{
    unsigned int temp = (queue->tail + 1) % queue->buffer_length;
    printf("queue_push enter temp is %d\n",temp);
    if(temp == queue->head)
    {
        printf("queue is empty\n");
        return QUEUE_OVERLOAD;
    }
    else
    {
        printf("data is %s\n",data);
        memcpy (&queue->buffer[queue->tail],data,32);
        queue->tail = temp;
        return QUEUE_OK;
    }
    
}
//  函数名称:Queue_Count
//  函数功能:获取队列内数据的个数
//  函数参数:hqueue           队列变量指针
//  函数返回:队列内数据的个数
unsigned int queue_count(QUEUE_HandleTypeDef_t *queue)
{
    if(queue->tail >= queue->head)
    {
        return (unsigned int)(queue->tail - queue->head);
    }else
    {
        return (unsigned int)(queue->buffer_length + queue->tail - queue->head);
    }
    
}
//  函数名称:Queue_Clear
//  函数功能:清空队列
//  函数参数:hqueue           队列变量指针
//  函数返回:void
void queue_clear(QUEUE_HandleTypeDef_t *queue)
{
    queue->head = 0;
    queue->tail = 0;
}
//  函数名称:Queue_Init
//  函数功能:初始化(创建)队列,每个队列必须先执行该函数才能使用。
//  函数参数:hqueue           队列变量指针
//  函数参数:buffer           队列缓存区地址
//  函数参数:len              队列缓存区长度
//  函数返回:void
void queue_init(QUEUE_HandleTypeDef_t *queue,QUEUE_DATA_T *buffer,unsigned int len)
{
    queue->buffer = buffer;
    queue->buffer_length = len;
    queue_clear(queue);

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值