-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse_Polish_Notation.py
More file actions
60 lines (57 loc) · 1.55 KB
/
Copy pathReverse_Polish_Notation.py
File metadata and controls
60 lines (57 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Node:
def __init__(self, value):
self.value = value
self.next = None
class Stack:
def __init__(self):
self.head = None
self.num_elements = 0
def push(self, value):
if self.head is None:
self.head = Node(value)
else:
new_node = Node(value)
new_node.next = self.head
self.head = new_node
self.num_elements += 1
def pop(self):
if self.head is None:
return None
value = self.head.value
self.head = self.head.next
self.num_elements -=1
return value
def size(self):
return self.num_elements
def is_empty(self):
return self.num_elements == 0
def top(self):
if self.head is None:
return None
return self.head.value
def evaluate_post_fix(input_list):
stack = Stack()
for element in input_list:
if element == '+':
second = stack.pop()
first = stack.pop()
stack.push(first + second)
elif element == '-':
second = stack.pop()
first = stack.pop()
stack.push(first-second)
elif element == '*':
second = stack.pop()
first = stack.pop()
stack.push(first*second)
elif elemnt == '/'
second = stack.pop()
first = stack.pop()
stack.push(int(first/second))
else:
stack.push(int(element))
stack = Stack()
stack.push(1)
stack.push(2)
stack.pop()
print(stack.top())