Chinaunix首页 | 论坛 | 博客
  • 博客访问: 70026
  • 博文数量: 13
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 145
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-09 19:43
文章分类

全部博文(13)

文章存档

2009年(5)

2008年(8)

我的朋友

分类: C/C++

2009-04-03 16:46:21

文件:QueueLib.rar
大小:424KB
下载:下载

#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED

//Define the queue element type is void *, so we can change it to all the types

typedef void * QElementType;

//Define the queue sturcture

typedef struct _QueueStruc
{
    int Capacity; //Record the queue size

    int Front; //Record the queue front positon

    int Rear; //Record the queue rear position

    int Size; //Record the queue current data count

    QElementType *data; //The queue element data

} QUEUE_STRUC, *QUEUE_STRUC_P;

/*
 * FUNCTION NAME: Enqueue
 *
 * DESCRIPTION:
 * Add a element to the queue tail.
 *
 * parameter:
 * Q The Queue will be operated
 * data The data will be add to queue tail
 * return : none
 */

void Enqueue(QElementType data, QUEUE_STRUC_P Q);
/*
 * FUNCTION NAME: Dequeue
 *
 * DESCRIPTION:
 * Remove a front element from the current queue.
 *
 * parameter: Q The Queue will be operate
 *
 * return : none
 */

void Dequeue(QUEUE_STRUC_P Q);
/*
 * FUNCTION NAME: FrontAndDequeue
 *
 * DESCRIPTION:
 * Get the front data of the queue, and remove it from the queue
 *
 * parameter:
 * Q The Queue will be operate
 *
 * return : The front data of the queue
 */

QElementType FrontAndDequeue(QUEUE_STRUC_P Q);
/*
 * FUNCTION NAME: CreateQueue
 *
 * DESCRIPTION:
 * Create a Queue
 *
 * parameter:
 * QueueSize The Queue size
 *
 * return :
 * The handler of the Queue
 */

QUEUE_STRUC_P CreateQueue(int QueueSize);
/*
 * FUNCTION NAME: IsQueueEmpty
 *
 * DESCRIPTION:
 * Judge if the queue is empty
 *
 * parameter:
 * Q The Queue will be operated
 *
 * return :
 * 0 The queue is not empty
 * 1 The queue is empty
 */

int IsQueueEmpty(QUEUE_STRUC_P Q);
/*
 * FUNCTION NAME: IsQueueFull
 *
 * DESCRIPTION:
 * Judge if the queue is Full
 *
 * parameter:
 * Q The Queue will be operated
 *
 * return :
 * 0 The queue is not full
 * 1 The queue is full
 */

int IsQueueFull(QUEUE_STRUC_P Q);
/*
 * FUNCTION NAME: DestroyQueue
 *
 * DESCRIPTION:
 * Release the Queue handler, and the memory it malloc
 *
 * parameter:
 * Q The Queue will be operated
 *
 * return : none
 */

void DestroyQueue(QUEUE_STRUC_P Q);
/*
 * FUNCTION NAME: MakeQueueEmpty
 *
 * DESCRIPTION:
 * Make a queue empty
 *
 * parameter:
 * Q The Queue will be make empty
 * return : none
 */

void MakeQueueEmpty(QUEUE_STRUC_P Q);
/*
 * FUNCTION NAME: FrontQueue
 *
 * DESCRIPTION:
 * GEt the queue front data
 *
 * parameter:
 * Q The Queue will be operated
 *
 * return :
 * The front data of the queue. if the queue is empty, return -1;
 */

QElementType FrontQueue(QUEUE_STRUC_P Q);
#endif // QUEUE_H_INCLUDED

/*
* A Simple Queue Lib implement.
*
* Copyright (C) 2009 Mike Shi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/

#include <stdio.h>
#include <malloc.h>
#include "QueueLib.h"

void MakeQueueEmpty(QUEUE_STRUC_P Q)
{
    Q->Front = 0;
    Q->Rear = 0;
    Q->Size = 0;
}

int IsQueueEmpty(QUEUE_STRUC_P Q)
{
    return (Q->Size == 0);
}

int IsQueueFull(QUEUE_STRUC_P Q)
{
    return (Q->Size == Q->Capacity);
}

void Enqueue(void *data, QUEUE_STRUC_P Q)
{
    if (IsQueueFull(Q))
    {
        printf("Queue is full!\n");
        return;
    }

    if (Q->Rear >Q->Capacity-1)
        Q->Rear = 0;

    Q->data[Q->Rear] = data;

    Q->Size++;
    Q->Rear++;
}

void Dequeue(QUEUE_STRUC_P Q)
{
    if (IsQueueEmpty(Q))
    {
        printf("Queue is Empty!\n");
        return;
    }

    Q->Size--;
    Q->Front++;
    if (Q->Front > Q->Capacity-1)
        Q->Front = 0;
}

QElementType FrontQueue(QUEUE_STRUC_P Q)
{
    QElementType data;

    if (Q->Size <= 0)
    {
        printf("Queue is Empty!\n");
        return (QElementType)-1;
    }

    data = Q->data[Q->Front];

    return data;
}

QElementType FrontAndDequeue(QUEUE_STRUC_P Q)
{
    QElementType data;

    if (Q->Size <= 0)
    {
        printf("Queue is Empty!\n");
        return NULL;
    }

    data = Q->data[Q->Front];

    Q->Size--;
    Q->Front++;
    if (Q->Front >Q->Capacity-1)
        Q->Front = 0;

    return data;
}
QUEUE_STRUC_P CreateQueue(int QueueSize)
{
    QUEUE_STRUC_P Q = NULL;

    Q = (QUEUE_STRUC_P)malloc(sizeof(QUEUE_STRUC));
    if (Q == NULL)
    {
        printf("Memory Not Enough!\n");
        return NULL;
    }

    Q->Capacity = QueueSize;
    Q->Front = 0;
    Q->Rear = 0;
    Q->Size = 0;

    Q->data = (QElementType*)malloc(sizeof(QElementType)*Q->Capacity);
    if (Q->data == NULL)
    {
        printf("Memory Not Enough!\n");
        return NULL;
    }

    return Q;
}

void DestroyQueue(QUEUE_STRUC_P Q)
{
    if (Q != NULL)
    {
        free(Q->data);
        free(Q);
    }
}

附CodeBlock下源码的工程文件

阅读(716) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~