DEV Community

221910301042
221910301042

Posted on

How to insert elements in a doubly linked list

To insert an element in a doubly linked list, we first need to create a list:

class Nodeq:
  def __init__(self, data):
    self.data = data
    self.next = None
    self.prev = None
Enter fullscreen mode Exit fullscreen mode

There are three ways of inserting an element into a doubly linked list:

  • insert_beginning
  • insert_end
  • insert_after

Insert beginning

Insert beginning is a method to include a new_node in front of the head node and represent new_node as the new head node.

def insert_beginning(self,data): 
  new_node = Nodeq(data)#creating a node with the given data
  if(self.head == None):
    self.head = new_node;    
    return
  self.head.prev = new_node
  new_node.next = self.head
  self.head = new_node #making the new_node as the new head node
Enter fullscreen mode Exit fullscreen mode

Insert end

With the help of insert_end, we can insert elements at the end of the doubly linked list.

def insert_end(self, new_data):
  new_node = Nodeq(new_data)
  if self.head is None:#checking if the DLL exists
    new_node.prev = None
    self.head = new_node
    return
  last = self.head
  while last.next:#to asigning the last node to the "last"
    last = last.next
  last.next = new_node
  new_node.prev = last#inserting the new node at the end of the DLL
  return
Enter fullscreen mode Exit fullscreen mode

Insert after

With the help of insert_after we can insert after a given element.

prev_data is the given element.

def insert_after(self, prev_data, data):
  h=self.head
  while h:#searching for the given data
    if prev_data == h.data:
      break
    h=h.next
  if h is None:
    print("The previous data is not present in the DLL")
    return
  new_node = Nodeq(data)
  # the new_node is inserted after the given node
  new_node.next= h.next
  h.next=new_node
  return
Enter fullscreen mode Exit fullscreen mode

To print the elements in doubly linked list we use Count_Display method.

def Count_Display(self): 
  if self.head==None: # to check if DLL exists
    print("We need a DLL to print")
    return
  temp = self.head 
  count=0
  while temp: 
    print (temp.data)#printing node data
    count+=1 
    temp = temp.next
  return count
Enter fullscreen mode Exit fullscreen mode

Code

The final code for the insertion of elements in the Doubly Linked List is shown below.

class Nodeq: 
    def __init__(self, data): 
        self.data = data 
        self.next = None
        self.prev = None

class DoublyLinkedList: 

    def __init__(self): 
        self.head = None

    def Count_Display(self): 

        if self.head==None:
            print("We need a DLL to print")
            return
        temp = self.head 
        count=0
        while temp: 
            print (temp.data)
            count+=1 
            temp = temp.next
        return count

    def insert_begining(self,data):
        new_node = Nodeq(data)    
        if(self.head == None):    
            self.head = new_node  
            return    
        self.head.prev = new_node    
        new_node.next = self.head    
        self.head = new_node

    def insert_end(self, new_data): 
        new_node = Nodeq(new_data) 
        if self.head is None: 
            new_node.prev = None
            self.head = new_node 
            return 
        last = self.head 
        while last.next: 
            last = last.next
        last.next = new_node 
        new_node.prev = last 

        return

    def insert_after(self, prev_data, data): 

        h=self.head
        while h:
            if prev_data == h.data:
                break
            h=h.next
        if h is None:
            print("The previous Node data must be in the DLL")
            return

        new_node = Nodeq(data)
        new_node.next= h.next
        h.next=new_node
        return
Dllist = DoublyLinkedList()
Dllist.insert_end(2) 
Dllist.insert_begining(3)
Dllist.insert_begining(4)
Dllist.insert_end(5) 
Dllist.insert_after(2,0)
print("The number of elements in the DLL list are :",Dllist.Count_Display())
Enter fullscreen mode Exit fullscreen mode

Top comments (0)