You are on page 1of 11

SWE:203L Data Structure & Algorithm SSUET/QR/114

LAB # 10

Stack ADT Implementation

Object

Implement Stack by using Stack class.

Sample Program # 1: package

Lab10;

import java.util.*;

public class StackDemo {

static void showpush(Stack st, int a)

{ st.push(new Integer(a));

System.out.println("push(" + a + ")");

System.out.println("stack: " + st);

static void showpop(Stack st)

{ System.out.print("pop -> ");

Integer a = (Integer) st.pop();

System.out.println(a);

System.out.println("stack: " + st);

public static void main(String args[])

{ Stack st = new Stack();

System.out.println("stack: " + st);

showpush(st, 42);

Name: Syed Ashhad


Roll Number: 2020-SE-224 Page 1
SWE:203L Data Structure & Algorithm SSUET/QR/114

showpush(st, 66);

showpush(st, 99);

showpop(st); showpop(st);

showpop(st);

try {

showpop(st);

} catch (EmptyStackException e) {

System.out.println("empty stack");

OUTPUT:

Name: Syed Ashhad


Roll Number: 2020-SE-224 Page 2
SWE:203L Data Structure & Algorithm SSUET/QR/114

LAB TASK

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

(A+B) * (C-D)

CODE:

package Lab10;

import java.util.Stack;

public class TASK1 {

public static void main(String[] args){

String exp = "(A+B)*(C-D)";

System.out.println("Infix notation: "+exp);

System.out.println("Postfix Notation: "+infixToPostfix(exp));

static int Prec(char ch)

{ switch (ch)

{ case

'+': case

'-':

return 1;

case '*': case

'/':

return 2;

case '^':

Name: Syed Ashhad


Roll Number: 2020-SE-224 Page 3
SWE:203L Data Structure & Algorithm SSUET/QR/114

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 // an operator is encountered

Name: Syed Ashhad


Roll Number: 2020-SE-224 Page 4
SWE:203L Data Structure & Algorithm SSUET/QR/114

while (!stack.isEmpty() && Prec(c) <=

Prec(stack.peek())){ result += stack.pop();

stack.push(c);

// pop all the operators from the stack

while (!stack.isEmpty()){

if(stack.peek() == '(')

return "Invalid Expression";

result += stack.pop();

return result;

OUTPUT:

Name: Syed Ashhad


Roll Number: 2020-SE-224 Page 5
SWE:203L Data Structure & Algorithm SSUET/QR/114

HOME TASK:

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

A + ( B * C - ( D / E | F ) * G ) * H CODE:

package Lab10;

import java.util.*;

public class HOMETASK{

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 notation: "+exp);

System.out.println("Posfix notation: "+postfix);

postfix = postfix.replace("A","2"); postfix =

postfix.replace("B","4"); postfix =

postfix.replace("C","7"); postfix =

postfix.replace("D","8"); postfix =

postfix.replace("E","3"); postfix =

postfix.replace("F","4"); postfix =

postfix.replace("G","3"); postfix =

postfix.replace("H","5");

System.out.println(postfix);

System.out.println("Evaluation: "+evaluatePostfix(postfix));

}catch (EmptyStackException e){

Name: Syed Ashhad


Roll Number: 2020-SE-224 Page 6
SWE:203L Data Structure & Algorithm SSUET/QR/114

e.printStackTrace();

static int evaluatePostfix(String exp) throws EmptyStackException{

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

// Scan all characters one by one 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 '*':

Name: Syed Ashhad


Roll Number: 2020-SE-224 Page 7
SWE:203L Data Structure & Algorithm SSUET/QR/114

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)

Name: Syed Ashhad


Roll Number: 2020-SE-224 Page 8
SWE:203L Data Structure & Algorithm SSUET/QR/114

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 // an operator is encountered

while (!stack.isEmpty() && Prec(c)

<= Prec(stack.peek())){

result += stack.pop();

stack.push(c);

Name: Syed Ashhad


Roll Number: 2020-SE-224 Page 9
SWE:203L Data Structure & Algorithm SSUET/QR/114

// pop all the operators from the stack

while (!stack.isEmpty()){

if(stack.peek() == '(')

return "Invalid Expression";

result += stack.pop();

return result;

OUTPUT:

Name: Syed Ashhad


Roll Number: 2020-SE-224 10
SWE:203L Data Structure & Algorithm SSUET/QR/114

Page

Name: Syed Ashhad


Roll Number: 2020-SE-224 11

You might also like