zoukankan      html  css  js  c++  java
  • 队列ADT

    红心队列的数组实现

    /* 队列的类型声明 */ 
    
    
    #ifndef _Queue_h
    
    struct QueueRecord;
    typedef struct QueueRecord *Queue;
    
    int         IsEmpty( Queue Q );
    int         IsFull( Queue Q );
    Queue       CreateQueue( int MaxElements );
    void        DisposeQueue( Queue Q );
    void        MakeEmpty( Queue Q );
    void        Enqueue( ElementType X, Queue Q );
    ElementType Front( Queue Q );
    void        Dequeue( Queue Q );
    ElementType FrontAndDequeue( Queue Q );
    
    #endif    /* _Queue_h */
    
    
    /* Place in implementation file */
    /* Queue implementation is a dynamically allocated array */
    #define MinQueueSize    ( 5 )
    
    struct QueueRecord
    {
        int Capacity;
        int Front;
        int Rear;
        int Size;
        ElementType *Array;
    };
    /* 测试队列是否为空 */
    
    int 
    IsEmpty( Queue Q )
    {
        return Q->Size == 0;
    }
    /* 构造空队列 */
    
    void
    MakeEmpty( Queue Q )
    {
        Q->Size = 0;
        Q->Front = 1;
        Q->Rear = 0;
    }
    /* 给出Q->Front、Q->Rear自增1后的数组下标值 */
    static int
    Succ( int Value, Queue Q )
    {
        if( ++Value == Q->Capacity )
            Value = 0;
        return Value;
    }
    
    /* 入队 */
    void 
    Enqueue( ElementType X, Queue Q )
    {
        if( IsFull( Q ) )
            Error( "Full queue" );
        else
        {
            Q->Size++;
            Q->Rear = Succ( Q->Rear, Q );
            Q->Array[ Q->Rear ] = X;
        }
    }
    
    /* 出队 */
    void
    Dequeue( Queue Q )
    {
        if( IsEmpty( Q ) )
            Error( "Empty queue" );
        else
        {
            Q->Size--;
            Q->Front = Succ( Q->Front, Q );
        }
    }
    
    /* 返回队头元素,但不出队 */
    ElementType 
    Front( Queue Q )
    {
        if( IsEmpty( Q ) )
            Error( "Empty queue" );
        else
            return Q->Array[ Q->Front ];
    }
    
    /* 返回队头元素,并且出队 */
    ElementType
    FrontAndDequeue( Queue Q )
    {
        ElementType Tmp;
        
        if( IsEmpty( Q ) )
            Error( "Empyt queue" );
        else
        {
            Tmp = Q->Array[ Q->Front ] ;
            Q->Size--;
            Q->Front = Succ( Q->Front, Q );
            
            return Tmp;
        }
    }
    /* 创建队列 */
    
    Queue
    CreateQueue( int MaxElements )
    {
        Queue Q;
        
        if( MaxElements < MinQueueSize )
            Error( "Queue size is too small" );
    
        Q = malloc( sizeof( struct QueueRecord ) );
        if( Q == NULL )
            FatalError( "Out of space!
    " );
    
        Q->Array = malloc( sizeof( ElementType ) * MaxElements );
        if( Q->Array == NULL )
            FatalError( "Out of space!
    " );
        Q->Capacity = MaxElements;
        MakeEmpty( Q );
    
        return Q;
    }
    /* 测试队列是否已满 */
    
    int
    IsFull( Queue Q )
    {
        return Q->Size == Q->Capacity;
    }
    /* 释放队列 */
    
    void
    DisposeQueue( Queue Q )
    {
        if( Q != NULL )
        {
            free( Q->Array );
            free( Q );
        }
    }
  • 相关阅读:
    视图的INSERT、UPDATE、DELETE注意事项
    SQL SERVER 用户管理 TSQL 命令
    SQL SERVER 利用存储过程查看角色和用户信息
    犯错了~
    配置tomcat
    python中的类继承之super
    python中参数解析
    python的几个内联函数:lambda ,zip,filter, map, reduce
    第一次性能测试http_load
    不能在 DropDownList 中选择多个项
  • 原文地址:https://www.cnblogs.com/nufangrensheng/p/3618641.html
Copyright © 2011-2022 走看看