栈 队列 优先级队列 模拟实现(C++)

栈 队列 优先级队列模拟实现

栈 的模拟实现

常用接口 push pop front size empty
栈遵循后进先出的原则
利用c++库中来实现相对比较简单
有三种方式可以模拟实现栈的过程 1.vector 2.list 3.deque

#include <queue>
#include <vector>
#include <list>
#include <iostream>
using namespace std;

template <class T,class Container = queue<T>> //后面这块是栈里面的一个容器
class Stack {

public:
	void push(const T& val) {
		_c.push_back(val);
	}
	void pop() {
		_c.pop_back();
	}
	const T& top() { //一般不修改栈顶元素,因此返回值为const
		return _c.back();
	}
	size_t size() const {
		return _c.size();
	}
	bool empty() const {
		return _c.empty();
	}
private:
	Container _c;
};
void  test1() {
	//Stack<int> st;
	//Stack<int,list<int>> st;
   //Stack<int ,vector<int>> st;
	Stack<int,deque<int>> st;

	cout << "入栈元素: ";
	st.push(1);
	cout << st.top() << " ";
	st.push(2);
	cout << st.top() << " ";
	st.push(3);
	cout << st.top() << " ";
	st.push(4);
	cout << st.top() << " ";
	cout << endl;
	cout << "出栈元素: ";
	while ( !st.empty()) {
		cout << st.top() << " ";//获取栈顶元素
		st.pop();// 打印完栈顶元素后要出栈
	}
	cout << endl;
}
int main() {
	test1();
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
根据入栈出栈结果比对: 符合栈的后入先出原则

队列 的模拟实现

常用接口 push pop front size empty
vector 不支持实现队列,因为vector不支持头删
满足栈遵循先入先出的原则

#include <queue>
#include <vector>
#include <list>
#include <iostream>
using namespace std;

template <class T, class Container = deque<T>> //后面这块是队列里面的一个容器
class Queue{
public:

	void push(const T& val) {
		_c.push_back(val);
	}
	void pop() {
		_c.pop_front();
	}
	const T& front() { //一般不修改栈顶元素,因此返回值为const
		return _c.front();
	}
	const T& back() { //一般不修改栈顶元素,因此返回值为const
		return _c.back();
	}
	size_t size() const {
		return _c.size();
	}
	bool empty() const {
		return _c.empty();
	}
private:
	Container _c;

};
void  test2() {
	//Queue<int> q;
	Queue<int,list<int>> q;
	//vector 不能实现 
	cout << "入队: ";
	q.push(1);
	cout << q.front() << " ";
	q.pop();
	q.push(2);
	cout << q.front() << " ";
	q.pop();
	q.push(3);
	cout << q.front() << " ";
	q.pop();
	q.push(4);
	cout << q.front() << " ";
	q.pop();
	cout << endl;

	cout << "出队: ";
	q.push(1);
	q.push(2);
	q.push(3);
	q.push(4);
	while (!q.empty()) {
		cout << q.front() << " ";
		q.pop();
	}
	cout << endl;
}
int main() {
	test2();
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
满足队列先入先出的原则

priority_queue 的模拟实现

priority_queue 的底层结构就是堆,因此只需要对堆进行底层封装即可

** 通过堆实现,默认为大堆**

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

void test1() {
	priority_queue<int>pq;

	pq.push(1);
	pq.push(2);
	pq.push(3);
	pq.push(4);
	pq.push(5);
	pq.push(6);
	while (!pq.empty()) {
		cout << pq.top() << " ";
		pq.pop();
	}
}

int main() {
	test1();
	system( "pause");
	return 0;
}

运行结果:
在这里插入图片描述
运行结果可以明显看出来属于大堆
建的是大堆,如果想要建小堆的话,需要在向下调整和向下调整中的(_c[parent] < _c[child])进行修改 即可其他地方不动,那么输出结果就变为了建小堆

#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

template <class T, class Container = vector<T>>
class Priority_Queue {
public:
	void push(const T& val) {
		_c.push_back(val);
		shiftUp(_c.size() - 1);
	}

	void pop() {
		swap(_c[0], _c[_c.size() - 1]);
		_c.pop_back();
		shiftDown(0);
	}

	T& top() {
		return _c.front();
	}

	size_t size() const {
		return _c.size();
	}

	bool empty() const {
		return _c.empty();
	}

private:
	void shiftDown(int parent) {
		int child = 2 * parent + 1;
		while (child < _c.size()) {
			if (child + 1 < _c.size() && _c[child] < _c[child + 1])
				++child;
			if (_c[parent] < _c[child]) {
			
				swap(_c[parent], _c[child]);
				parent = child;
				child = 2 * parent + 1;
			}
			else
				break;
		}
	}

	void shiftUp(int child) {
		int parent = (child - 1) / 2;
		while (child > 0) {
			if (_c[parent] < _c[child]){
			
				swap(_c[parent], _c[child]);
				child = parent;
				parent = (child - 1) / 2;
			}
			else
				break;
		}
	}
private:
	Container _c;
	
};

void test2() {

	Priority_Queue<int, vector<int>> pq;
	int n = 200;
	for (int i = 0; i < n; i++) {
		pq.push(i);
	}

	while (!pq.empty()) {
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;
}                                                                   

int main() {
	test2();
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述

自定义类型输出大堆还是小堆利用模板参数进行确定

自定义类型中支持">" “<” 重载,介入支持小堆实现必须有圆括号运算符重载

#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

template <class T>
//圆括号运算符重载
struct Greater {
	bool operator()(const T& val1, const T& val2) {
		return val1 > val2;
	}
};
template <class T>
struct Less {
	bool operator()(const T& val1, const T& val2)
	{
		return val1 < val2;
	}
};

class Date {
public:
	Date(int y, int m, int d)
		:_y(y)
		,
		_m(m)
		,
		_d(d)
	{}

	bool operator > (const Date& d) const {

		if (_y > d._y)
			return true;
		else if (_y == d._y) {
			if (_m > d._m)
				return true;
			else if (_m == d._m) {
				if (_d > d._d)
					return true;
			}
		}
		return false;

	}
	bool operator < (const Date& d) const {

		if (_y < d._y)
			return true;
		else if (_y == d._y) {
			if (_m < d._m)
				return true;
			else if (_y == d._y && _m == d._m) { 
				if (_d < d._d)
					return true;
			}
		}
		return false;
	}


public:
	int _y;
	int _m;
	int _d;

};

ostream& operator<<(ostream& cout, const Date& d){

	cout << d._y << "-" << d._m << "-" << d._d << endl;
	return cout;

}

template <class T, class Container = vector<T>, class Compare = Less<T>>

class Priority_Queue {
public:
	void push(const T& val) {
		_c.push_back(val);
		shiftUp(_c.size() - 1);
	}
	void pop() {
		swap(_c[0], _c[_c.size() - 1]);
		_c.pop_back();
		shiftDown(0);
	}

	T& top() {
		return _c.front();
	}

	size_t size() const {
		return _c.size();
	}

	bool empty() const {
		return _c.empty();
	}
private:
	void shiftDown(int parent) {
		int child = 2 * parent + 1;
		while (child < _c.size()) {
			if (child + 1 < _c.size() && _c[child] > _c[child + 1])
				++child;
			if (_c[parent] > _c[child]) {

				swap(_c[parent], _c[child]);
				parent = child;
				child = 2 * parent + 1;
			}
			else
				break;
		}
	}

	void shiftUp(int child) {
		int parent = (child - 1) / 2;
		while (child > 0) {
			if (_c[parent] > _c[child]){

				swap(_c[parent], _c[child]);
				child = parent;
				parent = (child - 1) / 2;
			}
			else
				break;
		}
	}
private:
	Container _c;
	Compare _cmp;
};

void test() {
	Priority_Queue<Date, deque<Date>, Less<Date>> pq;
	pq.push(Date(2020, 5, 31));
	pq.push(Date(2022, 5, 31));
	pq.push(Date(2008, 5, 31));
	pq.push(Date(2025, 5, 31));
	pq.push(Date(2020, 12, 31));
	pq.push(Date(2020, 5, 3));

	while (!pq.empty()) {
		cout << pq.top();
		pq.pop();
	}

}
int main() {
	
	test();
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值