|
| 1 | +class stack: |
| 2 | + def __init__(self): |
| 3 | + self.item = [] |
| 4 | + |
| 5 | + def push(self,it): |
| 6 | + self.item.append(it) |
| 7 | + def peek(self): #position of the data |
| 8 | + if self.isempty() == True: |
| 9 | + return 0 |
| 10 | + return self.item[-1] |
| 11 | + |
| 12 | + def pop(self): |
| 13 | + if self.isempty() == True: |
| 14 | + return 0 |
| 15 | + return(self.item.pop()) |
| 16 | + |
| 17 | + def length(self): |
| 18 | + return (len(self.item)) |
| 19 | + |
| 20 | + |
| 21 | + def isempty(self): |
| 22 | + if self.item == []: |
| 23 | + return True |
| 24 | + else: |
| 25 | + return False |
| 26 | + |
| 27 | + def display(self): |
| 28 | + if self.isempty()== True: |
| 29 | + return |
| 30 | + temps = stack() |
| 31 | + while(self.isempty() != True): |
| 32 | + x = self.peek() |
| 33 | + print("~",x) |
| 34 | + temps.push(x) |
| 35 | + self.pop() |
| 36 | + while(temps.isempty() != True): |
| 37 | + x = temps.peek() |
| 38 | + self.push(x) |
| 39 | + temps.pop() |
| 40 | + |
| 41 | + |
| 42 | + def isOperand(self, ch): |
| 43 | + return ch.isalpha() |
| 44 | + |
| 45 | + def notGreater(self, i): |
| 46 | + precedence = {'+':1, '-':1, '*':2, '/':2, '%':2, '^':3} |
| 47 | + if self.peek() == '(': |
| 48 | + return False |
| 49 | + a = precedence[i] |
| 50 | + b = precedence[self.peek()] |
| 51 | + if a <= b: |
| 52 | + return True |
| 53 | + else: |
| 54 | + return False |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + def infixToPostfix(self, exp): |
| 59 | + output = "" |
| 60 | + |
| 61 | + for i in exp: |
| 62 | + |
| 63 | + if self.isOperand(i) == True: # check if operand add to output |
| 64 | + print(i,"~ Operand push to stack") |
| 65 | + output = output + i |
| 66 | + |
| 67 | + # If the character is an '(', push it to stack |
| 68 | + elif i == '(': |
| 69 | + self.push(i) |
| 70 | + print(i," ~ Found ( push into stack") |
| 71 | + |
| 72 | + elif i == ')': # if ')' pop till '(' |
| 73 | + while( self.isempty() != True and self.peek() != '('): |
| 74 | + n = self.pop() |
| 75 | + output = output + n |
| 76 | + print(n, "~ Operator popped from stack") |
| 77 | + if (self.isempty() != True and self.peek() != '('): |
| 78 | + print("_________") |
| 79 | + return -1 |
| 80 | + else: |
| 81 | + x = self.pop() |
| 82 | + print(x, "Popping and deleting (") |
| 83 | + else: |
| 84 | + while(self.isempty() != True and self.notGreater(i)): |
| 85 | + if self.peek()=='^' and i=='^': |
| 86 | + break |
| 87 | + c = self.pop() |
| 88 | + output = output + c |
| 89 | + print(c,"Operator popped after checking precedence from stack") |
| 90 | + self.push(i) |
| 91 | + print(i,"~ Operator pushed to stack") |
| 92 | + |
| 93 | + # pop all the operator from the stack |
| 94 | + while self.isempty() != True: |
| 95 | + xx = self.pop() |
| 96 | + output = output + xx |
| 97 | + print(xx,"~ pop at last") |
| 98 | + print(output) |
| 99 | + self.display() |
| 100 | +st = stack() |
| 101 | +st.infixToPostfix("a^b^c") |
0 commit comments