其實,隊列也可以這麼來替代。

兩個棧來代替一個隊列,一個棧正常存儲數據,另一個棧在執行top()時候存儲“從第一個棧中去取出來的數據”,並將第二個棧的頂端數據彈出即可。

#include <iostream>
#include <malloc.h>
#include <algorithm>
#include <functional>



//------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------
// 基類

class UnCopyable{
public:
	UnCopyable();
	~UnCopyable();

private:
	UnCopyable(const UnCopyable& e);
	UnCopyable& operator=(const UnCopyable& e);
};

UnCopyable::UnCopyable(){

}

UnCopyable::~UnCopyable(){

}

UnCopyable::UnCopyable(const UnCopyable& e) {
	*this = e;
}
UnCopyable& UnCopyable::operator=(const UnCopyable&) {
	return *this;
}


// ------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------
// 基類UnCopyable是爲了阻止MyQueue可以被賦值和拷貝

class MyQueue : private UnCopyable {

public:
	MyQueue( unsigned int size ) 
		: m_Top( -1 ), m_Top_2(-1), 
		  m_MAX( -100000 ), m_Min( 100000 )
	{
		m_VAPACITY = size;
		m_Data = (int*)malloc(sizeof(int)*m_VAPACITY);
		m_Data_2 = (int*)malloc(sizeof(int)*m_VAPACITY);
	}
	~MyQueue();
private:
	int *m_Data;													//	存取值的數組
	int *m_Data_2;													//	存取值的數組
	int  m_Top;														//  棧頂索引
	int  m_Top_2;													//  棧頂索引
	int  m_MAX;														//  數組中的最大值
	int  m_Min;														//  數組中的最小值
	unsigned int m_VAPACITY;										//  棧的容量
public:
	enum QueueSort
	{
		less,
		greater
	};

	unsigned int QueueVapacity() const;								//  棧的容量
	unsigned int QueueMax() const;									//  棧最大
	unsigned int QueueMin() const;									//  棧最小
public:
	bool isEmpty();													//  判斷棧是否爲空
	bool isFull();													//  判斷棧是否滿了
	bool isEmpty_2();												//  判斷棧是否爲空
	bool isFull_2();												//  判斷棧是否滿了
	void pop();														//  出棧(刪除棧頂元素)
	void push( int value );											//  入棧
	void pop_2();													//  出棧(刪除棧頂元素)
	void push_2(int value);											//  入棧
	unsigned int Top();												//  取值
	unsigned int Top_2();
	unsigned int Tops();
	int max();														//  棧中最大值(版本1)
	int min();														//  棧中最小值(版本1)
	int max1();														//  棧中最大值(版本2)
	int min1();														//  棧中最小值(版本2)
	void QueueOrder( QueueSort Strategy );							//  將棧中的元素排序
};


unsigned int MyQueue::QueueVapacity() const {
	return m_VAPACITY;
}

unsigned int MyQueue::QueueMax() const {
	return m_MAX;
}

unsigned int MyQueue::QueueMin() const {
	return m_Min;
}

bool MyQueue::isEmpty(){
	return m_Top == -1;
}

bool MyQueue::isEmpty_2() {
	return m_Top_2 == -1;
}

bool MyQueue::isFull() {
	return m_Top == m_VAPACITY - 1;
}

bool MyQueue::isFull_2() {
	return m_Top_2 == m_VAPACITY - 1;
}

void MyQueue::pop() {
	try{
		 if (!isEmpty()) {
			 m_Top--;
		 }
		 else{
			 std::cout << "the Queue is empty." << std::endl;
		 }
	}
	catch (const std::exception& e){
		std::cout << e.what() << std::endl;
	}	
}

void MyQueue::push( int value ) {
	try{
		if (!isFull()) {
			m_Data[m_Top++] = value;
		}
		else{
			std::cout << "the Queue is full." << std::endl;
		}
	}
	catch (const std::exception& e){
		std::cout << e.what() << std::endl;
	}
}

void MyQueue::pop_2() {
	try {
		if (!isEmpty_2()) {
			m_Top_2--;
		}
		else {
			std::cout << "the Queue is empty." << std::endl;
		}
	}
	catch (const std::exception& e) {
		std::cout << e.what() << std::endl;
	}
}

void MyQueue::push_2(int value) {
	try {
		if (!isFull_2()) {
			m_Data_2[m_Top_2++] = value;
		}
		else {
			std::cout << "the Queue is full." << std::endl;
		}
	}
	catch (const std::exception& e) {
		std::cout << e.what() << std::endl;
	}
}

unsigned int MyQueue::Top() {
	if (!isEmpty()) {
		int Temp = m_Data[m_Top];
		m_Top--;
		return Temp;
	}
}

unsigned int MyQueue::Top_2() {
	if (!isEmpty()) {
		int Temp = m_Data_2[m_Top_2];
		m_Top_2--;
		return Temp;
	}
}

unsigned int MyQueue::Tops() {
	if (!isEmpty_2()) {
		
		// 第一步(判斷m_data_2不是空,直接將m_data_2的頂端數據倒出)
		return Top_2();
	}
	else{

		// 第二步(判斷m_data_2是空,直接將m_data_1的頂端數據全部數據倒出到m_data_2裏,然後將m_data_2的頂端數據倒出)
		while ( !isEmpty() ){
			push_2(Top());
		}

		return Top_2();
	}
}

int MyQueue::max(){
	if (!isEmpty()) {
		std::sort(m_Data, m_Data + m_Top + 1, std::greater<int>());
		return m_Data[0];
	}
}

int MyQueue::min() {
	if (!isEmpty()) {
		std::sort(m_Data, m_Data + m_Top + 1, std::less<int>());
		return m_Data[0];
	}
}

int MyQueue::max1() {
	if (!isEmpty()) {
		m_MAX = m_Data[0];
		for ( size_t i = 0; i <= m_Top; i++ ){
			if (m_Data[i] > m_MAX) {
				m_MAX = m_Data[i];
			}
		}

		return m_MAX;
	}

	return 0x1000000;
}

void MyQueue::QueueOrder( QueueSort Strategy ) {
	switch (Strategy){
			case QueueSort::greater:	
				std::sort(m_Data, m_Data + m_Top + 1, std::greater<int>());
			case QueueSort::less:
				std::sort(m_Data, m_Data + m_Top + 1, std::less<int>());
			default:
				break;
	}
}

int MyQueue::min1() {
	if (!isEmpty()) {
		m_Min = m_Data[0];
		for ( size_t i = 0; i <= m_Top; i++ ) {
			if (m_Data[i] < m_Min) {
				m_MAX = m_Data[i];
			}
		}
	}

	return 0x1000000;
}

MyQueue::~MyQueue(){

}

int main() {

	MyQueue Q(3);
	Q.push(1);
	Q.push(2);
	Q.push(3);

	std::cout << Q.Tops() << std::endl;
	std::cin.get();
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章