I am trying to pop an item off a stack (using a linked list as oppose to an array). I first created a LinkedList class with 3 nodes with the values [1,2,3]. So I would like pop off the last node (node_C, value=3), therefore I expect to see the values [1,2]. Instead nothing prints out.
class LinkedList:
def __init__(self, value):
self.value = value
self.next = None
node_A = LinkedList(1)
node_B = LinkedList(2)
node_C = LinkedList(3)
node_A.next = node_B
node_B.next = node_C
def pop(head):
current_node = head
while current_node.next:
if current_node.next == None:
break
else:
current_node = current_node.next
del current_node
return node_A.value, node_B.value, node_C.value
try:
print(pop(node_A))
except NameError:
pass
How can I rewrite this to achieve my desired results (i.e. show values 1,2 .. with 3 popped off)?