-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstack.py
More file actions
48 lines (36 loc) · 919 Bytes
/
Copy pathstack.py
File metadata and controls
48 lines (36 loc) · 919 Bytes
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
class MyStack:
def __init__(self):
self.list = []
self.head = 0
self.size = 0
'''
stack is first come last out data structure
so, when we pop we will get the data from head
'''
def pop(self):
if self.head == -1:
raise Exception("stack is empty")
# reduce size
self.size -= 1
value = self.list[self.head]
self.head -= 1
return value
'''
When we push any value, we will save that value in the list
using append and we will move header
'''
def push(self, value):
self.list.append(value)
self.head += 1
self.size += 1
def size(self):
return self.size
# Now test
stack = MyStack()
stack.push(1)
stack.push(10)
stack.push(20)
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.size)