一個小隊列,MCU專用

近幾天在做51單片機小車,爲了實時記錄小車狀態,就寫了個微型版的隊列。貼出來,或許大家也能用得上。

//////////////////////////////////////////////////////////////////////////
// Description: micro queue for 51 MCU.
// Author: xu.
// Date:2012-7-24.
//////////////////////////////////////////////////////////////////////////

//#define QCAP 32  // queue capacity. queue swtich.
#ifdef QCAP

typedef unsigned char  	_Ty; // element type.
typedef unsigned char  	_St; // size type.

_Ty queue[QCAP]; // queue instance.
_St size;  // 
_St i;
#define ILLEGAL_VAL 0xff; // 非法值.

void initQ(void)
{
	size = 0;
}

_St pushQ(_Ty state)
{
	if(size < QCAP) {
		queue[size++]=state;
		return 0;
	}
	for(i=1; i<QCAP; ++i ) {
		queue[i-1]=queue[i];					
	}		
	queue[QCAP-1]=state;
	return 1;
}

_Ty popQ(void)
{
	_Ty res;
	if ( size == 0 ) return ILLEGAL_VAL;
	res = queue[0];
	for(i=1; i<size; ++i ) {
		queue[i-1]=queue[i];					
	}
	--size;
	return res;
}

_Ty isempty(void)
{
	return size == 0;
}

#endif // QCAP


 

發佈了31 篇原創文章 · 獲贊 40 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章