【算法和数据结构】_4_线性结构_队列

  程序代码编译可以通,未进行运行时测试。

  

/*
    本程序测试线性逻辑结构:队列
*/


#include <stdio.h>
#include <stdlib.h>

struct queuelist
{
    int* queue;
    int  front;
    int  rear;
    int  size;
};

typedef struct queuelist QUEUE;
typedef enum {FALSE,TRUE} BOOL;

void CreateQueue(QUEUE* queue,int size);
BOOL IsQueueEmpty(QUEUE* queue);
BOOL IsQueueFull(QUEUE* queue);
BOOL Enqueue(QUEUE* queue,int element);
BOOL Dequeue(QUEUE* queue,int* element);


int main(int argc,char* argv[])
{
    QUEUE queue;
     
    CreateQueue(&queue,10);
    if(IsQueueEmpty(&queue))
        puts("Empty queue.\n");
    getchar();
    getchar();
    return 0;
}

/*
函数功能:
    创建队列
函数原型:
    void CreateQueue(QUEUE* queue,int size)
函数参数:
    QUEUE* queue:要创建的队列的指针
    int size:要创建的队列的大小
返回值:
    无
异常:
    传递指针
*/
void CreateQueue(QUEUE* queue,int size)
{
    if(queue!=NULL && size>0)
    {
        if(queue->queue=(int *)malloc((size+1)*sizeof(int)))
        {
            queue->front=size;
            queue->rear =size;
            queue->size =size;
        }
        else
        {
            puts("Error.\n");
            exit(0);    
        }

    }
    else
    {
        puts("Error.\n");
        exit(0);
    }
}

/*
函数功能:
    判断队列是否为空
函数原型:
    BOOL IsQueueEmpty(QUEUE* queue)
函数参数:
    QUEUE* queue:待判断队列指针
返回值:
    如果为空则返回TRUE,否则返回FALSE
异常:
    传递空指针
*/
BOOL IsQueueEmpty(QUEUE* queue)
{
    if(queue && queue->queue )
    {
        if(queue->front==queue->rear)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
    else
    {
        puts("Error.\n");
        exit(0);
    }
}

/*
函数功能:
    判断队列是否满
函数原型:
    BOOL IsQueueFull(QUEUE* queue)
函数参数:
    QUEUE* queue:待判断队列指针
返回值:
    若队列满,则返回TRUE,否则返回FALSE
异常:
    传递空指针
*/
BOOL IsQueueFull(QUEUE* queue)
{
    if(queue && queue->queue)
    {
        if(queue->rear == 0)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
    else
    {
        puts("Error.\n");
        exit(0);
    }
}

/*
函数功能:
    入栈
函数原型:
    BOOL Enqueue(QUEUE* queue,int element)
函数参数:
    QUEUE* queue:待入队的队列指针
    int element:待入队的元素
*/
BOOL Enqueue(QUEUE* queue,int element)
{
    if(NULL==queue || NULL==queue->queue)
    {
        puts("Error.\n");
        exit(0);
    }
    //如果队列为空
    if(IsQueueEmpty(queue))
    {
        if(queue->front==0)
        {
            //经过出队运算后为空
            queue->front=queue->size;
            queue->queue[queue->front] =element;
            return TRUE;
        }
        else
        {
            //队列为初始化状态的空
            queue->queue[queue->front] =element;
            queue->rear--;
            return TRUE;
        }
    }
    else
    {
        if(IsQueueFull(queue))
        {
            return FALSE;
        }
        queue->queue[queue->rear]=element;
        queue->rear--;
        return TRUE;
    }
}


/*
函数功能:
    出队
函数原型:
    BOOL Dequeue(QUEUE* queue,int* element)
函数参数:
    QUEUE* queue:待吹队队列
    int* element:如果出队成功,则存储出队的元素值
返回值:
    如果出队成功,返回TRUE;
    如果出队失败,返回FALSE,并且设置*element=0;
*/
BOOL Dequeue(QUEUE* queue,int* element)
{
    if(NULL==queue || NULL==queue->queue ||NULL==element )
    {
        puts("Error.\n");
        exit(0);
    }
    if(IsQueueEmpty(queue))
    {
        //队列为空
        *element=0;
        return FALSE;
    }
    else
    {
        //队列不为空
        *element=queue->queue[queue->front];
        queue->front--;
        return TRUE;
    }
}
原文地址:https://www.cnblogs.com/volcanol/p/3033167.html