Start working on Single linked list in Python
Working on Single linked list in python can be easy using py-linkedlist python package
Ever wonder what is a linked list ? why we need linked list apart from inbuilt data types like arrays, lists ? Let’s Understand from scratch in this tutorial. Here we are learning only about Single linked list
Note:- If you already aware of linked list concepts, Please go to package installation section.
Linked list:
Let’s understand about Node first.
Node is a data structure which used to store data. Additionally it also stores the address of another node. It contains two continuous blocks i.e., data block and link(next) block. Link(next) block is used to store address of another node and None in case of single Node.
Linked list is a data storing structure and a collection of nodes. The first element is called head node, and last element is called tail node.
Single Linked list contains multiple nodes and each node stores address of next node.
Linked list vs list
List in python stores data in continuous blocks of memory.
It is main disadvantage because it requires continuous space in the memory and imagine storing thousand of elements in list requires a large amount of free continuous space.
Linked list stores the data in nodes at random blocks in the memory. So it doesn’t requires continuous space anymore.
It will find free space at random address in the memory, saves the node and address of next node.
But the disadvantage of linked list is it doesn’t support indexing like in list. How to access the elements in linked list , we see in the below code section.
Installing py-linkedlist python package
Using pip, we can install py-linkedlist package.
Importing the linkedlist package
Initialising linked list object
Adding Elements to the linked list
- add(value) : Append data to the linked list. we can add various data types like list, tuples, string, dictionary.
2. addAtHead(value) : To insert data as the head element in the linked list
Length of linked list
use length() : Returns the length of the linked list
Printing the linked list
use show() : To print linked list
Removing elements from the linked list
- removeElement(value) : Remove the first occurrence of the value.
2. removeAtLoc(position) : Remove element from a position. Here we are following 0-based indexing which means first element position is 0.
3. removeHead() : Remove head element from the linkedlist
4. removeTail() : Remove tail element from the linkedlist
5. removeAll() : Remove all elements from the linkedlist
To check the linkedlist is empty
use isEmpty() : Returns True if linkedlist is empty or else it returns False
Indexing Linkedlist element with location
use eleAt(position) : Returns the value at the given position from the linkedlist
Uninstalling the package
Conclusion:
Linked lists are more preferable than list, let’s try to implement linked list using py-linkedlist package.
References:
[1] Refer https://pypi.org/project/py-linkedlist for documentation and Code snippets.