Dynamically Connecting Data with Linked Lists
Linked Lists are a type of data structure that stores data in a sequential manner where each element contains a reference (pointer) to the next element.
Linked lists have the advantage of not requiring the elements to be in contiguous physical locations and can dynamically adjust their size.
How Are Linked Lists Structured?
Linked lists are composed of units called nodes.
Each node consists of data and a reference (next) to the next node.
In a linked list, the first node is called the head, and since the last node has no reference to a next node, it points to None.
Implementing a Linked List in Python
A linked list is structured such that each node is connected to the next node through the reference.
Below is an example of implementing a linked list in Python.
# Node class class Node: def __init__(self, data): self.data = data self.next = None # Linked List class class LinkedList: # Initialize the linked list def __init__(self): self.head = None # Add a node def append(self, data): new_node = Node(data) if not self.head: self.head = new_node print(f"{data} has been added as the head node.") return last_node = self.head while last_node.next: last_node = last_node.next last_node.next = new_node print(f"{data} has been added to the list.") # Display the linked list def display(self): current_node = self.head while current_node: print(current_node.data, end=" -> ") current_node = current_node.next print("None") # Example usage of the linked list my_list = LinkedList() my_list.append(1) # 1 has been added as the head node my_list.append(2) # 2 has been added to the list my_list.append(3) # 3 has been added to the list my_list.display() # 1 -> 2 -> 3 -> None
Explanation of the Code
-
Nodeclass: A node class that contains data and a reference to the next node. -
LinkedListclass: A class that implements the linked list, with a methodappend()to add nodes and a methoddisplay()to print the list. -
append(): A method to add a new node. If the list is empty, it adds the node as the head. Otherwise, it adds the node after the last node. -
display(): A method to print the linked list by traversing from the head node to the last node, printing the data. -
my_list: An instance of theLinkedListclass. It adds 1, 2, and 3 using theappend()method and then prints the list using thedisplay()method.
When Are Linked Lists Used?
Linked lists are useful when there are frequent insertions and deletions of data.
For example, they are used in scenarios such as music player playlists that often change or memory management in operating systems where linked lists are utilized.
Lessons in this chapter ยท Object-Oriented Programming, Data Structures, and Basic Algorithms
- 1. What is Object-Oriented Programming?
- 2. Defining the Beginning, End, and Behavior of Objects
- 3. Extending Class Functionality Through Inheritance
- 4. Encapsulation and Polymorphism, Core Concepts of Object-Oriented Programming
- 5. Fill-in-the-blank quiz
- 6. Data Structures for Systematic Data Management
- 7. Piling Up Like Plates(Stack)
- 8. Standing in Line, Queue
- 9. Dynamically Connecting Data with Linked Lists
- 10. Managing Data with Keys and Values using a Hash Table
- 11. What is a Tree?
- 12. Multiple-choice quiz
- 13. What is an Algorithm?
- 14. Fast and Efficient Search Method, Binary Search
- 15. Coding Quiz - Implementing Binary Search Algorithm
What is the basic unit of a Linked List?
Head
Data
Node
Reference
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help