关闭

SeqQueue--顺序循环队列

标签: nulldateinsertfilestruct
1217人阅读 评论(0) 收藏 举报
分类:
看完复仇看了看队列。花了半小时来编写、调试。写完博客再去温习下枪火
/*
** 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*/
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9459次
    • 积分:197
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类