Status of ''self.head'' in a singly link list
Question:
I’ve been practicing link list but can’t understand what ”self.head” actually refers.
Is it the first value in a list at index 0? And how can I print data inside the head?
class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class Linkedlist:
def __init__(self):
self.head = None
def print_var(self):
itr = self.head
print(itr.data)
def insert_at_begining(self, data):
node = Node(data, self.head)
self.head = node
if __name__ = '__main__':
ll = Linkedlsit()
ll.insert_at_begining(3)
ll.insert_at_begining(4)
ll.insert_at_begining(6)
ll.insert_at_begining(8)
ll.print()
If I’m to call print fuction function, it will through an error.
(Say, linklist is not empty)
Answers:
self.head
refers to the first Node
in the list. From there, you can "walk" the list with something like:
current_node = ll.head
while current_node:
print(f"Current node data: {current_node.data}")
current_node = current_node.next
print("Reached End of List")
Check out my code and related comments
ll = LinkedList() # Create empty list
print(ll.head) # This prints None, because the list is empty, there are no nodes
ll.print_var() # This gives you an error. You're trying to print ll.head.data, but as shown before ll.head is None, not a Node
ll.head = Node('hello', ll.head) # The list now has one node
ll.print_var() # This is now working, and prints 'hello'
After fixing your misspellings and calling the function by its proper name, print_var, it functions as it should. If you wanted to display your entire linked list you can do so with one of these methods:
def __iter__(self):
node = self.head
while node is not None:
yield node.data
node = node.next
yield node
def show(self):
node = self.head
while node is not None:
print(node.data, end=" -> ")
node = node.next
print(node)
__iter__ makes it an iterable you can loop through and show() will print the contents of the linked list.
As for self.head, it points to the newest node added to your linked list or None in an empty linked list.
I’ve been practicing link list but can’t understand what ”self.head” actually refers.
Is it the first value in a list at index 0? And how can I print data inside the head?
class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class Linkedlist:
def __init__(self):
self.head = None
def print_var(self):
itr = self.head
print(itr.data)
def insert_at_begining(self, data):
node = Node(data, self.head)
self.head = node
if __name__ = '__main__':
ll = Linkedlsit()
ll.insert_at_begining(3)
ll.insert_at_begining(4)
ll.insert_at_begining(6)
ll.insert_at_begining(8)
ll.print()
If I’m to call print fuction function, it will through an error.
(Say, linklist is not empty)
self.head
refers to the first Node
in the list. From there, you can "walk" the list with something like:
current_node = ll.head
while current_node:
print(f"Current node data: {current_node.data}")
current_node = current_node.next
print("Reached End of List")
Check out my code and related comments
ll = LinkedList() # Create empty list
print(ll.head) # This prints None, because the list is empty, there are no nodes
ll.print_var() # This gives you an error. You're trying to print ll.head.data, but as shown before ll.head is None, not a Node
ll.head = Node('hello', ll.head) # The list now has one node
ll.print_var() # This is now working, and prints 'hello'
After fixing your misspellings and calling the function by its proper name, print_var, it functions as it should. If you wanted to display your entire linked list you can do so with one of these methods:
def __iter__(self):
node = self.head
while node is not None:
yield node.data
node = node.next
yield node
def show(self):
node = self.head
while node is not None:
print(node.data, end=" -> ")
node = node.next
print(node)
__iter__ makes it an iterable you can loop through and show() will print the contents of the linked list.
As for self.head, it points to the newest node added to your linked list or None in an empty linked list.