数据结构 栈和队列的应用

news/2024/11/9 2:44:56/

在昨天分享了有关栈和队列的基础知识和基本操作后,今天来分享一些有关栈和队列的应用

栈和队列的应用

  1. 删除字符串中的所有相邻重复项
#include <iostream>
#include <stack>
using namespace std;
string remove(string S)
{stack<char> charStack;for (int i = 0; i < S.length(); ++i){char c=S[i];if (!charStack.empty() && charStack.top() == c){charStack.pop();}else{charStack.push(c);}}string result;while (!charStack.empty()){result = charStack.top() + result;charStack.pop();}return result;
}
int main()
{string in;cin >> in;string result = remove(in);cout<<result;return 0;
}
  1. 括号匹配问题(类比20.LeetCode 有效的括号)
#include <iostream>
#include <stack>
#include <string>using namespace std;bool match(string input)
{stack<char> s;for (int i = 0; i < input.length(); i++){char bracket = input[i];if (bracket == '(' || bracket == '{' || bracket == '['){s.push(bracket);}else if (bracket == ')' || bracket == '}' || bracket == ']'){if (s.empty()){return false;}char top = s.top();s.pop();if ((bracket == ')' && top != '(') || (bracket == '}' && top != '{') || (bracket == ']' && top != '[')){return false;}}}return s.empty();
}int main()
{string input;cin >> input;if (match(input)){cout << "yes" << endl;}else{cout << "no" << endl;}return 0;
}
  1. 计算后缀表达式
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <cstdlib>using namespace std;int customAtoi(const string &str)
{int result = 0;int sign = 1;size_t i = 0;if (str[0] == '-'){sign = -1;i = 1;}for (; i < str.length(); i++){if (isdigit(str[i])){result = result * 10 + (str[i] - '0');}else{cerr << "Error: Invalid character in input: " << str[i] << endl;std::exit(1);}}return result * sign;
}bool isOperator(const string &token)
{return (token == "+" || token == "-" || token == "*");
}int evaluatePostfixExpression()
{stack<int> operands;string token;while (cin >> token){if (token == "#"){break;}if (!isOperator(token)){operands.push(customAtoi(token));}else{int operand2 = operands.top();operands.pop();int operand1 = operands.top();operands.pop();int result;if (token == "+"){result = operand1 + operand2;}else if (token == "-"){result = operand1 - operand2;}else if (token == "*"){result = operand1 * operand2;}operands.push(result);}}if (!operands.empty()){return operands.top();}cerr << "Error: Invalid expression." << endl;std::exit(1);
}int main()
{int result = evaluatePostfixExpression();cout << result << endl;return 0;
}
  1. 表达式求值
#include <iostream>
#include <stack>
#include <string>
#include <cctype>using namespace std;int precedence(char op)
{if (op == '+' || op == '-'){return 1;}else if (op == '*' || op == '/'){return 2;}return 0;
}int evaluateExpression(const string &expression)
{stack<int> values;stack<char> operators;for (size_t i = 0; i < expression.length(); i++){if (isspace(expression[i])){continue;}else if (isdigit(expression[i])){int num = 0;while (i < expression.length() && isdigit(expression[i])){num = num * 10 + (expression[i] - '0');i++;}i--;values.push(num);}else if (expression[i] == '('){operators.push(expression[i]);}else if (expression[i] == ')'){while (!operators.empty() && operators.top() != '('){char op = operators.top();operators.pop();int operand2 = values.top();values.pop();int operand1 = values.top();values.pop();if (op == '+'){values.push(operand1 + operand2);}else if (op == '-'){values.push(operand1 - operand2);}else if (op == '*'){values.push(operand1 * operand2);}else if (op == '/'){values.push(operand1 / operand2);}}operators.pop(); }else if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*' || expression[i] == '/'){while (!operators.empty() && operators.top() != '(' && precedence(operators.top()) >= precedence(expression[i])){char op = operators.top();operators.pop();int operand2 = values.top();values.pop();int operand1 = values.top();values.pop();if (op == '+'){values.push(operand1 + operand2);}else if (op == '-'){values.push(operand1 - operand2);}else if (op == '*'){values.push(operand1 * operand2);}else if (op == '/'){values.push(operand1 / operand2);}}operators.push(expression[i]);}}while (!operators.empty()){char op = operators.top();operators.pop();int operand2 = values.top();values.pop();int operand1 = values.top();values.pop();if (op == '+'){values.push(operand1 + operand2);}else if (op == '-'){values.push(operand1 - operand2);}else if (op == '*'){values.push(operand1 * operand2);}else if (op == '/'){values.push(operand1 / operand2);}}return values.top();
}int main()
{string expression;getline(cin, expression);int result = evaluateExpression(expression);cout << result << endl;return 0;
}
  1. 回文链表
#include <bits/stdc++.h>
using namespace std;
struct Node
{int data;Node *next;
};
void createList(Node *&h,int n)
{Node *p,*r;h=new Node;h->next=NULL;r=h;for(int i=1; i<=n; i++){p=new Node;cin>>p->data;r->next=p;r=p;}r->next=NULL;
}
void printList(Node *h)
{Node *p;p=h->next;while(p){cout<<p->data<<" ";p=p->next;}cout<<endl;
}bool isPalindrome(Node *head)
{if (!head || !head->next){return true;}Node *slow = head;Node *fast = head;while (fast && fast->next){slow = slow->next;fast = fast->next->next;}Node *prev = NULL;Node *curr = slow;while (curr){Node *next = curr->next;curr->next = prev;prev = curr;curr = next;}Node *left = head->next;Node *right = prev;while (left && right){if (left->data != right->data){return false;}left = left->next;right = right->next;}return true;
}int main()
{int n;Node *h;cin>>n;createList(h,n);cout<<(isPalindrome(h)?"yes":"no");return 0;
}

http://www.ppmy.cn/news/1241409.html

相关文章

嵌入式硬件基础知识——1

目录 SOC、MCU、MPU、CPU SPI STM32的时钟系统 can是什么 串口和并口 传感器输出引脚高阻抗好还是低阻抗好&#xff1f; iic 运算放大器特点 MOS管和三极管 同步电路和异步电路 SOC、MCU、MPU、CPU SOC 片上系统 手机的核心芯片 MCU 微控系统 单片机 MPU 嵌入式微处…

云计算领域的第三代浪潮!

根据IDC不久前公布的数据&#xff0c;2023年上半年中国公有云服务整体市场规模(IaaS/PaaS/SaaS)为190.1亿美元&#xff0c;阿里云IaaS、PaaS市场份额分别为29.9%和27.9%&#xff0c;都远超第二名&#xff0c;是无可置疑的行业领头羊。 随着人工智能&#xff08;AI&#xff09;…

物联网中基于信任的安全性调查研究:挑战与问题

A survey study on trust-based security in Internet of Things: Challenges and issues 文章目录 a b s t r a c t1. Introduction2. Related work3. IoT security from the one-stop dimension3.1. Output data related security3.1.1. Confidentiality3.1.2. Authenticity …

百战python04-循环结构

文章目录 趣味进度条:通过一个简单的进度条来进入循环的世界吧for-in循环语法内置函数range()练习:累和下面是使用for循环对字符串(第一个for)、range函数的循环取值示例for循环对字典、列表取值(后面会讲解字典,列表)while循环while循环实现猜数字小游戏结束循环的操…

超越噪音,让音乐重获新生:iZotope RX 10音频降噪修复软件

在音乐制作或者音频处理的过程中&#xff0c;噪音往往是一个让人头痛的问题。无论是环境噪音&#xff0c;还是设备产生的噪音&#xff0c;都会对音频质量产生重大影响。而现在&#xff0c;我们有了iZotope RX 10&#xff0c;这款专业的音频降噪修复软件&#xff0c;可以将你从噪…

掌握高效性能测试技能:JMeter基础入门!

一、JMeter基础 A、JMeter介绍 Apache JMeter是Apache组织开发的基于Java的压力测试工具。 Apache JMeter may be used to test performance both on static and dynamic resources (files, Servlets, Perl scripts, Java Objects, Data Bases and Queries, FTP Servers and …

C_4练习题

一、单项选择题&#xff08;本大题共20小题&#xff0c;每小题2分&#xff0c;共40分。在每小题给出的四个备选项中选出一个正确的答案,并将所选项前的字母填写在答题纸的相应位置上。) 定义如下变量和数组&#xff1a; int i; int x[3][3]{1,2,3,4,5,6,7,8,9}; 则下面语句的输…

时间序列预测实战(十九)魔改Informer模型进行滚动长期预测(科研版本)

论文地址->Informer论文地址PDF点击即可阅读 代码地址-> 论文官方代码地址点击即可跳转下载GIthub链接 个人魔改版本地址-> 文章末尾 一、本文介绍 在之前的文章中我们已经讲过Informer模型了&#xff0c;但是呢官方的预测功能开发的很简陋只能设定固定长度去预测未…