Up until a long time Python had no way of executing linked list data structure. It does support list but there were many problems encountered when using them as a concept of the linked list like list are rigid and are not connected by pointers hence take a defined memory space that may even be wasted if the list is not fully filled. In fact, to overcome the problems possessed by list Python's dequeue data structure could also be employed to work like a linked list. But all of this has been covered because of llist.
llist module in Python
llist is an extension module of CPython that provides a basic linked list structure. They are significantly faster than dequeue and even the standard list for that matter.
Installation
To make use of benefits provided by llist it has to be installed like any other python extension or module, using pip. The following command will do the job.
pip install llist
If not for this, it can be manually downloaded from http://pypi.python.org/pypi , then unpack resources and compile them with “python setup.py install”. Once the module is successfully installed you only need to import this module whenever needed.
Methods provided
Currently, llist provides the following two types of linked lists:
- singly linked list(sllist) : a linked list in which the nodes point to the node next to it.
- doubly linked list(dllist) : a linked list in which the nodes point to the node after it as well as to the one before it.
This module contains following objects:
- dllist : object to implement doubly linked list
- dllistnode : returns a new doubly linked list node, initialized optionally
- dllistiterator: an iterator object for dllist
- sllist : object to implement singly linked list
- sllistnode : a singly linked list node, initialized optionally
- sllistiterator : an iterator object for sllist
The following examples will help you understand better. They give basic ideas about the execution of the two types of list supported by llist: Example 1: sllist
Python3
# importing packages
import llist
from llist import sllist, sllistnode
# creating a linked list
lst = sllist(['first', 'second', 'third'])
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
# adding and inserting values
lst.append('fourth')
node = lst.nodeat(2)
lst.insertafter('fifth', node)
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
# popping a value
# i.e. removing the last entry of the list
lst.pop()
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
# removing a specific element
node = lst.nodeat(1)
lst.remove(node)
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
Output:
sllist([first, second, third])
sllistnode(first)
sllistnode(third)
3
sllist([first, second, third, fifth, fourth])
sllistnode(first)
sllistnode(fourth)
5
sllist([first, second, third, fifth])
sllistnode(first)
sllistnode(fifth)
4
sllist([first, third, fifth])
sllistnode(first)
sllistnode(fifth)
3
Example 2: dllist
Python3
# importing packages
import llist
from llist import dllist, dllistnode
# creating a linked list
lst = dllist(['first', 'second', 'third'])
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
# adding and inserting values
lst.append('fourth')
node = lst.nodeat(2)
lst.extendleft(['fifth', 'sixth'])
new_node = dllistnode('seventh')
ref_node = lst.nodeat(2)
lst.insertnode(new_node, ref_node)
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
# popping a value
# i.e. removing the last entry of the list
lst.pop()
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
# removing a specific element
node = lst.nodeat(1)
lst.remove(node)
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
Output:
dllist([first, second, third])
dllistnode(first)
dllistnode(third)
3
dllist([sixth, fifth, seventh, first, second, third, fourth])
dllistnode(sixth)
dllistnode(fourth)
7
dllist([sixth, fifth, seventh, first, second, third])
dllistnode(sixth)
dllistnode(third)
6
dllist([sixth, seventh, first, second, third])
dllistnode(sixth)
dllistnode(third)
5
Similar Reads
Inspect Module in Python
The inspect module in Python is useful for examining objects in your code. Since Python is an object-oriented language, this module helps inspect modules, functions and other objects to better understand their structure. It also allows for detailed analysis of function calls and tracebacks, making d
4 min read
Python Module Index
Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
Import module in Python
In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
Python-interface module
In object-oriented languages like Python, the interface is a collection of method signatures that should be provided by the implementing class. Implementing an interface is a way of writing an organized code and achieve abstraction. The package zope.interface provides an implementation of "object in
3 min read
Python Falcon - Inspect Module
In the world of web development, Python Falcon is a popular framework known for building high-performance APIs. One of the key components that makes Falcon powerful is its inspect module. This module provides utilities for introspection, which means examining the internal properties of Python object
3 min read
Built-in Modules in Python
Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext
9 min read
Python Math Module
Math Module consists of mathematical functions and constants. It is a built-in module made for mathematical tasks. The math module provides the math functions to deal with basic operations such as addition(+), subtraction(-), multiplication(*), division(/), and advanced operations like trigonometric
13 min read
Python Fire Module
Python Fire is a library to create CLI applications. It can automatically generate command line Interfaces from any object in python. It is not limited to this, it is a good tool for debugging and development purposes. With the help of Fire, you can turn existing code into CLI. In this article, we w
3 min read
Keyword Module in Python
Python provides an in-built module keyword that allows you to know about the reserved keywords of python. The keyword module allows you the functionality to know about the reserved words or keywords of Python and to check whether the value of a variable is a reserved word or not. In case you are una
2 min read
dllist class of llist module in Python
llist is an extension module of CPython that provides a basic linked list structure. They are significantly faster that dequeue and even the standard list for that matter. Doubly Linked List It is a type of linked list in each node stores data as well as two addresses (address of nodes succeeding an
4 min read