Linked List Program in Python – Create Linked List, Add Node, Iterrate and Print Data

Lets try to create Linked List in Python as part of out Data structure Tutorial.

Problem Statement 

Create Linked List Basic Program in Python.

Solution

Lets understand the Very basic about Linked List –

  • Linked List are dynamic array type data structure where nodes and allocated on the fly i.e. dynamically and address/link or next node is maintained by reference/pointer in previous node so that it can be iterated.
  • Linked List has two field – Data and Next  –
    Linked List Node {
    Data ;  // Data is being stored here
    Next ;  // It contains the reference to next node or empty if if last node.
    }
  • There is Root Node which is like starting node which is kept at the first node always for iteration.
  • Linked List Operations : 
    • Adding New Node -New node is added when new data is added. Create new Node and set it at the end of the linked list.
    • Printing List Data – Start printing data from root till last node, we traverse through each node and move forward next pointer from previous node to next.
    • Delete the Node – Deletion node from Linked List is like removing link of the node which has to be deleted. Keep a reference to previous node of the data and once you find the node unlink it and point previous node pointer to next of data node.

Python Program

Here is standalone python program which creates basic linked list, add 5 nodes and print the data.

 

from astropy.extern.ply.cpp import xrange

class Node:
    
    def __init__(self , data):
        self.data =data;
        self.next = None
        
class linkedList:
    
    root = None 
    
    def __init__(self):
        pass
    
    def printdata ( self ):
        temp = self.root
        while( temp != None):
            print (temp.data)
            temp = temp.next
            
    def addNewNode(self , Node):
        if self.root == None :
            self.root = Node
        else :
            next = self.root
            prev = next
            while(next != None):
                prev= next
                next = next.next
            prev.next = Node
            
    def deleteNode (self, data):
        temp = self.root
        prev = temp
        while (temp != None):
            if temp.data == data:
                prev.next = temp.next
                break
            prev = temp
            temp=temp.next
        
            
                
if __name__ == '__main__':
    
    ll = linkedList()
    input = [1,2,3,4,5]
    
    for i in xrange(len(input)):
        ll.addNewNode(Node(input[i]))
        
    ll.printdata()
 
    #delete node
    ll.deleteNode(4)
    ll.printdata()

 

Let us know if you find any issue with above program.

tags : basic linked list program in python, linked list program in python, python linked list program, create linked list , linked list in python, basic linked list program, data structure in python, python ds programs, linkedlist delete node, ll program in python.

Leave a Comment

%d bloggers like this: