0% found this document useful (0 votes)
2 views19 pages

Lect-2 Stack Convertion Program

Uploaded by

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

Lect-2 Stack Convertion Program

Uploaded by

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

1.

Write a Java Program for Evaluation of given infix expression

import [Link];

public class InfixEvaluation {

public static int precedence(char operator) {

switch (operator) {

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

default:

return -1;

public static int applyOperation(char operator, int b, int a) {

switch (operator) {

case '+':

return a + b;

case '-':

return a - b;

case '*':

return a * b;

case '/':

if (b == 0) {

throw new UnsupportedOperationException("Cannot divide by zero");


}

return a / b;

return 0;

public static int evaluate(String expression) {

Stack<Integer> operands = new Stack<>();

Stack<Character> operators = new Stack<>();

for (int i = 0; i < [Link](); i++) {

char currentChar = [Link](i);

if ([Link](currentChar)) {

continue;

if ([Link](currentChar)) {

StringBuilder buffer = new StringBuilder();

while (i < [Link]() && [Link]([Link](i))) {

[Link]([Link](i++));

i--; // Adjust i since the loop increments it one extra time

[Link]([Link]([Link]()));

} else if (currentChar == '(') {

[Link](currentChar);

} else if (currentChar == ')') {

while ([Link]() != '(') {

[Link](applyOperation([Link](), [Link](), [Link]()));

}
[Link](); // Pop '('

} else if (currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/') {

while (![Link]() && precedence([Link]()) >= precedence(currentChar)) {

[Link](applyOperation([Link](), [Link](), [Link]()));

[Link](currentChar);

while (![Link]()) {

[Link](applyOperation([Link](), [Link](), [Link]()));

return [Link]();

public static void main(String[] args) {

String expression = "3 + 5 * (2 - 8 )";

[Link]("The result of the expression is: " + evaluate(expression));

}
2. Write a Java Program for Evaluation of given prefix expression

import [Link];

public class PrefixEvaluation {

public static int applyOperation(char operator, int operand1, int operand2) {

switch (operator) {

case '+':

return operand1 + operand2;

case '-':

return operand1 - operand2;

case '*':

return operand1 * operand2;

case '/':

if (operand2 == 0) {

throw new UnsupportedOperationException("Cannot divide by zero");

return operand1 / operand2;

return 0;

public static int evaluate(String expression) {

Stack<Integer> stack = new Stack<>();

// Scan the expression from right to left

for (int i = [Link]() - 1; i >= 0; i--) {

char currentChar = [Link](i);

if ([Link](currentChar)) {

continue;
}

if ([Link](currentChar)) {

StringBuilder buffer = new StringBuilder();

// Handle multi-digit numbers

while (i >= 0 && [Link]([Link](i))) {

[Link]([Link](i--));

i++; // Adjust i since the loop decrements it one extra time

[Link]([Link]([Link]().toString()));

} else if (currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/') {

int operand1 = [Link]();

int operand2 = [Link]();

int result = applyOperation(currentChar, operand1, operand2);

[Link](result);

return [Link]();

public static void main(String[] args) {

String expression = "- + * 2 3 * 5 4 9"; // Example: equivalent to ((2 * 3) + (5 * 4)) - 9

[Link]("The result of the prefix expression is: " + evaluate(expression));

}
3. Write a Java Program for Evaluation of given postfix expression

import [Link];

public class PostfixEvaluation {

// Method to evaluate a postfix expression

public static int evaluatePostfix(String expression) {

// Stack to store operands

Stack<Integer> stack = new Stack<>();

// Iterate over each character in the postfix expression

for (int i = 0; i < [Link](); i++) {

char ch = [Link](i);

// If the character is an operand, push it onto the stack

if ([Link](ch)) {

[Link](ch - '0'); // Convert character to integer

// If the character is an operator, pop two elements from the stack, perform the operation, and

push the result back

else {

int operand2 = [Link]();

int operand1 = [Link]();

int result = 0;

switch (ch) {

case '+':

result = operand1 + operand2;

break;

case '-':

result = operand1 - operand2;


break;

case '*':

result = operand1 * operand2;

break;

case '/':

result = operand1 / operand2;

break;

// Push the result onto the stack

[Link](result);

// The final result will be the only element left in the stack

return [Link]();

public static void main(String[] args) {

String expression = "231*+9-"; // Example postfix expression

int result = evaluatePostfix(expression);

[Link]("The result of evaluating the postfix expression \"" + expression + "\" is: " +
result);

}
4. Write a Java Program for conversion of given infix expression to prefix expression

import [Link];

public class InfixToPrefix {

// Method to check if a character is an operator

public static boolean isOperator(char c) {

return c == '+' || c == '-' || c == '*' || c == '/';

// Method to check the precedence of operators

public static int precedence(char c) {

switch (c) {

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

default:

return -1;

// Method to reverse a string

public static String reverse(String s) {

StringBuilder sb = new StringBuilder(s);

return [Link]().toString();

// Method to convert infix expression to prefix expression


public static String infixToPrefix(String infix) {

// Reverse the infix expression

String reversedInfix = reverse(infix);

// Replace ( with ) and vice versa in the reversed expression

reversedInfix = [Link]('(', '#');

reversedInfix = [Link](')', '(');

reversedInfix = [Link]('#', ')');

// Get the postfix expression of the modified infix expression

String postfix = infixToPostfix(reversedInfix);

// Reverse the postfix expression to get the prefix expression

String prefix = reverse(postfix);

return prefix;

// Method to convert infix expression to postfix expression

public static String infixToPostfix(String infix) {

Stack<Character> stack = new Stack<>();

StringBuilder result = new StringBuilder();

for (int i = 0; i < [Link](); i++) {

char currentChar = [Link](i);

if ([Link](currentChar)) {

continue;

if ([Link](currentChar)) {

[Link](currentChar);

} else if (currentChar == '(') {

[Link](currentChar);
} else if (currentChar == ')') {

while (![Link]() && [Link]() != '(') {

[Link]([Link]());

[Link](); // Pop '('

} else if (isOperator(currentChar)) {

while (![Link]() && precedence([Link]()) >= precedence(currentChar)) {

[Link]([Link]());

[Link](currentChar);

// Pop all the operators left in the stack

while (![Link]()) {

[Link]([Link]());

return [Link]();

public static void main(String[] args) {

String infixExpression = "(A-B/C)*(A/K-L)";

String prefixExpression = infixToPrefix(infixExpression);

[Link]("Infix Expression: " + infixExpression);

[Link]("Prefix Expression: " + prefixExpression);

}
5. Write a Java Program for conversion of given infix expression to postfix expression

import [Link];

public class InfixToPostfix {

// Method to check if a character is an operator

public static boolean isOperator(char c) {

return c == '+' || c == '-' || c == '*' || c == '/';

// Method to check the precedence of operators

public static int precedence(char c) {

switch (c) {

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

default:

return -1;

// Method to convert infix expression to postfix expression

public static String infixToPostfix(String infix) {

Stack<Character> stack = new Stack<>();

StringBuilder result = new StringBuilder();

for (int i = 0; i < [Link](); i++) {

char currentChar = [Link](i);


if ([Link](currentChar)) {

continue;

// If the character is an operand, add it to the result

if ([Link](currentChar)) {

[Link](currentChar);

// If the character is '(', push it onto the stack

else if (currentChar == '(') {

[Link](currentChar);

// If the character is ')', pop from stack to result until '(' is found

else if (currentChar == ')') {

while (![Link]() && [Link]() != '(') {

[Link]([Link]());

[Link](); // Remove '(' from stack

// If the character is an operator

else if (isOperator(currentChar)) {

while (![Link]() && precedence([Link]()) >= precedence(currentChar)) {

[Link]([Link]());

[Link](currentChar);

}
// Pop all the operators left in the stack

while (![Link]()) {

[Link]([Link]());

return [Link]();

public static void main(String[] args) {

String infixExpression = "A+B*(C^D-E)^(F+G*H)-I";

String postfixExpression = infixToPostfix(infixExpression);

[Link]("Infix Expression: " + infixExpression);

[Link]("Postfix Expression: " + postfixExpression);

6. Write a Java Program for conversion of given prefix expression to infix expression

import [Link];

public class PrefixToInfix {

// Method to check if the character is an operator

public static boolean isOperator(char c) {

return c == '+' || c == '-' || c == '*' || c == '/';

// Method to convert prefix expression to infix expression

public static String convert(String prefix) {

Stack<String> stack = new Stack<>();

// Scan the prefix expression from right to left

for (int i = [Link]() - 1; i >= 0; i--) {


char currentChar = [Link](i);

if ([Link](currentChar)) {

continue;

if (isOperator(currentChar)) {

// Pop two operands from stack

String operand1 = [Link]();

String operand2 = [Link]();

// Form a new string by combining the operands with the operator

String temp = "(" + operand1 + currentChar + operand2 + ")";

// Push the resulting string back onto the stack

[Link](temp);

} else {

// If the character is an operand, push it onto the stack

[Link]([Link](currentChar));

// The remaining element in the stack is the infix expression

return [Link]();

public static void main(String[] args) {

String prefixExpression = "*-A/BC-/AKL"; // Example: equivalent to (A - (B / C)) * ((A / K) - L)

String infixExpression = convert(prefixExpression);

[Link]("Prefix Expression: " + prefixExpression);

[Link]("Infix Expression: " + infixExpression);

}}
7. Write a Java Program for conversion of given prefix expression to postfix expression

import [Link];

public class PrefixToPostfix {

// Method to check if the character is an operator

public static boolean isOperator(char c) {

return c == '+' || c == '-' || c == '*' || c == '/';

// Method to convert prefix expression to postfix expression

public static String convert(String prefix) {

Stack<String> stack = new Stack<>();

// Scan the prefix expression from right to left

for (int i = [Link]() - 1; i >= 0; i--) {

char currentChar = [Link](i);

if ([Link](currentChar)) {

continue;

if (isOperator(currentChar)) {

// Pop two operands from stack

String operand1 = [Link]();

String operand2 = [Link]();

// Form a new string by combining the operands with the operator

String temp = operand1 + operand2 + currentChar;

// Push the resulting string back onto the stack

[Link](temp);

} else {

// If the character is an operand, push it onto the stack


[Link]([Link](currentChar));

// The remaining element in the stack is the postfix expression

return [Link]();

public static void main(String[] args) {

String prefixExpression = "*-A/BC-/AKL";

String postfixExpression = convert(prefixExpression);

[Link]("Prefix Expression: " + prefixExpression);

[Link]("Postfix Expression: " + postfixExpression);

8. Write a Java Program for conversion of given postfix expression to infix expression

import [Link];

public class PostfixToInfix {

// Method to check if a character is an operator

public static boolean isOperator(char c) {

return c == '+' || c == '-' || c == '*' || c == '/';

// Method to convert postfix expression to infix expression

public static String convert(String postfix) {

Stack<String> stack = new Stack<>();

// Scan the postfix expression from left to right

for (int i = 0; i < [Link](); i++) {


char currentChar = [Link](i);

if ([Link](currentChar)) {

continue;

if (isOperator(currentChar)) {

// Pop two operands from the stack

String operand2 = [Link]();

String operand1 = [Link]();

// Form a new string by combining the operands with the operator in infix form

String temp = "(" + operand1 + currentChar + operand2 + ")";

// Push the resulting string back onto the stack

[Link](temp);

} else {

// If the character is an operand, push it onto the stack

[Link]([Link](currentChar));

// The remaining element in the stack is the infix expression

return [Link]();

public static void main(String[] args) {

String postfixExpression = "ABC/-AK/L-*"; // Example: equivalent to ((A-(B/C))*((A/K)-L))

String infixExpression = convert(postfixExpression);

[Link]("Postfix Expression: " + postfixExpression);

[Link]("Infix Expression: " + infixExpression);

}}
9. Write a Java Program for conversion of given postfix expression to prefix expression

import [Link];

public class PostfixToPrefix {

// Method to check if a character is an operator

public static boolean isOperator(char c) {

return c == '+' || c == '-' || c == '*' || c == '/';

// Method to convert postfix expression to prefix expression

public static String convert(String postfix) {

Stack<String> stack = new Stack<>();

// Scan the postfix expression from left to right

for (int i = 0; i < [Link](); i++) {

char currentChar = [Link](i);

if ([Link](currentChar)) {

continue;

if (isOperator(currentChar)) {

// Pop two operands from the stack

String operand2 = [Link]();

String operand1 = [Link]();

// Form a new string by combining the operator and operands in prefix form

String temp = currentChar + operand1 + operand2;

// Push the resulting string back onto the stack

[Link](temp);

} else {

// If the character is an operand, push it onto the stack


[Link]([Link](currentChar));

// The remaining element in the stack is the prefix expression

return [Link]();

public static void main(String[] args) {

String postfixExpression = "ABC/-AK/L-*"; // Example: equivalent to ((A-(B/C))*((A/K)-L))

String prefixExpression = convert(postfixExpression);

[Link]("Postfix Expression: " + postfixExpression);

[Link]("Prefix Expression: " + prefixExpression);

You might also like