算法7.链式栈

算法7.链式栈

cpp 复制代码
// 07_链式栈.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <stack>
using namespace std;

// 比较符号优先级的
bool Priority(char ch, char topch)
{
	if ((ch == '*' || ch == '/') && (topch == '+' || topch == '-'))
		return true;
	if (topch == '(' && ch != ')')
		return true;
	return false;
}

// 中缀表达式 => 后缀表达式
string MiddleToEndExpr(string expr)
{
	string result;
	stack<char> s;

	for (char ch : expr)
	{
		if (ch >= '0' && ch <= '9')
		{
			result.push_back(ch);
		}
		else
		{
			for (;;)
			{
				// 处理符号了
				if (s.empty() || ch == '(')
				{
					s.push(ch);
					break;
				}

				// 比较当前符号ch和栈顶符号top的优先级
				char topch = s.top();
				// Priority:true ch > topch   false ch <= topch
				if (Priority(ch, topch))
				{
					s.push(ch);
					break;
				}
				else
				{
					s.pop();
					if (topch == '(') // 如果遇见),一直出栈,直到(
						break;
					result.push_back(topch);
				}
			}
		}
	}

	// 如果符号栈还存留符号,直接输出到后缀表达式里面     + /
	while (!s.empty())
	{
		result.push_back(s.top());
		s.pop();
	}

	return result;
}

int main()
{
	cout << MiddleToEndExpr("(1+2)*(3+4)") << endl;
	cout << MiddleToEndExpr("2+(4+6)/2+6/3") << endl;
	cout << MiddleToEndExpr("2+6/(4-2)+(4+6)/2") << endl;
}

#if 0
// 链式栈
class LinkStack
{
public:
	LinkStack(): size_(0)
	{
		head_ = new Node;
	}
	~LinkStack()
	{
		Node* p = head_;
		while (p != nullptr)
		{
			head_ = head_->next_;
			delete p;
			p = head_;
		}
	}

public:
	// 入栈 O(1)   把链表头节点后面,第一个有效节点的位置,当作栈顶位置
	void push(int val)
	{
		// head_ -> 1
		// head_ -> 2 -> 1
		Node* node = new Node(val);
		node->next_ = head_->next_;
		head_->next_ = node;
		size_++;
	}
	// 出栈 O(1)
	void pop()
	{
		if (head_->next_ == nullptr)
			throw "stack is empty!";
		Node* p = head_->next_;
		head_->next_ = p->next_;
		delete p;
		size_--;
	}
	// 获取栈顶元素
	int top() const
	{
		if (head_->next_ == nullptr)
			throw "stack is empty!";
		return head_->next_->data_;
	}
	// 判空
	bool empty() const
	{
		return head_->next_ == nullptr;
	}
	// 返回栈元素个数   遍历一遍链表,记录节点个数O(n)    想达到O(1)
	int size() const
	{
		return size_;
	}

private:
	struct Node
	{
		Node(int data = 0) : data_(data), next_(nullptr) {}
		int data_;
		Node* next_;
	};

	Node* head_;
	int size_;
};

int main()
{
	int arr[] = { 12,4,56,7,89,31,53,75 };
	LinkStack s;

	for (int v : arr)
	{
		s.push(v);
	}

	cout << s.size() << endl;

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

	cout << s.size() << endl;
}
#endif
相关推荐
啊嘞嘞?1 小时前
力扣(回文链表)
算法·leetcode·链表
Awh-1 小时前
数据结构 第六章:哈希存储
数据结构·算法·哈希算法
M78佐菲1 小时前
Linux学习笔记:目录IO与帧缓冲
linux·笔记·学习·算法
玫瑰互动GEO1 小时前
抖音SEO优化技术拆解:搜索排名四大因子与4步落地算法分析
人工智能·算法·搜索引擎·语音识别
Interview Aid1122 小时前
Akuna OA 题目拆解|三题思路复盘
linux·运维·算法·面试·职场和发展
wabs6662 小时前
关于栈【力扣150.逆波兰表达式求值的思考】
数据结构·c++·算法·leetcode··代码随想录
ZGi.ai2 小时前
ZGI 父子分块:连接检索片段与完整上下文
人工智能·算法·知识库·企业ai·zgi·父子分块
IT毕设实战小研2 小时前
基于大数据的二手车数据分析与预测
java·大数据·后端·爬虫·python·算法·课程设计
Allen_LVyingbo2 小时前
医疗AI可扩展网格信息系统中网格计算技术的智能优化
大数据·人工智能·python·算法·机器学习·django·健康医疗