<7> 队列的各种操作

队列和栈一样,也是相对比较常见的数据结构。队列遵守的是“先进先出”的原则,因此在一些数据先到先处理的场景,可能会用到。下边使用C/C++编写了队列的自定义实现,其中包括创建、出队列,入队列、取值等。

#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_front(0),
		m_rear(0),
		m_MAX(-100000),
		m_Min(100000)
	{
		m_VAPACITY = size;
		m_Data = (int*)malloc(sizeof(int)*m_VAPACITY);
	}

	~MyQueue();
private:
	int *m_Data;													//	存取值的数组
	int  m_front;													//  队列头
	int  m_rear;													//  队列尾
	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();													//  判断队列是否满了
	void deQueue();													//  出队列(删除队列元素)
	void enQueue(int value);										//  入队列
	const int Top();												//  取值
	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_rear == m_front;
}

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

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

void MyQueue::enQueue(int value) {
	try {
		if (!isFull()) {
			m_Data[m_rear] = value;
			m_rear++;
		}
		else {
			// realloc函数进行扩容 
			std::cout << "the Queue is full.Do you need to expand?" << std::endl;
			m_Data = (int*)realloc(m_Data, sizeof(int)*m_VAPACITY);
		}
	}
	catch (const std::exception& e) {
		std::cout << e.what() << std::endl;
	}
}

const int MyQueue::Top() {
	try {
		if (!isEmpty()) {
			int Temp = m_Data[m_front];
			m_front++;
			return Temp;
		}
		else {
			std::cout << "the Queue is empty." << std::endl;
		}
	}
	catch (const std::exception& e) {
		std::cout << e.what() << std::endl;
	}

	return 0x1000000;
}

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

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

int MyQueue::max1() {
	if (!isEmpty()) {
		m_MAX = m_Data[0];
		for (size_t i = m_front; i <= m_rear; 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_rear - m_front + 1), std::greater<int>());
	case QueueSort::less:
		std::sort(m_Data, m_Data + (m_rear - m_front + 1), std::less<int>());
	default:
		break;
	}
}

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

	return 0x1000000;
}

MyQueue::~MyQueue() {

}

int main() {

	// 
	//
	//
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值