LinkedList : Simple and efficient way to access LinkedList in python
What is it ?
The LinkedList package offers a robust and efficient implementation of a singly linked list data structure in Python. Unlike raw linked lists, this package provides convenient access to elements using indices, allowing for seamless integration with Python's iterable features, such as for loops and other iterable functions. With this package, you can create linked lists and effortlessly access and modify their elements just like you would with lists or tuples.
The primary goal of this package is to bridge the gap between raw linked lists and Python's built-in data structures, making linked lists as user-friendly and versatile as possible. By using this package, you can harness the power of linked lists while enjoying the familiar functionalities offered by Python's native data structures.
Table of Contents
Project Architecture
WORKSPACE /
|
|--> LinkedList /
| |
| |--> __init__.py
| |
| |--> base_list.py
| |
| |--> node.py
| |
| |--> linked_list.py
|
|--> tests /
| |
| |--> linked_list_test.py
|
|--> .gitignore
|
|--> LICENSE
|
|--> logo.png
|
|--> README.md
|
|--> requirements.txt
|
|--> setup.py
Features
Here are some key features of LinkedList provided by this package :
- Create a new linked list.
- Setting element with indexing.
- Print linked list using print function.
- Insert element in an existing linked list.
- Get the length of the linked list and reverse it.
- Sort the linked list in ascending or descending order.
- Delete elements(Nodes) from the linked list (using index).
- Perform concatenation of two or more LinkedLists (using '+').
- Insert element in an existing linked list at any index of linked list.
- Access and update elements using index (Just like noraml list in python).
- Perform element-wise division with a numeric value (for numeric linked list).
- Perform element-wise multiplication with a numeric value (for numeric linked list).
Installation
You can install LinkedList package using pip :
pip install LinkedList_575
Usage
The linked list is present in linked_list.py file of LinkedList package. To use linked list in your project, you needed to import. You can import it as follows ;
from LinkedList.linked_list import LinkedList
CLICK HERE FOR CODE'S OUTPUT
Creating a linked list
Now , let's create an object of linked list using LinkedList module. It will create an empty linked list :
linked_list = LinkedList()
CLICK HERE FOR CODE'S OUTPUT
Inserting Elements
We have created an empty linked list. Now , we can add elements using insert()
method :
linked_list.insert(10)
linked_list.insert(30, 1)
linked_list.insert(20, 0)
linked_list.insert(40,-1)
linked_list.insert(-8,-2)
CLICK HERE FOR CODE'S OUTPUT
Printing Linked List
Now we can print each node of linked_list just using print function :
print(linked_list)
CLICK HERE FOR CODE'S OUTPUT
Accessing Elements
Now we can access each elements of linked_list present at any node using its index(i.e writing index in square braces) as we are accessing in case of normal list in python :
print(linked_list[2])
print(linked_list[-1])
print(linked_list[-2])
print(linked_list[0])
print(linked_list[-4])
CLICK HERE FOR CODE'S OUTPUT
Access Using for Loop
n = len(linked_list)
for i in range(n) :
print(linked_list[i])
CLICK HERE FOR CODE'S OUTPUT
Linked List as an Iterator
As we know that it is very convinient to use and access the element of data structure by using for loop. But in case of linked list we can not directly access elements(data present at the node) using for loop. To solve this problem, I have used __iter__
and __next__
dunder method to make it irerable.
for data in linked_list :
print(data)
CLICK HERE FOR CODE'S OUTPUT
Updating Elements
Now we can update each elements of linked_list present at any node using its index(i.e writing index in square braces) and assignment as we are updating in case of normal list in python :
linked_list[-1] = 25
linked_list[2] = 50
linked_list[0] = 108
linked_list[-2] = -9
linked_list[-4] = 0
CLICK HERE FOR CODE'S OUTPUT
Now , let's see the updated linked_list :
print(linked_list)
CLICK HERE FOR CODE'S OUTPUT
Concatenation of Linked List
We can perform concatenation operation of two or more linked lists by just using '+' operator between them, let's see the example ;
linked_list1 = LinkedList()
linked_list2 = LinkedList()
for i in range(6) :
linked_list1.insert(i , 0)
for i in range(5) :
linked_list2.insert(-(i+1) , 0)
CLICK HERE FOR CODE'S OUTPUT
print('linked_list1 =' ,linked_list1)
print('linked_list2 =' ,linked_list2)
CLICK HERE FOR CODE'S OUTPUT
result1 = linked_list1 + linked_list2
result2 = linked_list2 + linked_list1
CLICK HERE FOR CODE'S OUTPUT
print('result1 =' , result1)
print('result2 =' , result2)
CLICK HERE FOR CODE'S OUTPUT
result3 = linked_list1 + linked_list2 + linked_list
print('result3 =' , result3)
CLICK HERE FOR CODE'S OUTPUT
Basic Operations
In this section, we will perform some basic operations in linked list like finding length , reversing the node and sorting etc.
Finding Length
As in case of python list or tuple or any other iterable it is an important task to find the number of elements present in the data structure. As finding the number of elements can be very important insight of data structure like we can iterate over the length and so on. We can find the number of elements by using len()
funtion and we can pass linked list as as argument. This function len()
will return the number of elements present in the linked list or we can say that it is a length of linked list.
print(len(linked_list ))
print(len(linked_list1))
print(len(linked_list2))
print(len(result1))
print(len(result2))
print(len(result3))
CLICK HERE FOR CODE'S OUTPUT
Reversing Linked List
We have performed reverse operations many time in case of python iterables like list or tuple. Similarly, we can use the same function that is reversed()
to reverse node of linked list. In this function reverse()
we need to pass linked list as an argument.
print('Before reversing : linked_list =' , linked_list)
reversed(linked_list)
print('After reversing : linked_list =' , linked_list)
CLICK HERE FOR CODE'S OUTPUT
print('Before reversing : linked_list1 =' ,linked_list1)
reversed(linked_list1)
print('After reversing : linked_list1 =' ,linked_list1)
CLICK HERE FOR CODE'S OUTPUT
print('Before reversing : linked_list2 =' ,linked_list2)
reversed(linked_list2)
print('After reversing : linked_list2 =' ,linked_list2)
CLICK HERE FOR CODE'S OUTPUT
print('Before reversing : result1 =' , result1)
reversed(result1)
print('After reversing : result1 =' , result1)
CLICK HERE FOR CODE'S OUTPUT
print('Before reversing : result2 =' , result2)
reversed(result2)
print('After reversing : result2 =' , result2)
CLICK HERE FOR CODE'S OUTPUT
print('Before reversing : result3 =' , result3)
reversed(result3)
print('After reversing : result3 =' , result3)
CLICK HERE FOR CODE'S OUTPUT
Multiplying and Dividing linked list with numeric value
We know that in numpy we can multilpy or divide array(numeric) with any numeric value, as a result the we will get an array in which all the elements of numpy array will be multiplied or divided by the number we have provided. Same operation can be performed on this LinkedList
print('Before multiplication : linked_list =' , linked_list)
linked_list*2
print('After multiplication : linked_list =' , linked_list)
CLICK HERE FOR CODE'S OUTPUT
print('Before division : linked_list =' , linked_list)
linked_list / 2
print('After division : linked_list =' , linked_list)
CLICK HERE FOR CODE'S OUTPUT
Sorting Linked Lists
Sorting is one of the most important part in case of oterables. As we have many options to sort LinkedList. But, here we will use merge sort that will provide time complexity of nlog(n) and space complexity is constant.
Sorting Linked Lists in ascending order
print('Before sorting : linked_list2 =' , linked_list2)
linked_list2.sort()
print('After sorting : linked_list2 =' , linked_list2)
CLICK HERE FOR CODE'S OUTPUT
Sorting Linked Lists in descending order
print('Before sorting : linked_list1 =' , linked_list1)
linked_list1.sort(reverse = True)
print('After sorting : linked_list1 =' , linked_list1)
CLICK HERE FOR CODE'S OUTPUT
Contributing
Contributions to the LinkedList package are welcome! If you find any bugs or have suggestions for improvement, please open an issue or submit a pull request on GitHub.
License
© 2023 Saqib Shaikh
This package is distributed under the GNU General Public License v3.0 (GPLv3) License. See the LICENSE file for more details.