顺序表循环队列:创建&初始化、入队、出队、获取队列头数据、计算队列有效数据长度...

/*
    顺序表队列
    初始化
    入队列
    出队列
    取队列头数据
    计算队列有效长度
*/

#include <stdio.h>
#include <stdlib.h>

#define QElemType unsigned int

typedef struct __QueueInfo{
    int *data;
    QElemType front;
    QElemType rear;
    unsigned int capacity;
}QueueInfo;

/* 初始化 */
int initQueue(QueueInfo **ppQ, unsigned int size)
{
    if (NULL == ppQ)
    {
        printf("Queue point address is not exist,error\n");
        return -1;
    }

    if (1 > size)
    {
        printf("initQueue length: %d, error\n", size);
        return -1;
    }
    
    QueueInfo *pNewQueue = (QueueInfo *)malloc(sizeof(QueueInfo));
    if (NULL == pNewQueue)
    {
        printf("initQueue malloc error\n");
        return -1;
    }
    
    pNewQueue->data = (int *)malloc(sizeof(int)*size);
    if (NULL == pNewQueue->data)
    {
        printf("initQueue databuf malloc error\n");
        return -1;
    }
    pNewQueue->capacity = size;
    pNewQueue->front = pNewQueue->rear = 0;
    *ppQ = pNewQueue;
    
    return 0;
}

/* 入队列 */
int pushQueue(QueueInfo *pQ, int val)
{
    if (NULL == pQ)
    {
        printf("Queue is NULL\n");
        return -1;
    }

    if ((pQ->rear + 1)%pQ->capacity == pQ->front)
    {
        printf("Queue Full\n");
        return -1;
    }
    pQ->data[pQ->rear] = val;
    pQ->rear = (pQ->rear +1)%pQ->capacity;
    
    return 0;
}

/* 出队列 */
int popQueue(QueueInfo *pQ, int *val)
{
    if (NULL == pQ)
    {
        printf("Queue NULL\n");
        return -1;
    }

    if (pQ->rear == pQ->front)
    {
        printf("Queue empty\n");
        return -1;
    }
    
    *val = pQ->data[pQ->front];
    pQ->front = (pQ->front + 1)%pQ->capacity;
    
    return 0;
}

/* 取队列头数据 */
int queueFront(QueueInfo *pQ, int *val)
{
    if (NULL == pQ)
    {
        printf("Queue is NULL\n");
        return -1;
    }
    
    *val = pQ->data[pQ->front];

    return 0;
}

/* 计算队列有效数据长度 */
int queueCapacity(QueueInfo *pQ, unsigned int *size)
{
    if (NULL == pQ)
    {
        printf("Queue is NULL\n");
        return -1;
    }
    
    if (pQ->front <= pQ->rear)
    {
        *size = pQ->rear - pQ->front;
    }
    else
    {
        *size = pQ->capacity - pQ->front + pQ->rear;
    }
    
    return 0;
}
/* 测试程序入口 */
int main(void)
{
    QueueInfo *pQueue = NULL;
    int printValue = 0;
    unsigned int sizeOfQueue = 0;
    /* 队列项为10 */
    initQueue(&pQueue, 10);
    /* 连续push11次,检测是否会提示出错 */
    for (int i=0; i<11; i++)
    {
        printValue = 0;
        sizeOfQueue = 0;

        pushQueue(pQueue, i);

        queueCapacity(pQueue, &sizeOfQueue);
        printf("pQueue capacity: %u\n", sizeOfQueue);

        queueFront(pQueue, &printValue);
        printf("pQueue front value: %d\n", printValue);
    }
    /* 连续pop11次,检测是否会提示出错 */
    for (int i=0; i<11; i++)
    {
        printValue = 0;
        sizeOfQueue = 0;

        queueCapacity(pQueue, &sizeOfQueue);
        printf("pQueue capacity: %u\n", sizeOfQueue);

        queueFront(pQueue, &printValue);
        printf("pQueue front value: %d\n", printValue);

        popQueue(pQueue, &printValue);
        printf("pQueue pop value: %d\n", printValue);
    }
    
    free(pQueue);

    return 0;
}

转载于:https://www.cnblogs.com/tedani/p/9989122.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值