跳到正文

算法·理论

栈模板

Hungry_STS约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年4月27日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 2 分钟

版本

  1. v1当前

本人蒟蒻,模仿STL容器,有错请指正

C++
struct stackbyhand {
	int sizeee=100000;//栈大小默认为100000
	#define hhhhhhhhh int//默认为int 
	void define(string s,int lxn) {//定义或临时改变(建议程序过程中不要进行改变,否则可能会出现一些奇奇怪怪的错误,WA或RE)栈的类型 及长度
		if(s=="int") {
			#define hhhhhhhhh int
		}
		if(s=="long long") {
			#define hhhhhhhhh long long
		}
		if(s=="unsigned long long") {
			#define hhhhhhhhh unsigned long long
		}
		if(s=="bool") {
			#define hhhhhhhhh bool
		}
		if(s=="char") {
			#define hhhhhhhhh char
		}
      sizeee=lxn;
	}
	hhhhhhhhh a[sizeee],toop=-1;
	hhhhhhhhh top() {//栈顶元素 
		if(toop!=-1)return a[toop];
	}
	bool empty() {//是否为空,空返回1,否则为0 
		return toop==-1;
	}
	bool full() {//是否为满,满返回1,否则为0 
		return toop==sizeee-1;
	}
	void push(hhhhhhhhh xxxx) {//压栈 
		toop++;
		a[toop]=xxxx;
	}
	void pop() {//出栈 
		if(toop>-1) {
			a[toop]=0;
			toop--;
		}
	}
	int size() {//栈的长度 
		return toop+1;
	}
	void clear() {//清栈 
		toop=-1;
	}
};

注意:
void define(string s,int lxn) 传参为 string ,要加双引号,如:define(int,100); 会报错,应写 define("int",100); 。
还有,在此模板 hhhhhhhhh 和 lxn 为关键字,不要使用。

原文
存档评论7 条抓取于