In a previous post we already set the base of a linked list class. In this post we will write some code to expand our class, for that we will create an append, pop and print methods. So let’s deep dive into the code section.
Append #
The append method adds an item to the end of the linked list. To create this method follow these next steps:
- Create a new node.
- Set the tail of the list refers to the new node.
- Change the tail of the list to the new node.
- Increment the size of the linked list
class LinkedList:
# previous code
def append(self, value) -> bool:
"""
The append method add a new node to the LL
The tail of the LL, will be the new node
"""
new_node = Node(value = value)
#Check if the LL is empty
if self.length == 0:
self.head = new_node
self.tail = new_node
self.length = 1
else:
#The current tail refence to the new_node
self.tail.next = new_node
#The new_node is now the tail of the LL
self.tail = new_node
#Increase the lenght of the LL
self.length += 1
return True
Pop #
The pop method removes the last node in a linked list and returns this node. This method is more complex than the append method, because we have to remove the last element and move the tail to the previous node. Since linked lists don’t have indexes, we have to iterate over the entire linked list to find the previous node of the tail. Then we set the previous node as the tail and dereference the current tail in the linked list. To make this possible we establish two variables in the method temp and previous. The temp variable iterates through the entire linked list until there is none and the previous variable saves the previous node of the temp iteration. In a summarized way the steps we have to follows are:
- Set the temp and prev variables to the head of the linked list
- Iterate over the linked list until the temp.next is not None
- Set prev to temp, and temp to the next reference while - iterating over the linked list
- If we reach the end of the LL set the next in prev variable to None
- Return temp
class LinkedList:
# previous code
def pop(self) -> any:
"""
The append method delete the last node of the LL
The tail of the LL, will be the previous node to the tail
Return:
the node poped in the LL
"""
#Check if the LL is empty
if self.length == 0:
return None
temp = self.head
prev = self.head
#Iterate over every node of the LL until temp is None
while(temp.next):
prev = temp
#Move temp to the next node
temp = temp.next
#Set the tail to the previous node
self.tail = prev
self.tail.next = None
self.length -= 1
#If before pop the node into the LL the lenght is 0, set head and tail to None
if self.length == 0:
print("h")
self.head = None
self.tail = None
#Return the pop element
return temp
Print #
The print method is very basic. It iterates over the linked list, and prints the value of every node. I don’t need to explain the code, as it is self explanatory.
class LinkedList:
# previous code
def print(self) -> None:
"""
Print every value of the nodes in the LL in ascendent order, this mean to the head until the tail
"""
current_node = self.head
#Iterate over every node of the LL until temp is None
while(current_node):
print(current_node.value)
#Move to the next node
current_node = current_node.next
The song of the post #
¡Te están buscando, matador!
Me dicen el matador, nací en Barracas
Si hablamos de matar mis palabras matan
No hace mucho tiempo que cayó el León Santillán
Y ahora sé que, en cualquier momento, me la van a dar
— El matador - Los fabulosos Cadillacs