• 链式队列 + 顺序队列 + 顺序循环队列


    #include <stdio.h>
    #include <stdlib.h>
    
    #define OK 1
    #define FALSE 0
    #define ERROR -1
    
    typedef int Status;
    typedef int QElemType;
    
    typedef struct QNode
    {
        QElemType data;
        QNode *next;
    }*QueuePtr;
    
    struct LinkQueue
    {
        QueuePtr front,rear;//队头,队尾指针
    };
    
    Status InitQueue(LinkQueue &Q)
    {
        Q.front->next = NULL;
        return OK;
    }
    
    Status DestroyQueue(LinkQueue &Q)
    {
        while(Q.front){
            Q.rear = Q.front->next;
            free(Q.front);
            Q.front = Q.rear;
        }
        return OK;
    }
    
    Status ClearQueue(LinkQueue &Q)
    {
        QueuePtr p,q;
        Q.rear = Q.front;
        p = Q.front->next;
        Q.front->next = NULL;
        while(p){
            q = p;
            p = p->next;
            free(q);
        }
        return OK;
    }
    
    Status QueueEmpty(LinkQueue Q){
        if(Q.front == Q.rear)
            return true;
        else
            return false;
    }
    
    int QueueLength(LinkQueue Q){
        int i = 0;
        QueuePtr p;
        p = Q.front;
        while(Q.rear != Q.front){
            i++;
            p = p->next;
        }
        return i;
    }
    
    Status GetHead(LinkQueue Q,QElemType &e){
        QueuePtr p;
        if(Q.front == Q.rear)
            return ERROR;
        p = Q.front->next;
        e = p->data;
        return OK;
    }
    
    Status EnQueue(LinkQueue &Q,QElemType e){
        QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
        p->data = e;
        p->next = NULL;
        Q.rear->next = p;
        Q.rear = p;
        return OK;
    }
    
    Status DeQueue(LinkQueue &Q,QElemType &e){
        QueuePtr p;
        if(Q.front == Q.rear)
            return ERROR;
        p = Q.front->next;
        e = p->data;
        Q.front->next = p->next;
        if(Q.rear == p)
            Q.rear = Q.front;
        free(p);
        return OK;
    }
    
    Status QueueTraverse(LinkQueue Q,void(*vi)(QElemType))
    {
        QueuePtr p;
        p = Q.front->next;
        while(p){
            vi(p->data);
            p = p->next;
        }
    
        printf("
    ");
        return OK;
    }
    
    int main()
    {
        return 0;
    }

    顺序队列:

    //顺序队列
    #include <stdio.h>
    #include <stdlib.h>
    
    #define OK 1
    #define FALSE 0
    #define ERROR -1
    #define OVERFLOW -1
    #define MAXQSIZE 20 
    
    typedef int Status;
    typedef int QElemType;
    
    struct SqQueue
    {
        QElemType *base;
        int front;
        int rear;
    };
    
    Status InitQueue(SqQueue &Q){
        Q.base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType));
        //
        Q.front = Q.rear = 0;
        return OK;
    }
    
    Status DestroyQueue(SqQueue &Q){
        if(Q.base)
            free(Q.base);
        Q.base = NULL;
        Q.front = Q.rear = 0;
        return 0;
    }
    
    Status ClearQueue(SqQueue &Q){
        Q.front = Q.rear = 0;
        return OK;
    }
    
    Status QueueEmpty(SqQueue Q){
        if(Q.front == Q.rear)
            return true;
        else
            return false;
    }
    
    int QueueLength(SqQueue Q){
        return (Q.rear - Q.front);
    }
    
    Status GetHead(SqQueue Q,QElemType &e){
        if(Q.front == Q.rear)
            return ERROR;
        e = *(Q.base + Q.front);
        return OK;
    }
    
    Status EnQueue(SqQueue &Q,QElemType e){
        if(Q.rear >= MAXQSIZE)
        {
            Q.base = (QElemType *)realloc(Q.base,(Q.rear + 1) * sizeof(QElemType));
            //
        }
        *(Q.base + Q.rear) = e;
        Q.rear++;
        return OK;
    }
    
    Status DeQueue(SqQueue &Q,QElemType e){
        if(Q.front == Q.rear)
            return ERROR;
        e = Q.base[Q.front];
        Q.front = (Q.front + 1);
        return OK;
    }
    
    Status QueueTraverse(SqQueue Q,void(*vi)(QElemType)){
        int i ;
        i = Q.front;
        while(i != Q.rear){
            vi(*(Q.base + i));
            i++;
        }
        printf("
    ");
        return OK;
    }

    顺序循环队列:

    //顺序循环队列
    #include <stdio.h>
    #include <stdlib.h>
    
    #define OK 1
    #define FALSE 0
    #define ERROR -1
    #define OVERFLOW -1
    #define MAXQSIZE 5 
    
    typedef int Status;
    typedef int QElemType;
    
    struct SqQueue
    {
        QElemType *base;
        int front;
        int rear;
    };
    
    Status InitQueue(SqQueue &Q){
        Q.base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType));
        //
        Q.front = Q.rear = 0;
        return OK;
    }
    
    Status DestroyQueue(SqQueue &Q){
        if(Q.base)
            free(Q.base);
        Q.base = NULL;
        Q.front = Q.rear = 0;
        return 0;
    }
    
    Status ClearQueue(SqQueue &Q){
        Q.front = Q.rear = 0;
        return OK;
    }
    
    Status QueueEmpty(SqQueue Q){
        if(Q.front == Q.rear)
            return true;
        else
            return false;
    }
    
    int QueueLength(SqQueue Q){
        return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
    }
    
    Status GetHead(SqQueue Q,QElemType &e){
        if(Q.front == Q.rear)
            return ERROR;
        e = *(Q.base + Q.front);
        return OK;
    }
    
    Status EnQueue(SqQueue &Q,QElemType e){
        if((Q.rear + 1) % MAXQSIZE == Q.front) //队满
            return ERROR;
        Q.base[Q.rear] = e;
        Q.rear = (Q.rear + 1) % MAXQSIZE;
        return OK;
    }
    
    Status DeQueue(SqQueue &Q,QElemType e){
        if(Q.front == Q.rear)
            return ERROR;
        e = Q.base[Q.front];
        Q.front = (Q.front + 1) % MAXQSIZE;
        return OK;
    }
    
    Status QueueTraverse(SqQueue Q,void(*vi)(QElemType)){
        int i ;
        i = Q.front;
        while(i != Q.rear){
            vi(*(Q.base + i));
            i = (i + 1) % MAXQSIZE;
        }
        printf("
    ");
        return OK;
    }
  • 相关阅读:
    vue element-ui,上传文件加载进度条显示效果(使用定时器实现源码分享)
    vue element-ui 上传文件的 :on-progress钩子无法触发的原因及报错原因
    vue打包文件后首次加载速度慢解决方法----1.压缩文件js.css 2.使用cdn加载
    vue 报错 RangeError: Maximum call stack size exceeded
    vue在IE11报错‘vuex requires a Promise polyfill in this browser.’
    轻松理解MYSQL MVCC 实现机制
    推荐:mysql锁 innodb下的记录锁,间隙锁,next-key锁
    MySQL的四种事务隔离级别
    php中文件上传大小限制如何修改
    Https原理及流程
  • 原文地址:https://www.cnblogs.com/chenyang920/p/5002503.html
Copyright © 2020-2023  润新知