queue.h

来自「把中缀表达式转换为后缀表达后」· C头文件 代码 · 共 64 行

H
64
字号
#ifndef QUEUE_H_
#define QUEUE_H_
#include"Node.h"

template<class Queue_entry>
class Queue{
public:
	Queue();
	bool empty()const;
	Error_code append(const Queue_entry &item);
	Error_code serve();
	Error_code retrieve(Queue_entry &item)const;
	~Queue();
protected:
	Node<Queue_entry> *front,*rear;
	int count;
};
template<class Queue_entry>
Queue<Queue_entry>::Queue(){
	front=rear=NULL;
	count=0;
}
template<class Queue_entry>
Error_code Queue<Queue_entry>::append(const Queue_entry &item){
	Node<Queue_entry> *new_rear=new Node<Queue_entry>(item);
	if(new_rear==NULL)
		return overflow;
	if(rear==NULL) 
		front=rear=new_rear;
	else{
		rear->next=new_rear;
		rear=new_rear;
	}
	count++;
	return success;
}
template<class Queue_entry>
Error_code Queue<Queue_entry>::serve(){
	if(front==NULL) return underflow;
	Node<Queue_entry> *old_front=front;
	front=old_front->next;
	if(front==NULL) rear=NULL;
	delete old_front;
	count--;
	return success;
}
template<class Queue_entry>
bool Queue<Queue_entry>::empty()const{
	return front==NULL;
}
template<class Queue_entry>
Error_code Queue<Queue_entry>::retrieve(Queue_entry &item)const{
	if(front==NULL)
		return underflow;
	item=front->entry;
	return success;
}
template<class Queue_entry>
Queue<Queue_entry>::~Queue(){
	while(!empty())
		serve();
}

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?