-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
56 lines (48 loc) · 1.15 KB
/
stack.py
File metadata and controls
56 lines (48 loc) · 1.15 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
class Stack(object):
def __init__(self,size=10):
self.stack = []
self.size = size
def stackTrace(self):
"""
Prints all elements of the Stack
"""
print([x for x in self.stack])
def push(self,element):
"""
Push/Insert a new element into the Stack.
"""
if len(self.stack) >= self.size:
print("Stack Full!!")
else:
self.stack.append(element)
def pop(self):
"""
Pops/deletes the topmost element in the Stack.
"""
if len(self.stack) <= 0:
print("Stack Empty!!")
else:
self.stack.pop()
def getTop(self):
"""
Returns topmost element from the Stack without deleting it.
"""
print(self.stack[len(self.stack)-1])
def isEmpty(self):
"""
Returns True if the Stack is Empty.
"""
if len(self.stack) == 0:
return True
else:
return False
s = Stack(2)
s.pop()
s.push(50)
s.push(45)
s.push(88)
s.getTop()
s.stackTrace()
s.pop()
s.stackTrace()
s.getTop()