• 数据结构-循环队列


    //克服假溢出现象
    #include <stdio.h>
    #include <stdlib.h>
    #define LIST_INIT_SIZE 10
    #define LISTINCREMENT 100
    #define STACK_INIT_SIZE 100
    #define STACKINCREMENT 10
    #define TRUE 1
    #define FALSE 0
    #define true 1
    #define false 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    #define OVERFLOW -2
    #define OPSETSIZE 7
    #define MAXQSIZE 100
    typedef int Status;
    typedef int ElemType;
    typedef int QElemType;
    typedef struct
    {
        QElemType  *base;
        int  front, rear;
    } SqQueue;
    
    Status InitQueue(SqQueue *Q);
    Status EnQueue(SqQueue *Q, ElemType e);
    Status DeQueue (SqQueue *Q, ElemType *e);
    int QueueLength(SqQueue Q);
    
    int main()
    {
        SqQueue Q;
        InitQueue(&Q);
        EnQueue(&Q, 1);
        EnQueue(&Q, 2);
        EnQueue(&Q, 3);
        EnQueue(&Q, 4);
        EnQueue(&Q, 5);
        printf("Then the length of Queue is %d
    ", QueueLength(Q));
        while(QueueLength(Q))
        {
            ElemType E;
            DeQueue(&Q, &E);
            printf("DeQueue , The Elem is %d
    ", E);
        }
        return 0;
    }
    
    
    
    Status InitQueue(SqQueue *Q)
    {
        Q->base = (ElemType *) malloc(MAXQSIZE *sizeof (ElemType));
        if (!Q->base)
            exit (OVERFLOW);
        Q->front = Q->rear = 0;
        return OK;
    }
    
    Status EnQueue(SqQueue *Q, ElemType 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, ElemType *e)
    {
        if (Q->front == Q->rear)
            return ERROR;
        *e = Q->base[Q->front];
        Q->front = (Q->front + 1) % MAXQSIZE;
        return OK;
    }
    int QueueLength(SqQueue Q)
    {
        return (Q.rear - Q.front+MAXQSIZE) % MAXQSIZE;
    }
     

  • 相关阅读:
    3-12 初识JWT
    3-11 用户登录信息验证实现
    3-10 公共模块统一测试
    3-9 公共业务异常统一处理
    3-8 公共返回对象封装
    3-7 公共请求对象封装及优雅验证数据实现
    3-6 MD5介绍及用户添加功能实现
    3-5 用户模块数据层构建
    3-4 用户模块构建
    3-3 公共工具模块构建
  • 原文地址:https://www.cnblogs.com/chinashenkai/p/9451396.html
Copyright © 2020-2023  润新知