create queue template use c

Document created by 利中 舒 on Jun 1, 2015Last modified by Diana Torres on Feb 24, 2017
Version 2Show Document
  • View in full screen mode

/*

* Queue.h

*

*  Created on: May 28, 2015

*      Author: ShuLizhong

*/

 

 

#ifndef QUEUE_H_

#define QUEUE_H_

#ifdef _cplusplus

extern "C" {

#endif

/*If you want to change the queue type(QUEUE_TYPE) and queue max size(QUEUE_MAX_SIZE),

  you should define it at front of include queue.h file.

eg:

******in xxx.h file*****

code****

#define QUEUE_TYPE   Other type(unsigned int)

#define QUEUE_MAX_SIZE   Other size(100)

#include "qeue.h"

code****

*/

#ifndef QUEUE_TYPE

#define QUEUE_TYPE unsigned char

#endif

#ifndef QUEUE_MAX_SIZE

#define QUEUE_MAX_SIZE 100

#endif

#define bool unsigned int

typedef enum {

  OK,

  FULL,

  EMPTY

}QUEUE_STATUS;

typedef struct {

  unsigned int tail;

  unsigned int head;

  unsigned int size;

  unsigned int length;

  QUEUE_TYPE data[QUEUE_MAX_SIZE];

}Queue_tag,*pQueue_tag;

 

 

__inline void InitQueue(pQueue_tag q)

{

  q->tail = q->head = q->size = 0;

  q->length = QUEUE_MAX_SIZE;

}

__inline  QUEUE_STATUS EnQueue(pQueue_tag q,QUEUE_TYPE data)

{

  if(q->size++ == QUEUE_MAX_SIZE)

  return FULL;

  q->data[q->tail] = data;

  q->tail = (q->tail+1) % QUEUE_MAX_SIZE;

  return OK;

}

__inline QUEUE_STATUS DeQueue(pQueue_tag q, QUEUE_TYPE *data)

{

  if(q->size-- == 0)

  return EMPTY;

  *data = q->data[q->head];

  q->head = (q->head+1) % QUEUE_MAX_SIZE;

  return OK;

}

__inline bool IsQueueEmpty(pQueue_tag q)

{

  return q->size == 0;

}

__inline bool IsQueueFull(pQueue_tag q)

{

  return q->size == QUEUE_MAX_SIZE;

}

__inline unsigned int GetQueueSize(pQueue_tag q)

{

  return q->size;

}

__inline unsigned int GetQueueLength(pQueue_tag  q)

{

  return q->length;

}

/*__inline unsigned int DeMoreBytesFromQueue(pQueue_tag q,QUEUE_TYPE *data,unsigned int len)

{

  unsigned int i = 0;

  len++;

  return 0;

}*/

 

 

#ifdef _cplusplus

}

#endif

#endif /* QUEUE_H_ */

Attachments

    Outcomes