0% found this document useful (0 votes)
32 views6 pages

Data Structure Lab: Postfix Evaluation

This document contains the summaries of 3 coding problems from a data structures lab: 1. Postfix expression evaluation: The problem evaluates a postfix expression using a stack. A function is defined to evaluate the expression by iterating through it and using a stack to pop and perform operations. 2. Infix to postfix conversion: The problem converts an infix expression to postfix notation. It uses a stack and operators' precedence to convert the expression properly by pushing and popping from the stack. 3. Longest valid parentheses: The problem finds the length of the longest valid parentheses substring in a string. It uses two stacks, one for characters and one for indexes, to keep track of parentheses and find the maximum length between

Uploaded by

jihadhossain101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

Data Structure Lab: Postfix Evaluation

This document contains the summaries of 3 coding problems from a data structures lab: 1. Postfix expression evaluation: The problem evaluates a postfix expression using a stack. A function is defined to evaluate the expression by iterating through it and using a stack to pop and perform operations. 2. Infix to postfix conversion: The problem converts an infix expression to postfix notation. It uses a stack and operators' precedence to convert the expression properly by pushing and popping from the stack. 3. Longest valid parentheses: The problem finds the length of the longest valid parentheses substring in a string. It uses two stacks, one for characters and one for indexes, to keep track of parentheses and find the maximum length between

Uploaded by

jihadhossain101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like