데이터 구조 C 언어 구현 순환 대기 열

4064 단어 데이터 구조
#include 
#include 
//         
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

//          
#define QUEUESIZE 20

//      
typedef int ElemType ;
//          
typedef int State;

//        
typedef struct _CircleQueue
{
    ElemType data[QUEUESIZE];//      
    int front;//     
    int rear;//     
    int count;//      
}CircleQueue;


/*************************************************
Function:       InitQueue
Description:       ,     
Input:               CircleQueue *queue
Output:
Return:             OK
Others:             queue->front = queue->rear = 0
*************************************************/
State InitQueue(CircleQueue *queue)
{
    queue->front = queue->rear = 0;
    queue->count = 0;
    return OK;

}

//        
//1、     count,       ,front   rear
//2、         ,      :(rear+1)%QUEUESIZE=front,   front=rear
//rear            ,    ;      (rear-front+QUEUESIZE)%QUEUESIZE

/*************************************************
Function:       IsQueueEmpty
Description:          
Input:               CircleQueue *queue
Output:
Return:             TRUE,    FALSE
Others:
*************************************************/
State IsQueueEmpty(CircleQueue *queue)
{
    if(queue->count == 0)
        return TRUE;
    else
        return FALSE;


}

/*************************************************
Function:       IsQueueFull
Description:          
Input:               CircleQueue *queue
Output:
Return:             TRUE,    FALSE
Others:
*************************************************/
State IsQueueFull(CircleQueue *queue)
{
    if(queue->count == QUEUESIZE)
        return TRUE;
    else
        return FALSE;


}

/*************************************************
Function:       EnQueue
Description:      
Input:               CircleQueue *queue
                       ElemType e
Output:
Return:             OK,    ERROR
Others:
*************************************************/
State EnQueue(CircleQueue *queue, ElemType e)
{
    //        
    if(queue->count == QUEUESIZE)
    {
        printf("The queue is full");
        return ERROR;
    }
    //  
    queue->data[queue->rear] = e;
    //      
    queue->rear = (queue->rear + 1) % QUEUESIZE;
    //      
    queue->count++;
    return OK;

}

/*************************************************
Function:       DeQueue
Description:      
Input:               CircleQueue *queue
Output:
Return:                 ,      
Others:
*************************************************/
ElemType DeQueue(CircleQueue *queue)
{
    //        
    if(queue->count == 0)
    {
        printf("The queue is empty!");
        exit(EXIT_FAILURE);
    }

    //     
    ElemType e = queue->data[queue->front];
    //      
    queue->front = (queue->front + 1) % QUEUESIZE;
    //      
    queue->count--;

    return e;

}

/*************************************************
Function:       GetHead
Description:         
Input:               CircleQueue *queue
Output:
Return:                 ,      
Others:
*************************************************/
ElemType GetHead(CircleQueue *queue)
{
    //        
    if(queue->count == 0)
    {
        printf("The queue is empty!");
        exit(EXIT_FAILURE);
    }

    return queue->data[queue->front];

}

/*************************************************
Function:       ClearQueue
Description:        
Input:               CircleQueue *queue
Output:
Return:             OK
Others:
*************************************************/
State ClearQueue(CircleQueue *queue )
{
    queue->front = queue->rear = 0;
    queue->count = 0;
    return OK;

}

/*************************************************
Function:       GetLength
Description:           
Input:               CircleQueue *queue
Output:
Return:                
Others:
*************************************************/
int GetLength(CircleQueue *queue)
{
    return queue->count;


}

좋은 웹페이지 즐겨찾기