stack和queue

文章目录

适配器模式

适配器是一种设计模式(设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结),该种模式是将一个类的接口转换成客户希望的另外一个接口

stack的模拟实现

cpp 复制代码
template<class T, class Container = vector<T>>
class stack
{
	Container _con;
public:
	stack() {}
	~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 _con.back(); }
	size_t size() const { return _con.size(); }
	bool empty() const { return _con.empty(); }
};
void test_stack()
{
	stack<int> st;
	st.push(1);
	st.push(2);
	st.push(3);
	st.push(4);
	while (!st.empty())
	{
		cout << st.top() << " ";
		st.pop();
	}
	cout << endl;
}

queue的模拟实现

cpp 复制代码
template<class T, class Container = deque<T>>
class queue
{
	Container _con;
public:
	queue() {}
	~queue() {}
	bool empty() const { return _con.empty(); }
	size_t size() const { return _con.size(); }
	T& front() { return _con.front(); }
	const T& front() const { return _con.front(); }
	T& back() { return _con.back(); }
	const T& back() const { return _con.back(); }
	void push(const T& x) { _con.push_back(x); }
	void pop() { _con.pop_front(); }
};
void test_queue()
{
	queue<int> q;
	q.push(1);
	q.push(2);
	q.push(3);
	q.push(4);
	while (!q.empty())
	{
		cout << q.front() << ' ';
		q.pop();
	}
	cout << endl;
}

优先级队列

cpp 复制代码
template<class T>
struct less{
	bool operator()(const T& x, const T& y)
	{ return x < y; }
};
template<class T>
struct greater{
	bool operator()(const T& x, const T& y)
	{ return x > y; }
};
template<class T, class Container=vector<T>, class Compare=less<T>>
class priority_queue
{
public:
	priority_queue() {}
	void push(const T& x)
	{
		_con.push_back(x);
		AdjustUp(_con.size() - 1);
	}
	const T& top() const
	{
		return _con.front();
	}
	bool empty() const 
	{
		return _con.empty();
	}
	void pop()
	{
		assert(!_con.empty());
		swap(_con[0], _con[_con.size() - 1]);
		_con.pop_back();
		AdjustDown(0);
	}
private:
	// 向上调整
	void AdjustUp(int child)
	{
		Compare com;	// 仿函数
		int parent = (child - 1) / 2;
		while (child > 0)
		{
			if (com(_con[parent], _con[child]))
			{
				swap(_con[parent], _con[child]);
				child = parent;
				parent = (child - 1) / 2;
			}
			else break;
		}
	}
	// 向下调整
	void AdjustDown(int parent)
	{
		Compare com;	// 仿函数
		int child = 2 * parent + 1;
		while (child < _con.size())
		{
			if (child + 1 < _con.size() && com(_con[child], _con[child+1]))
				child = child + 1;
			if (com(_con[parent], _con[child]))
			{
				swap(_con[parent], _con[child]);
				parent = child;
				child = 2 * parent + 1;
			}
			else break;
		}
	}
	Container _con;
};


			{
				swap(_con[parent], _con[child]);
				parent = child;
				child = 2 * parent + 1;
			}
			else break;
		}
	}
	Container _con;
};
相关推荐
Dream it possible!3 分钟前
LeetCode 面试经典 150_图的广度优先搜索_最小基因变化(93_433_C++_中等)(广度优先搜索(BFS))
c++·leetcode·面试·广度优先
tuokuac8 分钟前
java中的浮点数基本操作
java·开发语言
他是龙55138 分钟前
第29天:安全开发-JS应用&DOM树&加密编码库&断点调试&逆向分析&元素属性操作
开发语言·javascript·安全
小菱形_1 小时前
【C#】LINQ
开发语言·c#·linq
爱吃大芒果1 小时前
Flutter 基础组件详解:Text、Image、Button 使用技巧
开发语言·javascript·flutter·华为·ecmascript·harmonyos
Hominid⁺1 小时前
深度解析:C 语言的 8 个翻译阶段与 GCC 编译全流程
c语言·开发语言
steins_甲乙1 小时前
C++并发编程
开发语言·c++
曹牧1 小时前
C#:foreach
开发语言·c#
计算机学姐1 小时前
基于Python的商场停车管理系统【2026最新】
开发语言·vue.js·后端·python·mysql·django·flask
小猪快跑爱摄影1 小时前
【AutoCad 2025】【Python】零基础教程(一)——简单示例
开发语言·python