0% found this document useful (0 votes)
58 views

LAB # 10 Stack ADT Implementation

The document describes a lab assignment on implementing a stack data structure in Java. It includes: 1. Pushing 20 names into a stack and performing pop and peek operations. 2. Converting an infix expression (A+B) * (C-D) to postfix notation using a stack. 3. As a homework problem, converting a more complex infix expression A+(B*C-(D/E|F)*G)*H to postfix and then evaluating the postfix expression. The code provided implements stacks using classes and methods to perform the conversions and evaluations.

Uploaded by

Farjad Ali
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)
58 views

LAB # 10 Stack ADT Implementation

The document describes a lab assignment on implementing a stack data structure in Java. It includes: 1. Pushing 20 names into a stack and performing pop and peek operations. 2. Converting an infix expression (A+B) * (C-D) to postfix notation using a stack. 3. As a homework problem, converting a more complex infix expression A+(B*C-(D/E|F)*G)*H to postfix and then evaluating the postfix expression. The code provided implements stacks using classes and methods to perform the conversions and evaluations.

Uploaded by

Farjad Ali
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
You are on page 1/ 6

SWE-203L: Data Structures and Algorithms SSUET/QR/114

LAB # 10

Stack ADT Implementation


Lab Task

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);
}

System.out.println("Stack: " + names);

names.pop();
System.out.println("Stack after pop: " + names);

names.peek();
System.out.println("Stack after peek: " + names);
}
}

Output:

Name:Muhammad Areesh Page 1


Roll No:2020-SE-150
SWE-203L: Data Structures and Algorithms SSUET/QR/114

2. Convert and evaluate the expression from infix to postfix using Stack ADT class.

Name:Muhammad Areesh Page 2


Roll No:2020-SE-150
SWE-203L: Data Structures and Algorithms SSUET/QR/114

(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);
}
}

Name:Muhammad Areesh Page 3


Roll No:2020-SE-150
SWE-203L: Data Structures and Algorithms SSUET/QR/114

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);

Name:Muhammad Areesh Page 4


Roll No:2020-SE-150
SWE-203L: Data Structures and Algorithms SSUET/QR/114

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("");

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 ==
')')

Name:Muhammad Areesh Page 5


Roll No:2020-SE-150
SWE-203L: Data Structures and Algorithms SSUET/QR/114

{
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:

Name:Muhammad Areesh Page 6


Roll No:2020-SE-150

You might also like