您的位置:首页 > 理论基础 > 数据结构算法

数据结构--循环队列

2017-05-03 16:33 197 查看
head为空值

#define QUEUEMAX 15

#include<stdio.h>

typedef struct

{

        DATA data[QUEUEMAX];

        int head;

        int tail;

}CycQueue;

CycQueue *CycQueueInit()

{

        CycQueue *q;

        if(q = (CycQueue *)malloc(sizeof(CycQueue)))

        {

                q->head = 0;

                q->tail = 0;

                return q;

        }

        else

        {

                return NULL;

        }

}

void CycQueueFree(CycQueue *q)

{

        if(q != NULL)

        free(q);

}

int CycQueueIsEmpty(CycQueue *q)

{

        return (q->head == q->tail);

}

int CycQueueIsFull(CycQueue *q)

{

        return ((q->tail+1)%QUEUEMAX == q->head);

}

int CycQueueIn(CycQueue *q,DATA data)

{

        if((q->tail+1)%QUEUEMAX == q->head)

        {

                printf("queue is full\n");

                return 0;

        }

        else

        {

                q->tail = (q->tail+1)%QUEUEMAX;

                q->data[q->tail] = data;

                return 1;

        }

}

DATA *CycQueueOut(CycQueue *q)

{

        if(q->head == q->tail)

        {

                printf("queue is empty\n");

                return NULL;

        }

        else

        {

                q->head = (q->head+1)%QUEUEMAX;

                return &(q->data[q->head]);

        }

}

int CycQueueLen(CycQueue *q)

{

        int n;

        n = q->tail - q->head;

        if(n < 0)

        n = QUEUEMAX + n;

        return n;

}

DATA *CycQueuePeek(CycQueue *q)

{

        if(q->head == q->tail)

        {

                printf("queue is empty\n");

                return NULL;

        }

        else

        {

                return &(q->data[(q->head+1)%QUEUEMAX]);

        }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: