⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 orderqueue.c

📁 练习.................................................
💻 C
字号:
#include "string.h"
#include "assert.h"
#include "stdio.h"

#define NAME_MAX_LEN         12

#define QUEUE_MAX_LEN        7

#define NULL_PTR                     0

#define ERR                               1

#define OK                                 0



typedef  enum SEX
{
    MALE,
    FEMALE
}SEX_ENUM;

#define __printf__sex(Sex) \
    ((Sex == 0) ? printf("\r\n     Sex:    %s","MALE") : printf("\r\n     Sex:    %s","FEMALE"))

typedef struct DATA_TAG
{
    char   name[NAME_MAX_LEN];
    int      age;
    SEX_ENUM sex;
}DATA_STRU;

typedef struct QUEUE_TAG
{
    DATA_STRU   pstArray[QUEUE_MAX_LEN];
    DATA_STRU * front;
    DATA_STRU * rear;
}QUEUE_STRU;

int QueueCreate(QUEUE_STRU * pstQueue)
{
    assert(pstQueue);

    pstQueue->front = pstQueue->pstArray;
    pstQueue->rear  =  pstQueue->pstArray;

    return OK;
}

int QueueEmpty(QUEUE_STRU * pstQueue)
{
    assert(pstQueue);
    
    return pstQueue->front == pstQueue->rear;
}

int QueueFull(QUEUE_STRU * pstQueue)
{
    assert(pstQueue);
    
    return (pstQueue->rear - pstQueue->front) == QUEUE_MAX_LEN;
}


int QueueWrite(QUEUE_STRU * pstQueue, int size, DATA_STRU * data)
{
    assert(NULL_PTR != pstQueue && NULL_PTR != data);
    
    if (QueueFull(pstQueue))
    {
        return ERR;
    }
    memcpy(pstQueue->rear, data, size);
    (pstQueue->rear)++;
    return OK;
}

int  QueueRead(QUEUE_STRU * pstQueue, int size, DATA_STRU * data)
{
    assert(NULL_PTR != pstQueue && NULL_PTR != data && size > 0);
    
    if (QueueEmpty(pstQueue))
    {
        return ERR;
    }
    memcpy(data, pstQueue->front, size);
    (pstQueue->front)++;
    
    if (QueueEmpty(pstQueue))
    {
        pstQueue->front = pstQueue->pstArray;
        pstQueue->rear  = pstQueue->pstArray;
    }

    return OK;
}

int main(void)
{
    int i = 1;
    int ret;
    QUEUE_STRU stQueue;
    DATA_STRU    pstPersion[] = { {"MeiMei", 20, FEMALE} , {"LiHongMei", 11, FEMALE},{"YuBo", 28,FEMALE}, 
                                            {"ZhenZhen", 26, MALE}, {"HuangLi", 13,FEMALE},  {"WangQiong", 28,FEMALE}, 
                                            {"XiaWeiWei", 12,FEMALE},{"LiLei", 10,MALE}};
    DATA_STRU stReadData;
   
    QueueCreate(&stQueue);
    
    printf("The Girl  \r\n");
    for (i = QUEUE_MAX_LEN; i >= 0; i--)
    {
        ret = QueueWrite(&stQueue, sizeof(DATA_STRU), &pstPersion[i]);
        if (OK != ret)
        {
            printf("\r\nQueueFull");
        }
        printf("\r\n>>No.%d", i - 1);
        printf("\r\n    Name:    %s", pstPersion[i].name);
        printf("\r\n     Age:    %d",pstPersion[i].age);
        
        __printf__sex(pstPersion[i].sex);
    }

    for (i = 0; i <= QUEUE_MAX_LEN; i++)
    {
        memset(&stReadData, 0, sizeof(stReadData));
        ret = QueueRead(&stQueue, sizeof(DATA_STRU), &stReadData);
        if (OK != ret)
        {
            printf("\r\nQueueEmpty");
        }
        printf("\r\n>>No.%d", i + 1);
        printf("\r\n    Name:    %s", pstPersion[i].name);
        printf("\r\n     Age:    %d",pstPersion[i].age);
        
        __printf__sex(pstPersion[i].sex);
    }

    return 0;
    
}

























⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -