Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
community creator

How to search a doubly linked list

P.Praneet

In order to search a Doubly Linked List(DLL) we, first, need a list:

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

Now, we insert the elements into DLL from the definitions insert_beginning and insert_end.

  • insert_beginning adds a new node element to the beginning of the DLL.
  • insert_end adds a new node element at the end of the DLL.
#Inside the class Dll

    def insert_beginning(self,data):
        new_node = Nodeq(data)  
        if(self.head == None): #incase of no Dll create new head   
            self.head = new_node     
            return    
        self.head.prev = new_node   
        new_node.next = self.head   
        self.head = new_node    # shifting head to new node

    def insert_end(self, new_data): 
        new_node = Nodeq(new_data) 
        if self.head is None: #incase of no Dll create new head   
            new_node.prev = None
            self.head = new_node 
            return 
        last = self.head 
        while last.next: 
            last = last.next
        last.next = new_node #inserting the new node
        new_node.prev = last 

Next, we check for the presence of the given element and compare it with elements in the DLL.

The data element is required to check.

temp = self.head
while temp:#To check all Dll elements
    if temp.data==data:
        break
        temp = temp.next
  • If the element is not found, then the given statement is printed, and False is returned.

  • If the element is found, then True is returned.

if gap==None:
  print("The given data doesnt exist")
  return False
return True

Hence, we get the final code that we can use with other implementations.

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

class DoublyLinkedList: 

    def __init__(self): 
        self.head = None
    def insert_beginning(self,data):
        new_node = Nodeq(data)  
        if(self.head == None): #incase of no Dll create new head   
            self.head = new_node     
            return    
        self.head.prev = new_node   
        new_node.next = self.head   
        self.head = new_node    # shifting head to new node

    def insert_end(self, new_data): 
        new_node = Nodeq(new_data) 
        if self.head is None: #incase of no Dll create new head   
            new_node.prev = None
            self.head = new_node 
            return 
        last = self.head 
        while last.next: 
            last = last.next
        last.next = new_node #inserting the new node
        new_node.prev = last 
    def search(self,data):
        temp = self.head
        while temp:
            if temp.data==data:
                break
            temp = temp.next
        if temp==None:
            print("The given data doesnt exist:")
            return False
        return True
#Test case
Dllist = DoublyLinkedList() 
Dllist.insert_beginning(2)
Dllist.insert_end(0)
print(Dllist.search(0)) # 0 is present in Dll
print(Dllist.search(3)) # 3 is not present in Dll   
Code to implement

RELATED TAGS

python
community creator
RELATED COURSES

View all Courses

Keep Exploring