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;
};
相关推荐
小南知更鸟几秒前
【2026 年 Python 包管理工具选型:从“工具崇拜”回到工程约束】
开发语言·python
努力努力再努力wz22 分钟前
【Lua入门系列】从动态类型到变量作用域:彻底理解 local、全局变量与跨文件访问
开发语言·junit·lua
程序猿乐锅28 分钟前
面向对象练习:图书借阅系统里的封装、抽象类与多态
开发语言·前端·数据库·后端·python·学习·1024程序员节
lunar_coder35 分钟前
哈希扩展 -- 位图思想
c++·算法·哈希算法
沐晓时光1 小时前
C语言入门,深入理解指针(6)
c语言·开发语言
略略略咯咯1 小时前
33333
开发语言·windows·python
垆边人似月.1 小时前
第三篇:函数、文件与异常处理 / Functions, Files & Exception Handling
开发语言·python
云和数据.ChenGuang1 小时前
项目打开后没有主类
开发语言·人工智能·langchain·1024程序员节
鬼手点金1 小时前
缓存作为架构约束
开发语言·前端·人工智能·python·microsoft·缓存·架构
SEO_juper1 小时前
Node.js 页面主题聚焦度检测:title 和正文各说各的,AI 也读不懂
开发语言·人工智能·node.js·seo·外贸独立站