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

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

#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;
}

posted @ 2018-11-20 15:17  thinking......  阅读(655)  评论(0编辑  收藏  举报