zoukankan      html  css  js  c++  java
  • 循环队列

    #include <stdio.h>
    #include <stdlib.h>
    
    //Real capacity is CircularQueueMaxSize -1
    #define CircularQueueMaxSize 1000
    
    typedef int ElementType;
    
    struct CircularQueue
    {
        ElementType QueueData[CircularQueueMaxSize];
        int Front;
        int Rear;
    };
    
    int CircularQueueIsEmpty(struct CircularQueue *Queue)
    {
        return (Queue -> Front == Queue -> Rear);
    }
    
    int CircularQueueIsFull(struct CircularQueue *Queue)
    {
        return ((Queue -> Rear + 1) % CircularQueueMaxSize == Queue -> Front);
    }
    
    struct CircularQueue *CircularQueueInit()
    {
        struct CircularQueue *Queue;
        Queue = malloc(sizeof(struct CircularQueue));
        
        Queue -> Front = Queue -> Rear = 0;
        
        return Queue;
    }
    
    //if Queue is full,return 1 
    int CircularQueueEnqueue(struct CircularQueue *Queue,ElementType ToBeEnqueue)
    {
        if(CircularQueueIsFull(Queue))
        {
            return 1;
        }
        else
        {
            Queue -> Rear = (Queue -> Rear + 1) % CircularQueueMaxSize;
            Queue -> QueueData[Queue -> Rear] = ToBeEnqueue;
        }
        return 0;
    }
    
    //if Queue is empty,return 1 
    ElementType CircularQueueTop(struct CircularQueue *Queue)
    {
        if(CircularQueueIsEmpty(Queue))
        {
            return 1;
        }
        else
        {
            return Queue -> QueueData[(Queue -> Front + 1) % CircularQueueMaxSize];
        }
        return 0;
    }
    
    //if Queue is empty,return 1 
    int CircularQueueDequeue(struct CircularQueue *Queue)
    {
        if(CircularQueueIsEmpty(Queue))
        {
            return 1;
        }
        else
        {
            Queue -> Front = (Queue -> Front + 1) % CircularQueueMaxSize;
            return 0;
        }
    }
    
    int MakeCircularQueueEmpty(struct CircularQueue *Queue)
    {
        Queue -> Front = Queue -> Rear = 0;
        
        return 0;
    }
    
    int CircularQueueDelete(struct CircularQueue *Queue)
    {
        free(Queue);
        Queue = NULL;
        return 0;
    }
    
    int main()
    {
        int test;
        int i;
        
        struct CircularQueue *Queue;
        Queue = CircularQueueInit();
        MakeCircularQueueEmpty(Queue);
        CircularQueueDelete(Queue);
        
        Queue = CircularQueueInit();
        test = CircularQueueTop(Queue);
        CircularQueueDequeue(Queue);
        
        CircularQueueEnqueue(Queue,39);
        test = CircularQueueTop(Queue);
        CircularQueueDequeue(Queue);
        printf("%d
    ",test);
        for(i = 1;i < 2000;i ++)
        {
            CircularQueueEnqueue(Queue,i);
        }
        for(i = 1;i < 500;i ++)
        {
            CircularQueueDequeue(Queue);
        }
        test = CircularQueueTop(Queue);
        CircularQueueDequeue(Queue);
        printf("%d
    ",test);
        return 0;
    }
  • 相关阅读:
    Mac OS X下Maven的安装与配置
    [MAC Eclipse] Eclipse for MAC 中文乱码的解决办法
    The type javax.servlet.http.HttpServletRequest cannot be resolved.
    IOS基础:深入理解Objective-c中@class的含义
    NSJSONSerialization-JSON数据与NSDictionary和NSArray之间的转化
    真机测试时的错误:No matching provisioning profiles found
    转帖Jmeter中的几个重要测试指标释义
    Spring集成log4j日志管理
    Log4J日志配置详解
    使用Redis的理由
  • 原文地址:https://www.cnblogs.com/Asurudo/p/9427305.html
Copyright © 2011-2022 走看看