Singly Linked List Using Python














































Singly Linked List Using Python



Why we use Singly Linked List :


   -->   In the linked list we can dynamically access the memory, through which we can save 

            a lot of memory.


What we can do in Singly Linked List :



   -->   Insert elements in different ways:


           1.  Add elements when the singly Linked List is empty.


               -   Firstly we check the length of the linked list if it is empty then create a newnode,


                    and update      self.head = newnode


 


           2. Add elements at the beginning of the Singly Linked List.   


               -   Firstly we check the length of the linked list, if empty then we call the add_empty()

                    function if not then we change.       node.next = self.head


                                                                            self.head = newnode 


 


          3. Add elements at the end of the Singly Linked List.


               -  Firstly we check the length of the linked list, if empty then we call the add_empty()

                    function if not then we go to the last node using a while loop and change the last

                    node value.


 


           4.  Add elements after the x Element.


                -  Checking the length of the linked list, if empty then we call the add_empty function,

                   if not then we check whether that the node which we are going to insert will be the

                   first node if true, then we call the add_begain() function, if not then we use a while

                    loop to reach that node that contains the element x and changes the required values.


 


            5.  Add elements before the x Element.


                 -  Checking the length of the linked list is our parity, it is true then we call add_empty()

                    function,if not then we check whether the inserted node is the last node, if true then 

                    we call the add_end() function if not then we use a while loop to reach that node that

                    contains the X element and changes the required values.


 


   -->   Delete elements in different ways :

 


             1.  Delete elements from the beginning.


               -  Checking the length of the linked list is common in every function, if not then we 

                   change the value of the head. (i.e)          self.head = self.head.next



             2.  Delete elements from the end.


                 -   Check the length of the linked list if true then we simply return and if not then

                      we use a while loop to reach the previous value of the end and change the

                      required value.


 


             3.  Delete elements by values.


               -   Check whether the element is present in the linked list, if not then check 

                    whether the deleted node is the first node if true then we call the delete_begain()

                    function, if the deleted node is last then we call delete_end(). If all these are 

                    false then we use a while loop to reach that point and change the required value.



   -->   Print elements :



              1. Forward Direction.

                  -  The way we have stored the value in the linked list is the same way we fetch

                       the value of the linked list. Just by running a while loop and passing the

                       head value until the node.next not become None.


                2. Reverse Direction.  

                  -   We can print the value just opposite the way we have stored it. For reversing we

                       need three-pointer which points the certain nodes and by updating some values 

                        of the node, we can reverse the linked list and print it.



 Requirements :


   -->   Python must be installed in your working system.


   -->   Use Jupiter Notebook.


 


Programming Language :


   


   -->  The language which we have used here in this program is PYTHON.


   -->  As we have used classes and objects here so prior knowledge is a must.



Programming Code

        

 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

class LinkedList:
def __init__(self):
self.head=
None

# Before printing the value of the linked List we check if whether the
# element is present in the linked list or if.
# If the element is found then we print the Value if not we simply
# print the list is empty.



def add_begain(self,data):
newNode=Node(data)
newNode.next=self.head
self.head=newNode


# Add begin defines the function which adds the value to the new
# beginning Node.

def add_end(self,data):
newNode=Node(data)
if self.head is None:
self.head=newNode
else:
n=self.head
while n.next is not None:
n=n.next
n.next = newNode

def add_after(self,data,x):
n=self.head
while n is not None:
if x==n.data:
break
n=n.next
if n is None:
print(
"node is not present in linked list")
else:
newNode=Node(data)
newNode.next=n.next
n.next=newNode

def add_before(self,data,x):
n=self.head
if n is None:
print(
"Linked List Is empty")
return
if n.data == x:
newNode=Node(data)
newNode.next=self.head
self.head=newNode
return
while n.next is not None:
if n.next.data is x:
break
n=n.next
if n.next is None:
print(
"Node Is None")
else:
newNode=Node(data)
newNode.next=n.next
n.next=newNode

def add_empty(self,data):
if self.head is None:
newNode=Node(data)
self.head=newNode
else:
print(
"list is empty")

def delete_begain(self):
if self.head is None:
print(
"The linked listis empty")

else:
self.head=self.head.next

def delete_end(self):
n=self.head
if n is None:
print(
"List is empty")
else:
while n.next.next is not None:
n=n.next
n.next=
None
def delete_value(self,x):
n=self.head
if n is None:
print(
"List is empty")
return
if n.data == x:
n=n.next
return
while n.next is not None:
if x==n.next.data:
break
n=n.next
if n.next is None:
print(
"No node is present")

else:
n.next=n.next.next


def print_count(self):
n=self.head
count=
0
while n is not None:
count+=
1
n=n.next
print(count)



# As the head has no value when the linked list is empty so we
# assign it here head=None.


def print_list(self):
if self.head is None:
print(
"The list is empty")
else:
n=self.head
while n is not None:
print(n.data,
"----->",end=" ")
n=n.next



def reverse(self):
prev=
None
current = self.head
while(current is not None):
next = current.next
current.next = prev
prev = current
current = next 

self.head = prev   



ll1=LinkedList()

ll1.add_empty(5) ll1.print_list() ll1.add_begain(10) ll1.print_list() ll1.add_end(100) ll1.print_list() ll1.add_after(500,10) ll1.print_list() ll1.add_before(25,10) ll1.print_list() ll1.delete_begain() ll1.print_list() ll1.delete_end() ll1.print_list() ll1.delete_value(5) ll1.print_list() ll1.print_count()


     

 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  Output :

      5 -----> 

10 -----> 5 -----> 

10 -----> 5 -----> 100 -----> 

10 -----> 500 -----> 5 -----> 100 -----> 

25 -----> 10 -----> 500 -----> 5 -----> 100 -----> 

10 -----> 500 -----> 5 -----> 100 -----> 

10 -----> 500 -----> 5 -----> 

10 -----> 500 -----> 

2