Creating a Linked List Structure Using Nodes
A Linked List
is a data structure where data elements are organized into individual units called Nodes
which are interconnected.
Each node is made up of Data
and a Pointer
pointing to the next node.
Structure and Characteristics of Linked Lists
Linked lists have the following structure and characteristics.
Node
The basic unit of a linked list.
Each node contains data and one or two pointers that reference the next node (and sometimes the previous node).
Head
A pointer that references the first node of the linked list.
It signifies the starting point of the linked list.
Tail
A pointer that references the last node of the linked list.
The pointer in the tail node generally points to None
.
How can linked lists be implemented?
Below is a Python example that implements a Singly Linked List
.
class Node: def __init__(self, data): # Node data self.data = data # Pointer to the next node self.next = None class LinkedList: def __init__(self, data): # Create the first node self.head = Node(data) # Initially, head and tail are the same self.tail = self.head def append(self, data): # Create a new node and link it node = Node(data) # Link the new node to the tail node self.tail.next = node # Set the new node as the tail self.tail = node def print_all(self): # Print the linked list node = self.head # Traverse the nodes while printing data while node: print(node.data, end=" ") node = node.next print() # Create the linked list linked_list = LinkedList(5) # Add nodes linked_list.append(12) linked_list.append(7) # Print the linked list linked_list.print_all() # Output: 5 12 7
What types of linked lists are there?
Linked lists are categorized according to how nodes are connected.
Singly Linked List
The simplest form of a linked list where each node points only to the next node.
Doubly Linked List
Each node points to both the previous node and the next node.
Allows bidirectional traversal, making insertion and deletion more efficient.
Circular Linked List
The last node points to the first node, forming a circular structure.
Useful for cyclic iterations.
Comparison between Linked Lists and Arrays
Characteristic | Linked List | Array |
---|---|---|
Memory Allocation | Dynamically allocates memory | Uses a contiguous block of memory |
Insertion/Deletion | Fast insertion/deletion at specific positions | Inefficient insertion/deletion at specific positions |
Random Access | Can only be accessed sequentially | Instant access via indexing |
Linked lists are advantageous for memory management and insertion/deletion operations, while arrays are better suited for random access and faster data processing.
What is each element of a linked list called?
Pointer
Head
Node
Tail
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result