1class Stack:
2 def __init__(self):
3 self.items = []
4
5 def isEmpty(self):
6 return self.items == []
7
8 def push(self, item):
9 self.items.append(item)
10
11 def pop(self):
12 return self.items.pop()
13
14 def peek(self):
15 return self.items[len(self.items)-1]
16
17 def size(self):
18 return len(self.items)
19
1# Stack
2class My_stack():
3 def __init__(self):
4 self.data = []
5 def my_push(self, x):
6 return (self.data.append(x))
7 def my_pop(self):
8 return (self.data.pop())
9 def my_peak(self):
10 return (self.data[-1])
11 def my_contains(self, x):
12 return (self.data.count(x))
13 def my_show_all(self):
14 return (self.data)
15
16arrStack = My_stack()
17arrStack.my_push(1)
18arrStack.my_push(2)
19arrStack.my_push(1)
20arrStack.my_push(3)
21print(arrStack.my_show_all())
22arrStack.my_pop()
23print(arrStack.my_show_all())
24print(arrStack.my_contains(1))
1>>> myStack = []
2
3>>> myStack.append('a')
4>>> myStack.append('b')
5>>> myStack.append('c')
6
7>>> myStack
8['a', 'b', 'c']
9
10>>> myStack.pop()
11'c'
12>>> myStack.pop()
13'b'
14>>> myStack.pop()
15'a'
16
17>>> myStack.pop()
18Traceback (most recent call last):
19 File "<console>", line 1, in <module>
20IndexError: pop from empty list
21