【墨鳌】【C++手写队列】【空间优化不彻底】

template<typename T> 
class Queue{
private:
	vector<T>q;
	int size,top;
public:	
	Queue(){ top=0;q.clear(); }
	int getSize(){ return size; }
	bool empty(){ return (size==0)? free():false; }	
	void push(T x) { q.push_back(x);size++; }	
	T front(){ return q[top]; }
	T pop(){		
		if(!empty()){
			T x=q[top++];
			size--;
			return x;
		}
		else{
			puts("EMPTY ERROR");
			exit(0);
		}
	}
	bool free(){
		if(q.size()) {
			puts("FREE THE QUEUE");
			q.clear();
		}
		top=0;
		return true;
	}	
};
posted @ 2022-02-13 13:36  墨鳌  阅读(43)  评论(0编辑  收藏  举报