• C语言描述队列的实现及操作(数组实现)


    一.静态数组实现

    1.队列接口

      

    #include<stdio.h>
    
    //    一个队列模块接口
    //    命名为myqueue.h
    
    #define  QUEUE_TYPE    int    //    定义队列类型为int
    
    //    enqueue函数
    //    把一个新值插入队列末尾
    void enqueue(QUEUE_TYPE value);
    
    //    dequeue函数
    //    删除队列首元素并返回
    QUEUE_TYPE dequeue(void );
    
    //    is_empty函数
    //    判断队列是否为空
    bool is_empty(void);
    
    //    is_full函数
    //    判断队列是否已经满
    bool is_full(void);
    
    //    front函数
    //    返回队列第一个值
    QUEUE_TYPE front_value(void);
    
    //    get_size函数
    //    获取队列元素个数
    int get_size(void);

    2.静态数组队列

    #include<stdio.h>
    #include<assert.h>
    #include"myqueue.h"
    
    const    int      QUEUE_SIZE=100;    //队列中元素个数最大限制
    static    QUEUE_TYPE    queue[QUEUE_SIZE+1];    //存储队列中值的数组
    static    int    front = 0;    //指向队列首元素的指针
    static    int rear = 0;    //指向队列尾元素的指针
    
    void enqueue(QUEUE_TYPE value) {
        //    判断队列是否为满
        assert(!is_full());
        //    判断队列是否为空
        if (is_empty())
            queue[front] = value;
        queue[rear] = value;
        rear = (rear + 1) % (QUEUE_SIZE + 1);
    
    }
    
    QUEUE_TYPE dequeue(void) {
        //判断队列是否为空
        assert(!is_empty());
    
        int temp = queue[front];
        front = (front + 1) % (QUEUE_SIZE + 1);
        return temp;
    }
    
    bool is_empty(void) {
        //如果rear==front则队列为空
        return rear == front;
    }
    
    bool is_full(void) {
        //如果(rear+1)%(QUEUE_SIZE+1)==front则队列为满
        return (rear + 1) % (QUEUE_SIZE + 1) == front;
    }
    
    QUEUE_TYPE front_value(void) {
        return queue[front];
    }
    
    int get_size(void) {
        return (rear-front);
    }

    二.动态数组实现

    1.队列接口

    #include<stdio.h>
    //    在原有基础上增加了creat_queue和destroy_queue函数
    #define  QUEUE_TYPE    int    //    定义队列类型为int
    
    //    creat_queue函数
    //    创建一个队列
    void creat_queue(size_t size);
    
    //    destroy_queue函数
    //    销毁队列
    void destroy_queue(void);
    
    //    enqueue函数
    //    把一个新值插入队列末尾
    void enqueue(QUEUE_TYPE value);
    
    //    dequeue函数
    //    删除队列首元素并返回
    QUEUE_TYPE dequeue(void );
    
    //    is_empty函数
    //    判断队列是否为空
    bool is_empty(void);
    
    //    is_full函数
    //    判断队列是否已经满
    bool is_full(void);
    
    //    front函数
    //    返回队列第一个值
    QUEUE_TYPE front_value(void);
    
    //    get_size函数
    //    获取队列元素个数
    int get_size(void);

    2.动态数组队列

    #include<stdio.h>
    #include<assert.h>
    #include<malloc.h>
    
    static QUEUE_TYPE *queue;    //定义队列指针
    static size_t queue_size;    //记录队列大小
    static int  front = 0;
    static int  rear = 0;
    
    
    void creat_queue(size_t size) {
        assert(queue_size == 0);
        queue_size = size;
        queue =(QUEUE_TYPE*) malloc((queue_size+1)*sizeof(QUEUE_TYPE));
        assert(queue != NULL);    
    }
    
    void destroy_queue(void) {
        assert(queue_size > 0);
        queue_size = 0;
        free(queue);
        queue = NULL;
    }
    
    void enqueue(QUEUE_TYPE value) {
        //    判断队列是否为满
        assert(!is_full());
        //    判断队列是否为空
        if (is_empty())
            queue[front] = value;
        queue[rear] = value;
        rear = (rear + 1) % (queue_size + 1);
    }
    
    QUEUE_TYPE dequeue(void) {
        //判断队列是否为空
        assert(!is_empty());
    
        int temp = queue[front];
        front = (front + 1) % (queue_size + 1);
        return temp;
    }
    
    bool is_empty(void) {
        //如果rear==front则队列为空
        return rear == front;
    }
    
    bool is_full(void) {
        //如果(rear+1)%(QUEUE_SIZE+1)==front则队列为满
        return (rear + 1) % (queue_size + 1) == front;
    }
    
    QUEUE_TYPE front_value(void) {
        return queue[front];
    }
    
    int get_size(void) {
        return (rear - front);
    }
  • 相关阅读:
    SpringBoot list查询方法
    eclipse创建web项目
    loadrunner获取返回值为乱码
    连接数据库
    lr并发量和迭代的区别
    LoadRunner11.00入门教程出现的问题
    python学习杂记--函数参数的设置
    adb logcat的命令行开启和关闭
    python学习杂记--pycharm控制台输出乱码
    python学习杂记--装饰器
  • 原文地址:https://www.cnblogs.com/maluning/p/8964052.html
Copyright © 2020-2023  润新知