forked from super30admin/PreCourse-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_2.py
More file actions
52 lines (44 loc) · 1.46 KB
/
Exercise_2.py
File metadata and controls
52 lines (44 loc) · 1.46 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
#Time Complexity: O(1)
#space Complexity: O(1)
#did not do on leetcode
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Stack:
def __init__(self):
self.head=None
def push(self, data):
# Create a new node and point its next node to the current head then update the head to the new node
new_node=Node(data)
new_node.next=self.head
self.head=new_node
def pop(self):
#check if node is empty
if self.head is None:
return None
else:
#take the data node of head and change head tode to the next one
pop_node=self.head.data
self.head=self.head.next
return pop_node
a_stack = Stack()
while True:
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
print('push <value>')
print('pop')
print('quit')
do = input('What would you like to do? ').split()
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
operation = do[0].strip().lower()
if operation == 'push':
a_stack.push(int(do[1]))
elif operation == 'pop':
popped = a_stack.pop()
if popped is None:
print('Stack is empty.')
else:
print('Popped value: ', int(popped))
elif operation == 'quit':
break
#pop operation was not working correctly because I did not check if it is empty