c语言 环形列表

Modified on: Thu, 21 Mar 2019 22:24:35 +0800 热度: 1,217 度
#include "stdlib.h"

typedef struct
{
    int *pQueue;
    int QueueLen;
    int QueueHead;
    int QueueTail;
    int QueueCapative;
}Queue;

void InitQueue(Queue *Q,int Capative);
void DestroyQueue(Queue *Q);
void ClearQueue(Queue *Q);
int QueueEmpty(Queue Q);
int QueueFull(Queue Q);
int EnQueue(Queue *Q,int element);
int DeQueue(Queue *Q,int *element);
void QueueTraverse(Queue Q);

void InitQueue(Queue *Q,int Capative)
{
    Q->QueueCapative=Capative;
    Q->pQueue=(int *)malloc(sizeof(int)*Q->QueueCapative);
    Q->QueueHead=0;
    Q->QueueTail=0;
    Q->QueueLen=0;
}
void ClearQueue(Queue *Q)
{
    Q->QueueHead=0;
    Q->QueueTail=0;
    Q->QueueLen=0;    
} 
void DestroyQueue(Queue *Q)
{
    free(Q->pQueue);
    Q->pQueue=NULL; 
}

int QueueEmpty(Queue Q)
{
    if(Q.QueueLen)
        return 0;
    return 1;
    
}

int QueueFull(Queue Q)
{
    if(Q.QueueLen==Q.QueueCapative)
        return 1;
    return 0;
}

int EnQueue(Queue *Q,int element)
{
    if(QueueFull(*Q))
    {
        return 1;
    }
    Q->pQueue[Q->QueueTail]=element;
    Q->QueueTail++;
    Q->QueueTail%=Q->QueueCapative;
    Q->QueueLen++;
    return 0; 
}

int DeQueue(Queue *Q,int *element)
{
    if(QueueEmpty(*Q))
    {
        return 1;
    }
    *element=Q->pQueue[Q->QueueHead];
    Q->QueueHead++; 
    Q->QueueHead%=Q->QueueCapative;
    Q->QueueLen--;
    
}
void QueueTraverse(Queue Q)
{
    int i;
    for(i=Q.QueueHead;i<Q.QueueLen+Q.QueueHead;i++)
    {
        printf("%-5d",Q.pQueue[i%Q.QueueCapative]);
    }
    putchar(10);
}

添加新评论