LAB # 10 Stack ADT Implementation
LAB # 10 Stack ADT Implementation
LAB # 10
1. Write a program that takes 20 names as input in stack. Perform all operations on stack
using stack ADT class.
Source code:
package muntasir;
import java.util.Scanner;
import java.util.Stack;
public class Muntasir {
public static void main(String[] args) {
Stack<String> names= new Stack<>();
Scanner sc = new Scanner(System.in);
for(int i = 1; i <= 20; i++)
{
System.out.println("Enter any Name: ");
String name = sc.nextLine();
names.push(name);
}
names.pop();
System.out.println("Stack after pop: " + names);
names.peek();
System.out.println("Stack after peek: " + names);
}
}
Output:
2. Convert and evaluate the expression from infix to postfix using Stack ADT class.
(A+B) * (C-D)
Source Code:
package q1;
import java.util.*;
public class Q1 {
public static void main(String[] args) {
String exp = "(A+B)*(C-D)";
System.out.println("Infix Expression:"+exp);
System.out.println("Postfix Expression:"+infixToPostfix(exp));
}
static int Prec(char ch){
switch (ch){ case
'+': case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
static String infixToPostfix(String exp){
String result = new String("");
Stack<Character> stack = new Stack<>();
for (int i = 0; i<exp.length(); ++i){
char c = exp.charAt(i);
if
(Character.isLetterOrDigit(c))
result += c;
else if (c == '(')
stack.push(c);
else if (c == ')'){
while (!stack.isEmpty() &&
stack.peek() != '(')
result += stack.pop();
stack.pop();
} else {
while (!stack.isEmpty() && Prec(c)
<= Prec(stack.peek())){
result += stack.pop();
}
stack.push(c);
}
}
while (!stack.isEmpty()){
if(stack.peek() == '(')
return "Invalid Expression";
result += stack.pop();
}
return result;
}
}
Output:
Home Task
1. Convert and evaluate the expression from infix to postfix using Stack ADT class.
A+(B*C-(D/E|F)*G)*H
Source Code:
package ht1;
import java.util.*;
public class Ht1 {
public static void main(String[] args) {
try{
String exp = "A+(B*C-(D/E+F)*G)*H";
String postfix = infixToPostfix(exp);
System.out.println("Infix Expression:"+exp);
System.out.println("Posfix Expression:"+postfix);
postfix = postfix.replace("A","1"); postfix =
postfix.replace("B","2"); postfix =
postfix.replace("C","3"); postfix =
postfix.replace("D","4"); postfix =
postfix.replace("E","5"); postfix =
postfix.replace("F","6"); postfix =
postfix.replace("G","7"); postfix =
postfix.replace("H","8");
System.out.println(postfix);
System.out.println("Evaluation: "+evaluatePostfix(postfix));
}catch (EmptyStackException e){ e.printStackTrace();
} } static int evaluatePostfix(String exp) throws
EmptyStackException{ Stack<Integer> stack=new Stack<>();
for(int i=0;i<exp.length();i++){ char c=exp.charAt(i);
if(Character.isDigit(c)) stack.push(c - '0');
else{
int val1 = stack.pop();
int val2 = stack.pop();
switch(c){
case '+':
stack.push(val2+val1);
break;
case '-':
stack.push(val2- val1);
break;
case
'/':
stack.push(val2/val1);
break;
case '*':
stack.push(val2*val1);
break;
}
} }
return stack.pop();
}
static int Prec(char ch){
switch (ch){ case
'+': case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
} static String infixToPostfix(String exp) throws
EmptyStackException{ String result = new String("");
else if (c == '(')
stack.push(c);
else if (c ==
')')
{
while (!stack.isEmpty() &&
stack.peek() != '(') result +=
stack.pop();
stack.pop();
}
else {
while (!stack.isEmpty() && Prec(c)
<= Prec(stack.peek())){
result += stack.pop();
}
stack.push(c);
}
} while (!stack.isEmpty()){
if(stack.peek() == '(')
return "Invalid Expression";
result += stack.pop();
}
return result;
}
}
Output: