基于简单循环数组的队列实现方法

队列代码笔记

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils_def.h"
#include "utils_errcode.h"

typedef struct tagElement
{
    // 队列元素结构体
    CHAR szName[STR_LEN_32];
    ULONG ulAge;
}QUE_ELE_S;

typedef struct tagArrQueue
{
    LONG lFront;        // 队列首部,删除数据
    LONG lRear;         // 队列尾部,插入数据
    ULONG ulArrSize;    // 用来构建队列的数组大小
    ULONG ulElemNum;    // 当前队列的元素个数
    QUE_ELE_S *pstArr;  // 队列元素构成的数组

    /* 其它成员可自行定义,如MUTEX,本Demo忽略 */
}QUEUE_S;

static QUEUE_S g_stQue = { 0 };

QUEUE_S *GetQue()
{
    return &g_stQue;
}

// 获取数组元素个数
ULONG GetArrSize()
{
    return GetQue()->ulArrSize;
}

// 获取当前队列元素个数
ULONG GetEleNum()
{
    return GetQue()->ulElemNum;
}

LONG InitQueue(ULONG ulArrSize)
{
    QUEUE_S *pstQue = NULL;
    ULONG ulDataLen = 0;

    pstQue = GetQue();
    memset(pstQue, 0, sizeof(QUEUE_S));
    pstQue->lFront = 0;
    pstQue->lRear = 0;
    pstQue->ulArrSize = ulArrSize;
    pstQue->ulElemNum = 0;

    // 分配数组内存并初始化
    ulDataLen = sizeof(QUE_ELE_S) * pstQue->ulArrSize;
    pstQue->pstArr = (QUE_ELE_S *)malloc(ulDataLen);
    if (NULL == pstQue->pstArr)
    {
        // log
        return EC_COMMON_OUT_MEM;
    }
    memset(pstQue->pstArr, 0, ulDataLen);
    return EC_COMMON_OK;
}

VOID DestroyQueue()
{
    QUEUE_S *pstQue = NULL;
    pstQue = GetQue();
    if (NULL == pstQue) return;
    if (NULL != pstQue->pstArr)
    {
        free(pstQue->pstArr);
        pstQue->pstArr = NULL;
    }
    
    // 对其它变量进行赋0操作
    memset(pstQue, 0, sizeof(QUEUE_S));
}

BOOL_T IsFull()
{
    QUEUE_S *pstQue = GetQue();
    if (((pstQue->lRear + 1) % pstQue->ulArrSize) == pstQue->lFront)
    {
        return BOOL_TRUE;
    }
    return BOOL_FALSE;
}

BOOL_T IsEmpty()
{
    QUEUE_S *pstQue = GetQue();
    if (pstQue->lFront == pstQue->lRear)
    {
        return BOOL_TRUE;
    }
    return BOOL_FALSE;
}

// 进入队列,此处进行数据复制操作 
LONG EnQue(IN QUE_ELE_S *pstEle)
{
    if (NULL == pstEle) return EC_COMMON_NULL_PTR;
    if (BOOL_TRUE == IsFull())
    {
        // log, 满了
        return EC_COMMON_FULL;
    }
    QUEUE_S *pstQue = GetQue();
    QUE_ELE_S *pstCurElem = NULL;
    pstCurElem = &(pstQue->pstArr[pstQue->lRear]);
    memcpy(pstCurElem, pstEle, sizeof(QUE_ELE_S));
    pstQue->lRear = (pstQue->lRear + 1) % pstQue->ulArrSize;

    // 队列元素个数计数
    pstQue->ulElemNum = pstQue->ulElemNum + 1;
    return EC_COMMON_OK;
}

LONG DeQue(OUT QUE_ELE_S *pstEle)
{
    memset(pstEle, 0, sizeof(QUE_ELE_S));
    if (BOOL_TRUE == IsEmpty())
    {
        // log
        return EC_COMMON_EMPTY;
    }
    QUEUE_S *pstQue = GetQue();
    QUE_ELE_S *pstCurElem = NULL;
    pstCurElem = &(pstQue->pstArr[pstQue->lFront]);
    memcpy(pstEle, pstCurElem, sizeof(QUE_ELE_S));
    memset(pstCurElem, 0, sizeof(QUE_ELE_S));
    pstQue->lFront = (pstQue->lFront + 1) % pstQue->ulArrSize;
    pstQue->ulElemNum = pstQue->ulElemNum - 1;
    return EC_COMMON_OK;
}

VOID testQueue()
{
    LONG lRet = InitQueue(5);

    QUE_ELE_S q0 = { "0.0", 0 };
    QUE_ELE_S q1 = { "1", 1 };
    QUE_ELE_S q2 = { "2", 2 };
    QUE_ELE_S q3 = { "3", 3 };
    QUE_ELE_S q4 = { "4", 4 };
    QUE_ELE_S q5 = { "5", 5 };
    QUE_ELE_S q6 = { "6", 6 };
    QUE_ELE_S q7 = { "7", 7 };
    QUE_ELE_S q8 = { "8", 8 };
    QUE_ELE_S q9 = { "9", 9 };
    QUE_ELE_S out;
    lRet = EnQue(&q0);
    lRet = EnQue(&q1);
    lRet = EnQue(&q2);
    lRet = EnQue(&q3);
    lRet = EnQue(&q4);

    lRet = EnQue(&q5);
    lRet = EnQue(&q6);
    lRet = EnQue(&q7);
    lRet = EnQue(&q8);

    lRet = DeQue(&out);
    lRet = DeQue(&out);
    lRet = DeQue(&out);
    lRet = DeQue(&out);
    lRet = DeQue(&out);
    lRet = DeQue(&out);
    lRet = DeQue(&out);
    lRet = DeQue(&out);
    lRet = DeQue(&out);
    lRet = DeQue(&out);


    DestroyQueue();
    printf("ok\n");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值