Linked list using dstructure library in Python


Many different libraries are available in Python that offer effective tools for managing, manipulating, and analysing data. The Dstructure library is just one of them. There are many different data structures in this package that are simple to construct. This article serves as an introduction to linked list management using the Dstructure library.

We'll begin with a fundamental overview of linked lists before moving on to how to generate and work with them using the Python Dstructure package. You need to feel at ease using the Dstructure library to operate on linked lists by the time you finish reading this article.

What is a Linked List?

A linked list is a linear data structure used in computer science where the components are not kept in consecutive memory regions. A linked list's elements are connected via pointers. A linked list's nodes each have two components: the data and a pointer to the node after it.

Introduction to Dstructure Library

Linked Lists, Stacks, Queues, Binary Trees, and other data structures can all be implemented using the open-source Python Dstructure library. These data structures can be handled quickly and easily thanks to the Dstructure library.

We must first install the Dstructure library before we can utilise it. Pip can be used to install it.

pip install dstructure

Implementing Linked List using Dstructure Library

Let's look at how we may create a linked list using the Dstructure library.

Example 1: Creating a Linked List

Let's start by making a basic linked list and adding some elements to it.

from dstructure import LinkedList

# Create a LinkedList
linked_list = LinkedList()

# Add elements
linked_list.append(10)
linked_list.append(20)
linked_list.append(30)

# Print LinkedList
linked_list.print_list()  # Output: 10 -> 20 -> 30

We begin by importing the LinkedList class from the dstructure library in our example. Then, after adding a few entries to the newly created LinkedList object, we print it.

Example 2: Removing an Element from a Linked List

Additionally, Dstructure makes it simple to remove items from a linked list. Let's look at the procedure.

from dstructure import LinkedList

# Create a LinkedList
linked_list = LinkedList()

# Add elements
linked_list.append(10)
linked_list.append(20)
linked_list.append(30)

# Remove element
linked_list.remove(20)

# Print LinkedList
linked_list.print_list()  # Output: 10 -> 30

In this illustration, the linked list was expanded to include the numbers 10, 20, and 30. The output was 10 -> 30 after we deleted element 20 from the list and printed the list.

Example 3: Inserting an Element at a Specific Position in a Linked List

Additionally, we can place elements in the linked list at particular locations.

from dstructure import LinkedList

# Create a LinkedList
linked_list = LinkedList()

# Add elements
linked_list.append(10)
linked_list.append(30)

# Insert element at position 1
linked_list.insert(1, 20)

# Print LinkedList
linked_list.print_list()  # Output: 10 -> 20 -> 30

Example 4: Checking if an Element Exists in a Linked List

The includes method makes it simple to determine whether an element is present in the linked list.

from dstructure import LinkedList

# Create a LinkedList
linked_list = LinkedList()

# Add elements
linked_list.append(10)
linked_list.append(20)
linked_list.append(30)

# Check if element exists
print(linked_list.contains(20))  # Output: True
print(linked_list.contains(40))  # Output: False

In this case, we built a linked list and added a few items to it. The contains method was then used to determine whether specific elements were present in the linked list.

Example 5: Getting the Size of a Linked List

The size method can be used to determine the linked list's element count.

from dstructure import LinkedList

# Create a LinkedList
linked_list = LinkedList()

# Add elements
linked_list.append(10)
linked_list.append(20)
linked_list.append(30)

# Get the size of LinkedList
print(linked_list.size())  # Output: 3

In this example, a linked list was built, a few elements were added, and the size technique was used to determine how many elements were in the linked list.

Example 6: Clearing a Linked List

The clear method can be used to delete every element from a linked list.

from dstructure import LinkedList

# Create a LinkedList
linked_list = LinkedList()

# Add elements
linked_list.append(10)
linked_list.append(20)
linked_list.append(30)

# Clear the LinkedList
linked_list.clear()

# Print LinkedList
linked_list.print_list()  # Output: None

Here, we established a linked list, filled it with elements, and then deleted everything from it. When the list is printed after being cleared, the result is None, indicating the list is now empty.

Conclusion

We have now looked at a number of the most important actions that the Python Dstructure library can perform on linked lists. We have seen everything from constructing a linked list to adding elements, removing elements, inserting components at certain points, determining the size of the linked list, and clearing the linked list.

This library offers an effective method for handling linked lists in Python, simplifying the implementation and usage of this crucial data structure. It is a fantastic tool to have in your toolkit if you are a Python developer and working with intricate data structures.

Updated on: 18-Jul-2023

36 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements