Skip to main content

Data Structure Homework 3

·98 words·1 min
Andy Lu
Author
Andy Lu

Question 1 (a)
#

Question 1(b)
#

Answer derived from the below path.

Question 2(a)
#

Question 2(b)
#

Question 3
#

Question 4
#

# pseudo postfix eval code 
# this is based on code in homework 3
postfix:
    while(token != endOfString):
        if(token.isdigit()) stack.push(int(token))
        else:
            number2 = stack.pop()
            number1 = stack.pop()
            stack.push(calculate(number1 , token , number2))
        token = get_token_fromString()
    return stack.top()

# modify to prefix version
prefix:
    while(token != endOfString):
        if(token.isdigit()):
            if(stack.top().isdigit()):
                number2 = int(token)
                number1 = stack.pop()
                calc = stack.pop() # +-*/
                stack.push(calculate(number1 , calc , number2))
            else:
                stack.push(int(token))
        else:
            stack.push(token)
        token = get_token_fromString()
    return stack.top()