近几天在做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