数据结构(C语言) -- 循环队列demo

循环队列demo

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

#define MAXSIZE 15
#define FULL 1
#define NONFULL 0
#define EMPTY 3
#define NONEMPTY 2

typedef struct CircularQueue
{
    int front;
    int rear;
    int data[MAXSIZE];
} CircularQueue;

CircularQueue *initQueue()
{
    CircularQueue *CirQueue = (CircularQueue *)malloc(sizeof(CircularQueue));
    CirQueue->front = CirQueue->rear = 0;
    return CirQueue;
}

void printQueue(CircularQueue *CirQueue)
{
    printf("遍历队列 > ");
    int length = (CirQueue->rear - CirQueue->front + MAXSIZE) % MAXSIZE;
    int index = CirQueue->front;
    for (int i = 0; i < length; i++)
    {
        printf("%d -> ", CirQueue->data[index]);
        index = (index + 1) % MAXSIZE;
    }
    printf("OVER\n");
}

int isFull(CircularQueue *CirQueue)
{
    if ((CirQueue->rear + 1) % MAXSIZE == CirQueue->front)
    {
        return FULL;
    }
    else
    {
        return NONFULL;
    }
}

int isEmpty(CircularQueue *CirQueue)
{
    if (CirQueue->front == CirQueue->rear)
    {
        return EMPTY;
    }
    else
    {
        return NONEMPTY;
    }
}

int enCircularQueue(CircularQueue *CirQueue, int data)
{
    if (isFull(CirQueue) == FULL)
    {
        return 0;
    }
    else
    {
        CirQueue->data[CirQueue->rear] = data;
        CirQueue->rear = (CirQueue->rear) + 1 % MAXSIZE;
        printf("%d 入队\n", data);
        return 1;
    }
}

int deCircularQueue(CircularQueue *CirQueue)
{
    if (isEmpty(CirQueue) == EMPTY)
    {
        printf("队列为空!");
    }
    else
    {
        int data = CirQueue->data[CirQueue->front];
        CirQueue->front = (CirQueue->front + 1) % MAXSIZE;
        return data;
    }
}

int main(int argc, char const *argv[])
{
    CircularQueue *CirQueue = initQueue();
    for (int i = 0; i <= 10; i++)
    {
        enCircularQueue(CirQueue, i);
    }
    printf("----------------------------------------------------------\n");
    printQueue(CirQueue);
    printf("----------------------------------------------------------\n");
    for (int i = 0; i <= 10; i++)
    {
        int data = deCircularQueue(CirQueue);
        printf("当前 %d 出队\n", data);
    }
    printf("----------------------------------------------------------\n");
    printQueue(CirQueue);
    return 0;
}

运行结果:

superlan@GodFather:~/C_Language/data_structure$ ./a.out 
0 入队
1 入队
2 入队
3 入队
4 入队
5 入队
6 入队
7 入队
8 入队
9 入队
10 入队
----------------------------------------------------------
遍历队列 > 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> OVER
----------------------------------------------------------
当前 0 出队
当前 1 出队
当前 2 出队
当前 3 出队
当前 4 出队
当前 5 出队
当前 6 出队
当前 7 出队
当前 8 出队
当前 9 出队
当前 10 出队
----------------------------------------------------------
遍历队列 > OVER

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值