Unveiling the Power of Linked Lists in Python: A Comprehensive Guide

Unveiling the Power of Linked Lists in Python: A Comprehensive Guide

Hey everyone! Today, I'm diving into the world of linked lists, a fundamental data structure in computer science.

Linked lists are invaluable for storing sequential data where order matters. Unlike arrays, which have a fixed size, linked lists are dynamic, making them memory-efficient for situations where data size can change.

Building a Python Class for Linked Lists

Let's walk through the creation of a Python class that encapsulates the magic of linked lists and explore its functionalities:

Node Class:

This class represents an individual node, the building block of our linked list. Each node has two primary attributes:

class Node:
    def __init__(self, data=None, next=None):
        self.data = data  # The data stored in the node
        self.next = next  # Pointer to the next node in the list        

  • data: Holds the actual data you want to store in the list.
  • next: Acts as a pointer, referencing the next node in the sequence, essentially chaining the nodes together to form the linked list structure.

LinkedList Class:

The LinkedList class manages the operations of the entire linked list:

class LinkedList:
    def __init__(self):
        self.head = None  # Head (starting point) of the list

    # Other methods (see below)        

  • init: Initializes an empty linked list by setting the head (the starting point) to None.

Key Methods of LinkedList Class:

  • print: Visualizes the contents of the linked list by iterating through each node and printing its data.
  • get_length: Calculates the total number of nodes in the linked list.
  • insert_at_beginning, insert_at_end, insert_at: Allows strategic insertion of new nodes at the beginning, end, or a specific index within the linked list.
  • remove_at: Removes a node at a particular index.
  • insert_values: Efficiently inserts a collection of values to create a linked list.

Putting It into Practice:

The main Block:

Here’s how we apply the LinkedList class methods:

  1. Linked List Creation and Fruit Basket Construction:

if __name__ == "__main__":
    ll = LinkedList()
    ll.insert_values(["banana", "mango", "grapes", "orange"])
        

2. Targeted Insertion:

    ll.insert_at(1, "blueberry")        

3. Strategic Removal:

    ll.remove_at(2)        

4. Visualizing the Transformed List:

    ll.print()  # Output: banana --> blueberry --> orange        

5. Numerical Playground: Building a List of Numbers:

    num_ll = LinkedList()
    num_ll.insert_values([1, 3, 5, 7, 9])        

Conclusion:

Linked lists are powerful tools in programming, offering flexibility and efficiency in managing sequential data. By understanding their structure and methods, you can leverage linked lists to build more dynamic and scalable applications.

Stay tuned for more insights into data structures and algorithms!

Full Code:

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

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

    def print(self):
        if self.head is None:
            print("Linked list is empty")
            return
        itr = self.head
        llstr = ''
        while itr:
            llstr += str(itr.data)+' --> ' if itr.next else str(itr.data)
            itr = itr.next
        print(llstr)

    def get_length(self):
        count = 0
        itr = self.head
        while itr:
            count+=1
            itr = itr.next

        return count

    def insert_at_begining(self, data):
        node = Node(data, self.head)
        self.head = node

    def insert_at_end(self, data):
        if self.head is None:
            self.head = Node(data, None)
            return

        itr = self.head

        while itr.next:
            itr = itr.next

        itr.next = Node(data, None)

    def insert_at(self, index, data):
        if index<0 or index>self.get_length():
            raise Exception("Invalid Index")

        if index==0:
            self.insert_at_begining(data)
            return

        count = 0
        itr = self.head
        while itr:
            if count == index - 1:
                node = Node(data, itr.next)
                itr.next = node
                break

            itr = itr.next
            count += 1

    def remove_at(self, index):
        if index<0 or index>=self.get_length():
            raise Exception("Invalid Index")

        if index==0:
            self.head = self.head.next
            return

        count = 0
        itr = self.head
        while itr:
            if count == index - 1:
                itr.next = itr.next.next
                break

            itr = itr.next
            count+=1

    def insert_values(self, data_list):
        self.head = None
        for data in data_list:
            self.insert_at_end(data)


if __name__ == '__main__':
    ll = LinkedList()
    ll.insert_values(["banana","mango","grapes","orange"])
    ll.insert_at(1,"blueberry")
    ll.remove_at(2)
    ll.print()

    ll.insert_values([45,7,12,567,99])
    ll.insert_at_end(67)
    ll.print()        


要查看或添加评论,请登录

Pranav K. J.的更多文章

社区洞察

其他会员也浏览了