Premise:
一开始想在网上找个栗子, 随便改改就行, 但是都如出一辙, 不是本人想要的
于是自己动手写了一个.
Code:
const uint8_t MAXCMDLENTH = 255;
class RecycleQueue
{
public:
RecycleQueue()
{
container = new int[MAXCMDLENTH];
left = 0;
right = 0;
full = false;
}
~RecycleQueue()
{
delete container;
container = nullptr;
}
uint8_t size()
{
if( full ) return MAXCMDLENTH;
if( right >= left ) return right - left;
else {
return (uint16_t)right + MAXCMDLENTH - left;
}
}
void push(int temp)
{
if( size() >= MAXCMDLENTH)
return;
container[right] = temp;
right = (right + 1) % MAXCMDLENTH;
if( right == left ) full = true;
}
void pop()
{
if( size() == 0)
return;
left = ( left + 1) % MAXCMDLENTH;
full = false;
}
int front()
{
return container[left];
}
void clear()
{
left = right;
full = false;
}
private:
int* container;
uint8_t left;
uint8_t right;
bool full;
};
自实现循环队列

本文介绍了一种自定义实现的循环队列数据结构,详细展示了其构造与成员函数,包括push、pop、size等操作,适用于高效的数据管理和缓冲场景。
1990

被折叠的 条评论
为什么被折叠?



