forked from NITSkmOS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
62 lines (44 loc) · 1.15 KB
/
stack.py
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
60
61
62
class Stack:
"""
Class for operations related to stack.
"""
def __init__(self):
self.stack = [] # Initialise empty stack
def push(self, dataval):
"""
Method to push items in stack.
:param dataval: Value that user needs to push.
"""
self.stack.append(dataval)
def pop(self):
"""
Method for removing the last element from the stack.
:return: Element that is popped.
"""
if len(self.stack) <= 0:
raise IndexError("No element in the Stack")
else:
return self.stack.pop()
def top(self):
"""
Method to show the last element of the stack.
:return: last element of the stack.
"""
return self.stack[len(self.stack)-1]
def display(self):
"""
Method to print all elements of the stack.
"""
print(*self.stack)
def main():
AStack = Stack()
AStack.push("Mon")
AStack.push("Tue")
AStack.pop()
AStack.push("Wed")
m = AStack.top()
print(m)
AStack.push("Thu")
AStack.display()
if __name__ == "__main__":
main()