Daata Structure
Daata Structure
Submitted to:
Sir Ashar
Submitted by:
Ayesha Fayaz 22_Arid_4354
Khansa Rehman 22_Arid_4368
Areeba Noor 22_Arid_4353
Mahnoor Nadeem 22_Arid_4369
Course Title:
Data Structure and Algorithm
Submission Date:
15_11_2023
class PostfixToInfixConverter {
public:
string postfixToInfix(string postfix) {
stack<string> st;
if (isOperand(ch)) {
string operand(1, ch);
st.push(operand);
} else if (isOperator(ch)) {
string operand2 = st.top();
st.pop();
string operand1 = st.top();
st.pop();
return st.top();
}
bool isOperand(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
};
int main() {
PostfixToInfixConverter converter;
string postfix;
cout << "Enter the postfix expression: ";
cin >> postfix;
return 0;
}
OUTPUT
Enter the postfix expression: ab+
Infix Expression: (a+b)
• Prefix to infix
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c ==
'/');
}
if (isOperator(current)) {
// Pop two operands from the stack
string operand1 = stack.top();
stack.pop();
int main() {
string prefixExpression = "*+AB-CD";
string infixExpression =
prefixToInfix(prefixExpression);
• POSTFIX TO PREFIX
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c ==
'/');
}
string postfixToPrefix(const string& postfix) {
stack<string> stack;
int main() {
string postfixExpression = "AB+C*";
string prefixExpression =
postfixToPrefix(postfixExpression);
cout << "Postfix Expression: " <<
postfixExpression << endl;
cout << "Prefix Expression: " <<
prefixExpression << endl;
return 0;
}
OUTPUT:
Postfix Expression: AB+C*
Prefix Expression: *+ABC
• PREVIX TO POSTFIX
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c ==
'/');
}
if (isOperator(current)) {
// Pop two operands from the stack
string operand1 = stack.top();
stack.pop();
string operand2 = stack.top();
stack.pop();
string postfixExpression =
prefixToPostfix(prefixExpression);
return 0;
}
OUTPUT
Prefix Expression: *+AB-CD
Postfix Expression: AB+CD-*