Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Linked List in Python

2 Replies, 2536 Views

[Image: BFMMlbcQvml9HSqXcvNp]
Singly Linked List:
          Hi guys, I hope you all are doing well. In python tutorials I have mentioned a number of data structures like Lists, Dictionary, etc but in this thread I am going to tell you about a user defined data structure known as Linked List. as In python’s list thread I mentioned that python does not support arrays built-in; it has lists instead of arrays. Arrays are contiguous memory locations. But Linked Lists can be random in memory. Linked lists consist of nodes and each node stores two things: first data of that node and the second address of the next node. 

[Image: Singly-Linked-List1.png]

So if we code a linked list we have two create two classes one for nodes and one for linked list itself the first node is called head of linked list and the last node does not store any address it is represented as null.

So now first of all we will create the classes for linked lists and its nodes.

Code:
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
class LinkedList:
    def __init__(self):
        self.head = None

     
So this is the basic structure of linked list in python

We can perform the following function to any of data structure we will apply it one by one
Traversing (Accessing each element of data structure)
Searching
Inserting
Deleting

So now first of all we will see how we can traverse each node of the linked list. So now I will create some nodes and link them and then I will traverse them.

Code:
def traversing(self):
        temp = self.head
        while temp:
            print(temp.data, end='\t')
            temp = temp.next


This method is used to traverse the code we created a temp variable and assign it to the first node of the class and then one by one we will go to each node of the linked list

Now we will talk about how we can insert data into start of linked list

Code:
# inserting at the start of linked list
    def insert_start(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node


In this way we can add nodes to end and at point in linked list

Code:
# inserting after a specified node
    def insert_after(self, prev, new_data):
        if prev is None:
            print("This node does not access.")
            return
        new_node = Node(new_data)
        new_node.next = prev.next
        prev.next = new_node

In this code first we will check that the node exists that we want to add our new node after that and then we will create our node.
And in last we will discuss about adding node at the end of linked list

Code:
# Inserting node at the end of Linked list
    def append(self, new_data):
        new_node = Node(new_data)
        # check if linked list is null or not
        if self.head is None:
            self.head = new_node
        temp = self.head
        while temp.next:
            temp = temp.next
     
        temp.next = new_node


In this method we will loop until the address specified by the node is Null in this way we can reach through the last address and we will assign it new_node

Now we will talk about the searching and deleting a specified node. In deleting a node in python we will take care of two things the node we are searching for it is first node or not
And this node exists or node. So now taking care of these things I am going to code a delete method for it and after that we will create a program that demonstrates all these methods at once. Once we get the node we want to delete then we will remove all its relations and assign it as null.

Code:
# deleting node
    def delete_node(self, key):
        # if the node we want to delete is head node
        temp = self.head
        if temp:
            if temp.data == key:
                self.head = temp.next
                temp = None
        # if it is not the head node
        while temp:
            if temp.data == key:
                break
            prev = temp
            temp = temp.next
        if temp is None:
            print("The node you wanted to delete does not exist.")
        prev.next = temp.next
        temp = None

Now I am going to give you all that code in form of one program so you can check how each these method works

Code:
# Creating a class for node
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
# Creating a class for Linked List
class LinkedList:
    def __init__(self):
        self.head = None
 
    # inserting at the start of linked list
    def insert_start(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node
 
    # inserting after a specified node
    def insert_after(self, prev, new_data):
        if prev is None:
            print("This node does not access.")
            return
        new_node = Node(new_data)
        new_node.next = prev.next
        prev.next = new_node
    # Inserting node at the end of Linked list
    def append(self, new_data):
        new_node = Node(new_data)
        # check if linked list is null or not
        if self.head is None:
            self.head = new_node
        temp = self.head
        while temp.next:
            temp = temp.next
     
        temp.next = new_node
    # deleting node

    def delete_node(self, key):
        # if the node we want to delete is head node
        temp = self.head
        if temp:
            if temp.data == key:
                self.head = temp.next
                temp = None
        # if it is not the head node
        while temp:
            if temp.data == key:
                break
            prev = temp
            temp = temp.next
        if temp is None:
            print("The node you wanted to delete does not exist.")
        prev.next = temp.next
        temp = None

     


    # Travering and printing each node of linked list
    def showInfo(self):
        temp = self.head
        while temp:
            print(temp.data, end='\t')
            temp = temp.next
 
# Initializing the nodes
number = LinkedList()
number.insert_start(12)
number.insert_start(32)
number.insert_after(number.head, 55)
number.append(66)
number.delete_node(66)
number.showInfo()


So that’s it for now 
"Keep Good care of your Health
Allah Hafiz and Good Bye Till the next post"
Love me like you do Heart
Thanks bro, I usually use List and ArrayList a lot in Java.

They are very flexible and easy to use
Rs
* Thankful to Allah *
Kurdy
(07-28-2022, 07:19 AM)Mr.Kurd Wrote: Thanks bro, I usually use List and ArrayList a lot in Java.

They are very flexible and easy to use

Obviously List is easy to use but Linked list has advantages in memory management
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,179 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,570 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,776 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,541 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,773 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,725 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,738 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,757 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,728 05-26-2022, 08:50 PM
Last Post: Covid-19
  Tutorial Python 3.1: OOP Covid-19 2 1,559 05-24-2022, 06:37 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)