-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstack.py
More file actions
54 lines (42 loc) · 947 Bytes
/
Copy pathstack.py
File metadata and controls
54 lines (42 loc) · 947 Bytes
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
class Stack:
def __init__(self, data = None):
# create a list that store data
if data == None:
self.stack = list()
elif isinstance(data, list):
self.stack = data
else:
raise ValueError('input data should be list')
return
def __iter__(self):
return iter(self.stack)
def push(self, data):
# put data into the top of stack
self.stack.append(data)
return
def pop(self):
# remove the data on the top and return it
return self.stack.pop()
def top(self):
# get the data value of the top item
return self.stack[-1]
def output_stack(self):
# print the full stack
data = []
for x in self.stack:
data.append(x)
print(data)
return
"""
stack1 = Stack()
stack1.push(1)
stack1.push(3)
stack1.push(2)
print(stack1.pop())
stack1.output_stack()
stack2 = Stack([4,6,2,1,6,3])
stack2.pop()
stack2.output_stack()
for x in stack2:
print(x)
"""