-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyStack.py
More file actions
59 lines (45 loc) · 1.2 KB
/
Copy pathmyStack.py
File metadata and controls
59 lines (45 loc) · 1.2 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
#IMPLEMENT A STACK USE LIST
class myStack:
__data = []
__count = 0
__CAPACITY = 10
def __init__(self):
self.__data = []
self.__count = 0
def pop(self):
if self.__count == 0:
print('Stack underflow!')
else:
self.__count -= 1
return self.__data.pop()
def push(self, dat):
if self.__count == 10:
print('Tring to')
print('Stack overflow!')
else:
self.__data.append(dat)
self.__count += 1
def peak(self):
if self.__count == 0:
print('Stack is empty!')
else:
return self.__data[self.__count-1]
if __name__ == '__main__':
'''empty pop test'''
s = myStack()
assert(s.pop() == None)
'''push one element'''
s.push(1)
assert(s.peak() == 1)
print('Top of stack is', s.peak())
'''push one more and pop test'''
s.push(2)
assert(s.pop() == 2)
assert(s.peak() == 1)
print('Top of stack is', s.peak())
'''Overflow test'''
s.pop()
print(s.peak())
for i in range(0,11):
s.push(i)
print('Top of stack is', s.peak())