change the postfix function to a prefix and infix functionsrespectively. In python please.. If possible add comments as itwill be helpful explaining
# Stack.py
class Stack:
#————————————————————
def __init__(self,c):
”’post: creates an empty LIFO stack”’
self.items = []
self.c=c
#————————————————————
def push(self, item):
”’post: places x on top of the stack”’
self.items.append(item)
#————————————————————
def pop(self):
”’post: removes and returns the top element of
the stack”’
return self.items.pop()
#————————————————————
def top(self):
”’post: returns the top element of the stack without
removing it”’
return self.items[-1]
#————————————————————
def size(self):
”’post: returns the number of elements in the stack”’
return len(self.items)
def postfix(self, sia):
for i in sia:
if i.isdigit():
self.push(i)
else:
val1 = self.pop()
val2 = self.pop()
self.push(str(eval(val2 + i + val1)))
return int(self.pop())
doc=”231*9-“
nurse = Stack(len(doc))
print(“Value of %s is %d” % (doc, nurse.postfix(doc)))
Expert Answer
An
PayPal Gateway not configured
PayPal Gateway not configured