队列的总结

队列也称为先进先出表(FIFO)

标准模板库
#include
方法
append() //将元素加入队尾
server()  //删除队头元素
retrieve() //返回队头元素
empty() //如果队头为空,返回true;否则返回false
实现
const int maxqueue=10;

class Queue{
public:
	Queue();
	bool empty() const;
	Error_code serve();
	Error_code append(const Queue_entry &item);
	Error_code retrieve(Queue_entry &item)const;
	protected:
	int count;
	int front, rear;
	Queue_entry entry(maxqueue);
};

Queue::Queue()
{
	count=0;
	rear=maxqueue-1;
	front=0;
}

bool Queue::empty() const
{
	return count==0;
}

Error_code Queue::append(const Queue_entry &item)
{
	if(count>=maxqueue)
		return overflow;
	count++;
	rear=((rear+1)==maxqueue)?0:(rear+1);
	entry[rear]=item;
	return success;
}

Error_code Queue::serve()
{
	if(count<=0)
		return underflow;
	count--;
	front=((front+1)==maxqueue)?0:(front+1);
	return success;
}

Error_code Queue::retrieve(Queue_entry &item)const
{
	if(count<=0)
		return underflow;
	item=entry[front];
	return success;
}

你可能感兴趣的:(数据结构)