forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stacks.py
62 lines (47 loc) · 1.37 KB
/
Stacks.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
# Implementing stacks using linked list
class Node(object):
def __init__(self, value):
self.value = value
self.next = None
class Stack(object):
def __init__(self):
self.head = None
self.num_elements = 0
def push(self, value):
new_node = Node(value)
if self.head is None:
self.head = new_node
else:
new_node.next = self.head
self.head = new_node
self.num_elements += 1
def size(self):
return self.num_elements
def is_empty(self):
return self.num_elements == 0
def pop(self):
if self.is_empty():
return None
value = self.head.value
self.head = self.head.next
self.num_elements -= 1
return value
if __name__ == '__main__':
# Initialize stacks with some values
stack = Stack()
stack.push(10)
stack.push(20)
stack.push(30)
stack.push(40)
stack.push(50)
# Test size()
print ("Pass" if (stack.size() == 5) else "Fail")
# Test pop()
print ("Pass" if (stack.pop() == 50) else "Fail")
# Test push()
stack.push(60)
print ("Pass" if (stack.pop() == 60) else "Fail")
print ("Pass" if (stack.pop() == 40) else "Fail")
print ("Pass" if (stack.pop() == 30) else "Fail")
stack.push(50)
print ("Pass" if (stack.size() == 3) else "Fail")