-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stack.py
98 lines (67 loc) · 1.9 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'''
The functions associated with stack are:
empty() – Returns whether the stack is empty – Time Complexity : O(1)
size() – Returns the size of the stack – Time Complexity : O(1)
top() – Returns a reference to the top most element of the stack – Time Complexity : O(1)
push(g) – Adds the element ‘g’ at the top of the stack – Time Complexity : O(1)
pop() – Deletes the top most element of the stack – Time Complexity : O(1)
'''
'''Implementation using list:'''
print('----------Implementation using list-------------')
stack = []
stack.append('January')
stack.append('February')
stack.append('March')
stack.append('April')
print(stack)
print(stack.pop())
print(stack)
print(stack.pop())
print(stack)
print(stack.pop())
print(stack)
print(stack.pop())
print(stack)
'''Implementation using collections.deque'''
print('----------Implementation using collections.deque-------------')
from collections import deque
stack_de = deque()
stack_de.append('Book')
stack_de.append('pen')
stack_de.append('College')
stack_de.append('University')
print(stack_de)
print(stack_de.pop())
print(stack_de)
print(stack_de.pop())
print(stack_de)
print(stack_de.pop())
print(stack_de)
print(stack_de.pop())
print(stack_de)
'''Exaple Of Stack Implementation'''
print('-------------Exaple Of Stack Implementation---------------')
class Stack():
def __init__(self):
self.items = []
def push(self,item):
self.items.append(item)
def pop(self):
return self.items.pop()
def is_emppty(self):
return self.items == []
def peek(self):
if not self.is_emppty():
return self.items[-1]
def get_stack(self):
return self.items
myStack = Stack()
myStack.push('Java')
myStack.push('Python')
myStack.push('Dart')
myStack.push('JavaScript')
print(myStack)
print('----test----')
print(myStack.peek())
print(myStack.is_emppty())
print(myStack.get_stack())