【priority_queue、stack、queue模拟实现】

1、priority_queue模拟实现

(1)priority_queue.h

#pragma once
#include<vector>

namespace pz
{
	template<class T,class Container = vector<T>,class Compare = less<T>>
	class priority_queue
	{
	public:
		void AdjustUp(int child)//向上调整,大堆
		{
			Compare com;
			int parent = (child - 1) / 2;
			while (child>0)
			{
				//if (_con[child] > _con[parent]);
				if (com(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}
		void AdjustDdown(int root)//向下调整 
		{
	
			int parent = root;
			int child = parent * 2 + 1;
			Compare com;

			while (child<_con.size())
			{
				//if (child + 1 < _con.size() && _con[child + 1] > _con[child])
				if (child + 1 < _con.size() && com(_con[child],_con[child+1]))
				{
					++child;
				}
				if (com(_con[parent], _con[child]))
				{
					swap(_con[child], _con[parent]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}


		void push(const T& x)
		{
			_con.push_back(x);
			AdjustUp(_con.size() - 1);//向上调整算法

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

			AdjustDown(0);
		}
		T& top()
		{
			return _con[0];
		}
		size_t size()
		{
			return _con.size();
		}
		bool empty()
		{
			return _con.empty();
		}
	private:
		Container _con;

	};
	void test()
	{
		priority_queue<int>a;
		a.push(1);
		a.push(2);
		a.push(3);
		a.push(4);
		a.push(5);
		while (!a.empty())
		{
			cout << a.top() << " ";
			a.pop();
		}
		cout << endl;
	}
}

(2)main.cpp

#include<iostream>
#include"priority_queue.h"
using namespace std;

namespace pz
{
	template<class T>//仿函数
	struct less//小于号  大堆
	{
		bool operator()(const T & x1, const T& x2)
		{
			return x1 < x2;
		}
	};

	template<class T>
	struct greater//大于号  小堆
	{
		bool operator()(const T& x1, const T& x2)
		{
			return x1>x2;
		}
	};
}




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

仿函数:

在这里插入图片描述

又叫它函数对象,因为它的对象可以像函数一样使用
在这里插入图片描述

2、stack模拟实现

#include<iostream>
#include<deque>


template <class T, class Con = std::deque<T>>
class Stack
{
public:
	Stack() {}

	void push(const T&x)
	{
		_con.push_back(x);
	}

	void pop()
	{
		_con.pop_back();
	}

	T& Top()
	{
		return _con.back();
	}

	const T& Top()const
	{
		return _c.back();
	}
	size_t Size()
	{
		return _con.size();
	}

	bool empty()
	{
		return _con.empty();
	}
private:
	Con _con;
};

void test()
{
	Stack<int,std::deque<int>>a;
	a.push(1);
	a.push(2);
	a.push(3);
	a.push(4);

	std::cout << a.Size() << std::endl;
	std::cout << a.Top() << std::endl;

	a.pop();
	a.pop();

	std::cout << a.Size() << std::endl;
	std::cout << a.Top() << std::endl;
}

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

3、queue模拟实现

#include<deque>
#include<iostream>

template <class T, class Con = std::deque<T>>
class queue
{
public:
	queue(){}
	void push_back(const T& x)
	{
		 _con.push_back(x);
	}
	void pop_front()
	{
		 _con.pop_front();
	}
	T& back()
	{
		return _con.back();
	}
	T& front()
	{
		return _con.front();
	}
	const T& back()const 
	{
		return _con.back();
	}
	const T& front()const
	{
		return _con.front();
	}
	size_t Size()
	{
		return _con.size();
	}
	bool empty()
	{
		return _con.empty();
	}
private:
	Con _con;
};


void test1()
{
	queue<int>a;
	a.push_back(1);
	a.push_back(2);
	a.push_back(3);
	std::cout << a.Size() << std::endl;
	std::cout << a.front() << std::endl;
	std::cout << a.back() << std::endl;

	a.pop_front();
	std::cout << a.Size() << std::endl;
	std::cout << a.front() << std::endl;
	std::cout << a.back() << std::endl;
}

int main()
{
	test1();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值