循环数组实现循环队列 c++

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;
};

你可能感兴趣的:(C++,Algorithm)