Poison的专栏

编程达人

SeqQueue--顺序循环队列
看完复仇看了看队列。花了半小时来编写、调试。写完博客再去温习下枪火
/*
** File name: SeqQueue.h
** Author: ZhouFeng
** Date: 2012/03/27
*/
#ifndef SEQ_QUEUE_H
#define SEQ_QUEUE_H

#define BOOL int
#define ERROR 0
#define SUCCESS 1
#define TRUE 1
#define FALSE 0
#define MAX_SIZE 100

typedef int EleType;
typedef struct SeqQueue
{
    EleType _seqQueue[MAX_SIZE];
    /* End of queue */
    int nRear;
    /* Front of queue */
    int nFront;
    /* Counter number of element */
    int nCount;
}SeqQueue;

/* Operation */
void InitSeqQueue(SeqQueue *Q);
int SeqQueueAppend(SeqQueue *Q, EleType data);
int SeqQueueDelete(SeqQueue *Q, EleType *data);
BOOL IsQueueEmpty(SeqQueue *Q);
int GetQueue(SeqQueue *Q, EleType *data);

#endif
/*
** File name: SeqQueue.c
** Author: ZhouFeng
** Date: 2012/03/27
*/
#include <stdlib.h>
#include <stddef.h>
#include "SeqQueue.h"

void InitSeqQueue(SeqQueue *Q)
{
    /*  Deal Q is NULL pointer. */
    if(Q == NULL)
    {
	return;
    }

    Q->nRear = 0;
    Q->nFront = 0;
    Q->nCount = 0;
}

int SeqQueueAppend(SeqQueue *Q, EleType data)
{
    if(Q == NULL)
    {
	return ERROR;
    }

    /*  Deal Q is full. */
    if(Q->nCount > 0 && Q->nFront == Q->nRear)
    {
	return ERROR;
    }

    /* Insert at the end of Queue */
    Q->_seqQueue[Q->nRear] = data;
    ++(Q->nRear);
    Q->nRear %= MAX_SIZE;
    ++(Q->nCount);

    return SUCCESS;
}

int SeqQueueDelete(SeqQueue *Q, EleType *data)
{
    if(Q == NULL)
    {
	return ERROR;
    }

    /* Deal with Q is empty. */
    if(Q->nCount == 0)
    {
	return ERROR;
    }

    *data = Q->_seqQueue[Q->nFront];
    Q->nFront += 1;
    Q->nFront %= MAX_SIZE;
    --(Q->nCount);

    return SUCCESS;
}

BOOL IsQueueEmpty(SeqQueue *Q)
{
    if(Q == NULL)
    {
	return FALSE;
    }

    if(Q->nCount == 0)
    {
	return TRUE;
    }
    else
    {
	return FALSE;
    }
}

int GetQueue(SeqQueue *Q, EleType *data)
{
    if(Q == NULL)
    {
	return ERROR;
    }

    if(Q->nCount == 0)
    {
	return FALSE;
    }

    *data = Q->_seqQueue[Q->nFront];

    return SUCCESS;
}
/* End ~SeqQueue.c*/
阅读更多
个人分类: C&Data Structure
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

SeqQueue--顺序循环队列

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭