CSE 212- Data Structure Lab || Lab -06
Name: Jihad H0ssain Jisan;
ID:231016712
Faculty : Md. Jahidul Hasan Jahid
Table of Contents
Problem Statement : Postfix Expression Evaluation : ............................................................................................................................................2
Problem statement : Infix to Postfix ......................................................................................................................................................................... 3
Problem statement : Longest Valid Parentheses .................................................................................................................................................... 4
PAGE 1
Problem Statement : Postfix Expression Evaluation :
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int evaluatePostfix(const string& expression) {
stack<int> s;
string number;
for (int i = 0; i < [Link](); i++) {
if (expression[i] == ' ') {
if (![Link]()) {
[Link](stoi(number));
[Link]();
}
continue;
}
if (isdigit(expression[i]) || (expression[i] == '-' && isdigit(expression[i+1]))) {
number += expression[i];
} else {
int val2 = [Link]();
[Link]();
int val1 = [Link]();
[Link]();
switch (expression[i]) {
case '+': [Link](val1 + val2); break;
case '-': [Link](val1 - val2); break;
case '*': [Link](val1 * val2); break;
case '/': [Link](val1 / val2); break;
}
}
}
return [Link]();
}
int main() {
int N;
cout<<"number of expression"<<" : ";
cin >> N;
[Link]();
for (int i = 0; i < N; ++i) {
string expression;
getline(cin, expression);
cout << "Result of test case " << i+1 << ": " << evaluatePostfix(expression) << endl;
}
return 0;
}
PAGE 2
PROBLEM STATEMENT : INFIX TO POSTFIX
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int prec(char c) {
if(c == '^')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
void infixToPostfix(string s)
{
stack<char> st;
string result;
for (int i = 0; i < [Link](); i++) {
char c = s[i];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
result += c;
else if (c == '(')
[Link]('(');
else if (c == ')') {
while ([Link]() != '(') {
result += [Link]();
[Link]();
}
[Link]();
}
else {
while (![Link]() && prec(c) <= prec([Link]())) {
result += [Link]();
[Link]();
}
[Link](c);
}
}
while (![Link]()) {
result += [Link]();
[Link]();
}
cout << result << endl;
}
int main()
{
PAGE 3
cout<<"enter expression"<<" : ";
string exp;
getline(cin,exp);
infixToPostfix(exp);
return 0;
}
Problem statement : Longest Valid Parentheses
#include <iostream>
#include <stack>
using namespace std;
class Solution {
public:
int longestValidParentheses(string s) {
stack<char> ct;
stack<int> index;
[Link](-1);
int max_length = 0;
for (int i = 0; i < [Link](); ++i) {
if (s[i] == '(') {
[Link]('(');
[Link](i);
} else {
if (![Link]() && [Link]() == '(') {
[Link]();
[Link]();
int current_length = i - [Link]();
if (current_length > max_length) {
max_length = current_length;
}
} else {
[Link](')');
[Link](i);
}
}
}
return max_length;
}
};
PAGE 4
int main() {
Solution solution;
string input = "(()())())()";
cout << "Length of the longest valid parentheses substring: " << [Link](input) <<
endl;
return 0;
}
PAGE 5