中科因仑“3+1”工程特种兵精英论坛
标题:
一个不错的队列代码,发给大家看看
[打印本页]
作者:
左耳
时间:
2016-4-7 21:14
标题:
一个不错的队列代码,发给大家看看
从书上抄下来的,觉得不错,发给大家看看
/*******************************************************************************
**File Name : queue.c
**MCU Chip :
**FOSC :
**CCLK :
**Description :
**Author :
**Version :
*******************************************************************************/
#include "queue.h"
/*==============================================================================
** **
** FUNCTION PROTOTYPES **
** **
==============================================================================*/
__STATIC_INLINE void CopyToNode(Node_Type *pq,ITEM item);
__STATIC_INLINE void CopyToItem(ITEM *pitem,Node_Type *pq);
void QueueInit(Queue_Type *pq);
BOOL QueueIsFull(const Queue_Type *pq);
BOOL QueueIsEmpty(const Queue_Type *pq);
UINT8 QueueItemCnt(const Queue_Type *pq);
BOOL EnQueue(Queue_Type *pq,ITEM item);
BOOL DeQueue(Queue_Type *pq,ITEM *pitem);
void EmptyTheQueue(Queue_Type *pq);
/*==============================================================================
** **
** GLOBAL VARIABLES **
** **
==============================================================================*/
/*==============================================================================
** **
** LOCAL GLOBAL VARIABLES **
** **
==============================================================================*/
/******************************************************************************
**Function Name : QueueInit
**Input Parameters : none
**Output Parameters : none
**Returned Value : none
**Descrition : initialize queue
******************************************************************************/
void QueueInit(Queue_Type *pq)
{
pq->pfront = NULL;
pq->prear = NULL;
pq->ucItemCnt = 0;
}
/******************************************************************************
**Function Name : QueueIsFull
**Input Parameters : the queue base pointer
**Output Parameters : none
**Returned Value : 1->full
** 0->not full
**Descrition : initialize queue
******************************************************************************/
BOOL QueueIsFull(const Queue_Type *pq)
{
return (QUEUE_SIZE == pq->ucItemCnt);
}
/******************************************************************************
**Function Name : QueueIsEmpty
**Input Parameters : the queue base pointer
**Output Parameters : none
**Returned Value : 1 -> empty
** 0 -> not empty
**Descrition : initialize queue
******************************************************************************/
BOOL QueueIsEmpty(const Queue_Type *pq)
{
return (0 == pq->ucItemCnt);
}
/******************************************************************************
**Function Name : QueueGetItemCnt
**Input Parameters : the queue base pointer
**Output Parameters : none
**Returned Value : 1 -> empty
** 0 -> not empty
**Descrition : initialize queue
******************************************************************************/
UINT8 QueueItemCnt(const Queue_Type *pq)
{
return (pq->ucItemCnt);
}
/******************************************************************************
**Function Name : CopyToNode
**Input Parameters : Queue_Type *pq -> the queue base pointer
** ITEM item -> the value of item
**Output Parameters : none
**Returned Value : none
**Descrition : initialize queue
******************************************************************************/
__STATIC_INLINE void CopyToNode(Node_Type *pq,ITEM item)
{
pq->item = item;
}
/******************************************************************************
**Function Name : EnQueue
**Input Parameters : Queue_Type *pq -> the queue base pointer
** ITEM item -> the value of item
**Output Parameters : none
**Returned Value : 1 -> enqueue successful
** 0 -> enqueue failure
**Descrition : initialize queue
******************************************************************************/
BOOL EnQueue(Queue_Type *pq,ITEM item)
{
Node_Type *pnew;
if(QueueIsFull(pq))
{
return FALSE;
}
pnew = (Node_Type *)malloc(sizeof(Node_Type)); //obtain a memory space
if(pnew == NULL) //obtain a memory space fail ?
{
return FALSE;
}
CopyToNode(pnew,item); //copy to node
pnew->pnext = NULL; //restore pointer
if(QueueIsEmpty(pq))
{
pq->pfront = pnew; //the new node is the first one
}
else
{
pq->prear->pnext = pnew; //add to the tail
}
pq->prear = pnew; //the new node is the last one
pq->ucItemCnt++; //increase the item counter
return TRUE;
}
/******************************************************************************
**Function Name : CopyToNode
**Input Parameters : Queue_Type *pq -> the queue base pointer
** ITEM item -> the value of item
**Output Parameters : none
**Returned Value : none
**Descrition : initialize queue
******************************************************************************/
__STATIC_INLINE void CopyToItem(ITEM *pitem,Node_Type *pq)
{
*pitem = pq->item;
}
/******************************************************************************
**Function Name : DeQueue
**Input Parameters : Queue_Type *pq -> the queue base pointer
** ITEM item -> the value of item
**Output Parameters : none
**Returned Value : 1 -> dequeue successful
** 0 -> dequeue failure
**Descrition : initialize queue
******************************************************************************/
BOOL DeQueue(Queue_Type *pq,ITEM *pitem)
{
Node_Type *pt;
if(QueueIsEmpty(pq)) //the queue is empty ?
{
return FALSE;
}
CopyToItem(pitem,pq->pfront); //copy the node to item
pt = pq->pfront; //get the basic point of the first node
pq->pfront = pq->pfront->pnext; //now the next node is the first node
free(pt); //free the space
pq->ucItemCnt--;
if(0 == pq->ucItemCnt) //the queue is empty?
{
pq->prear = NULL;
}
return TRUE;
}
/******************************************************************************
**Function Name : EmptyTheQueue
**Input Parameters : Queue_Type *pq -> the queue base pointer
** ITEM item -> the value of item
**Output Parameters : none
**Returned Value : 1 -> dequeue successful
** 0 -> dequeue failure
**Descrition : initialize queue
******************************************************************************/
void EmptyTheQueue(Queue_Type *pq)
{
ITEM dummy;
while(!(QueueIsEmpty(pq)))
{
DeQueue(pq,&dummy);
}
}
#ifndef _queue_h
#define _queue_h
#include "type.h"
#include "string.h"
#include "stdlib.h"
/*==============================================================================
** **
** IO DEFINITION **
** **
==============================================================================*/
/*==============================================================================
** **
** CONSTANT DEFINITION **
** **
==============================================================================*/
#define QUEUE_SIZE 10
/*==============================================================================
** **
** TYPEDEF DEFINITION **
** **
==============================================================================*/
typedef unsigned char ITEM ;
typedef struct node{
ITEM item;
struct node *pnext;
}Node_Type;
typedef struct queue{
Node_Type *pfront; //指向队列首的指针
Node_Type *prear; //指向队列尾的指针
UINT8 ucItemCnt; //队列中项目的个数
}Queue_Type;
/*==============================================================================
** **
** FUNCTION PROTOTYPES **
** **
==============================================================================*/
void QueueInit(Queue_Type *pq);
BOOL QueueIsFull(const Queue_Type *pq);
BOOL QueueIsEmpty(const Queue_Type *pq);
UINT8 QueueItemCnt(const Queue_Type *pq);
BOOL EnQueue(Queue_Type *pq,ITEM item);
BOOL DeQueue(Queue_Type *pq,ITEM *pitem);
void EmptyTheQueue(Queue_Type *pq);
#endif
欢迎光临 中科因仑“3+1”工程特种兵精英论坛 (http://bbs.enlern.com/)
Powered by Discuz! X3.4