Linked list using dstructure library in Python

The Dstructure library in Python provides efficient tools for implementing various data structures including linked lists. This library simplifies the creation and manipulation of linked lists with easy-to-use methods for common operations.

We'll explore how to create and work with linked lists using the Python Dstructure library, covering essential operations from basic creation to advanced manipulations.

What is a Linked List?

A linked list is a linear data structure where elements are not stored in consecutive memory locations. Instead, each node contains data and a pointer to the next node. This structure allows for dynamic memory allocation and efficient insertion/deletion operations.

Installation

First, install the Dstructure library using pip ?

pip install dstructure

Creating a Linked List

Let's start by creating a basic linked list and adding 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()
10 -> 20 -> 30

We import the LinkedList class from the dstructure library, create a new LinkedList object, add elements using append(), and print the list.

Removing Elements

The Dstructure library makes it easy to remove elements from a linked list ?

from dstructure import LinkedList

# Create and populate LinkedList
linked_list = LinkedList()
linked_list.append(10)
linked_list.append(20)
linked_list.append(30)

# Remove element
linked_list.remove(20)

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

Inserting at Specific Position

You can insert elements at specific positions in the linked list ?

from dstructure import LinkedList

# Create LinkedList
linked_list = LinkedList()
linked_list.append(10)
linked_list.append(30)

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

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

Checking Element Existence

Use the contains() method to check if an element exists in the linked list ?

from dstructure import LinkedList

# Create and populate LinkedList
linked_list = LinkedList()
linked_list.append(10)
linked_list.append(20)
linked_list.append(30)

# Check if elements exist
print("Contains 20:", linked_list.contains(20))
print("Contains 40:", linked_list.contains(40))
Contains 20: True
Contains 40: False

Getting Size and Clearing

You can get the size of the linked list and clear all elements ?

from dstructure import LinkedList

# Create and populate LinkedList
linked_list = LinkedList()
linked_list.append(10)
linked_list.append(20)
linked_list.append(30)

# Get size
print("Size:", linked_list.size())

# Clear the LinkedList
linked_list.clear()
print("Size after clearing:", linked_list.size())
Size: 3
Size after clearing: 0

Common Operations Summary

Operation Method Description
Add element append(value) Adds element to the end
Remove element remove(value) Removes first occurrence of value
Insert at position insert(index, value) Inserts value at specific index
Check existence contains(value) Returns True if value exists
Get size size() Returns number of elements
Clear all clear() Removes all elements

Conclusion

The Dstructure library provides an efficient and user-friendly way to work with linked lists in Python. It offers essential methods for creating, manipulating, and managing linked lists with clean, intuitive syntax that makes implementing this fundamental data structure straightforward for developers.

Updated on: 2026-03-27T08:21:10+05:30

257 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements