Clicky

Go back to the programming category
Go back to previous page
Array
Trees
Introduction to Linked Lists

Introduction to Linked Lists

Linked Lists are an important data structure in computer science and are used in many real-world applications. They are a linear collection of data elements, called nodes, where each node points to the next node in the list. Linked Lists offer several benefits over arrays, including dynamic sizing, easy insertion and deletion, and efficient memory utilization.

Advantages of Linked Lists Over Arrays

Arrays have a fixed size and can become slow when it comes to inserting or deleting elements in the middle of the list. In contrast, linked lists have dynamic sizing, which means they can grow or shrink as needed. This makes linked lists a better choice when working with large amounts of data that may change frequently.

Another advantage of linked lists is their ease of insertion and deletion. Adding or removing an element from a linked list can be done in constant time, O(1), regardless of the size of the list. In contrast, inserting or deleting elements from an array can take linear time, O(n), because the elements after the insertion or deletion point need to be shifted to accommodate the change.

Finally, linked lists can also be more memory efficient than arrays. This is because linked lists only allocate memory for the data elements as needed, rather than allocating a large block of memory for an array in advance. This can be especially important when working with limited memory resources.

Types of Linked Lists

There are several different types of linked lists, each with its own unique characteristics. Some of the most common types of linked lists include:

  • Singly Linked Lists: In a singly linked list, each node has a single pointer that points to the next node in the list.
  • Doubly Linked Lists: In a doubly linked list, each node has two pointers, one that points to the next node and another that points to the previous node.
  • Circular Linked Lists: In a circular linked list, the last node in the list points back to the first node, creating a circular loop.
  • Multi-linked Lists: In a multi-linked list, each node has multiple pointers that can point to different nodes in the list.

Applications of Linked Lists

Some applications of Linked lists are:

  • Implementing Stacks and Queues: Linked lists can be used to implement both stacks and queues.
  • Dynamic Memory Allocation: Linked lists are often used to implement dynamic memory allocation in computer systems.
  • Graphs: Linked lists can be used to represent graphs, which are useful for modeling relationships between objects.
  • Databases: Linked lists can be used to store data in databases, allowing for fast and efficient retrieval and manipulation of the data.

Python Implementation

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            return
        current = self.head
        while current.next:
            current = current.next
        current.next = new_node

    def prepend(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node

    def insert_after_node(self, prev_node, data):
        if not prev_node:
            print("Previous node is not in the list")
            return
        new_node = Node(data)
        new_node.next = prev_node.next
        prev_node.next = new_node

    def delete_node(self, key):
        current = self.head
        if current and current.data == key:
            self.head = current.next
            current = None
            return
        prev = None
        while current and current.data != key:
            prev = current
            current = current.next
        if current is None:
            return
        prev.next = current.next
        current = None

    def delete_node_at_pos(self, pos):
        current = self.head
        if pos == 0:
            self.head = current.next
            current = None
            return
        prev = None
        count = 1
        while current and count != pos:
            prev = current
            current = current.next
            count += 1
        if current is None:
            return
        prev.next = current.next
        current = None

    def len_iterative(self):
        count = 0
        current = self.head
        while current:
            count += 1
            current = current.next
        return count

    def len_recursive(self, node):
        if node is None:
            return 0
        return 1 + self.len_recursive(node.next)

    def print_list(self):
        current = self.head
        while current:
            print(current.data, end=' ')
            current = current.next

Conclusion

Linked lists are an important data structure that offer several benefits over arrays. They are dynamic, efficient, and versatile. Whether you are implementing stacks and queues, allocating memory, modeling relationships, or storing data, linked lists can provide a powerful solution.