Throughout the entire talk of data structures we have used Python lists to implement the data types presented. A list is a simple, yet useful way of to provide the programmer with a large variety of options at presenting data types. Not all programming languages have a list collection so the programmer must implement the list manually using an un-ordered list or linked list. Linked lists to me was a somewhat easier concept to comprehend than trees. I learned a linked list is made up of nodes, where each node makes reference to the next one in the list. A node has a value and a next which takes the value of the next node in the list. A linked list can be one of two things:
1. An empty list which is represented by None
2. A node that contains a value object and a reference to a linked list
The node class can be written as follows:
class Node:
def __init__(self, value=None, next=None):
self.value = value
self.next = next
def __str__(self):
return str(self.value)
with the __init__ class initializing the value within the node and next that takes on the value of the following node. The next image helps to really represent what is going on in during a linked list. As you traverse down the list you obtain a new current value for each node with the next value refereeing to the following node's value.
There are many different implementations for a linked list and many different uses for them.
No comments:
Post a Comment