-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement_stack_linkedList.py
More file actions
62 lines (51 loc) · 1.36 KB
/
implement_stack_linkedList.py
File metadata and controls
62 lines (51 loc) · 1.36 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
57
class Node:
def __init__(self, value):
self.value = value
self.next = None
class Stack:
def __init__(self):
self.head = None
self.num_elements = 0
def to_list(self):
py_list = []
node = self.head
while node:
py_list.append(node.value)
node = node.next
return py_list
def push(self, value):
if self.head is None:
self.head = Node(value)
else:
new_node = Node(value)
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.head is None:
return None
value = self.head.value
self.head = self.head.next
self.num_elements -=1
return value
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")