数据结构//C——静态队列(数组)

# include <stdio.h>
# include <malloc.h>
# include <stdbool.h>

typedef struct Queue{
    int * pBase;
    int len;
    int front;
    int rear;
} QUEUE, * pQUEUE;

void init_queue(pQUEUE);
bool bool_en_queue(pQUEUE, int);
bool bool_de_queue(pQUEUE);
bool bool_full_queue(pQUEUE);
bool bool_empty_queue(pQUEUE);
void traverse_queue(pQUEUE);


int main (void){

    QUEUE queue;
    init_queue(&queue);
    bool_empty_queue(&queue);
    bool_en_queue(&queue, 1);
    bool_en_queue(&queue, 2);
    bool_en_queue(&queue, 3);
    bool_en_queue(&queue, 4);
    bool_en_queue(&queue, 5);
    bool_de_queue(&queue);
    traverse_queue(&queue);

    return 0;
}
void init_queue(pQUEUE pQueue){
    printf("请输入队列长度:");
    scanf("%d", &(pQueue->len));
    pQueue->pBase = (int *) malloc(sizeof(int) * pQueue->len);
    pQueue->front = 0;
    pQueue->rear = 0;
}
bool bool_en_queue(pQUEUE pQueue, int val){
    if(bool_full_queue(pQueue)){
        printf("队列满,无法添加元素:%d\n", val);
        return false;
    }
    pQueue->pBase[pQueue->rear] = val;
    pQueue->rear = (pQueue->rear + 1) % (pQueue->len);
    return true;
}
bool bool_de_queue(pQUEUE pQueue){
    if(bool_empty_queue(pQueue)){
        printf("队列空,无法删除!");
        return false;
    } else{
        printf("删除的元素:%d\n", pQueue->pBase[pQueue->front]);
        pQueue->front = (pQueue->front + 1) % pQueue->len;
        return true;
    }
}
bool bool_full_queue(pQUEUE pQueue){
    int dot = (pQueue->rear + 1) % (pQueue->len);
    if(dot == pQueue->front){
        return true;
    } else{
        return false;
    }
}
bool bool_empty_queue(pQUEUE pQueue){
    if(pQueue->rear == pQueue->front){
        printf("队列空!\n");
        return true;
    } else{
        return false;
    }
}
void traverse_queue(pQUEUE pQueue){
    if(bool_empty_queue(pQueue)){
        printf("队列空,无法遍历!\n");
    } else{
        printf("队列中元素:\n");
    }
    while (pQueue->front != pQueue->rear){
        printf("%d  ", pQueue->pBase[pQueue->front]);
        pQueue->front = (pQueue->front + 1) % pQueue->len;
    }
    printf("\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值