Deletion and Searching in Doubly Linked List in DSA Python
by DataFlair Team
Program 1
# Implementation of Doubly Linked List
class Node:
def __init__(self):
self.ladd=None
self.data=None
self.radd=None
class DoubleLinkedList:
def __init__(self):
self.start=None
self.count=0
# Create method of list
def createList(self):
n=int(input("Enter an element"))
self.start=Node()
self.start.ladd=None
self.start.data=n
self.start.radd=None
temp=self.start
self.count=self.count+1
choice=input("Want to continue")
while(choice=="Y" or choice=="y"):
n=int(input("Enter next element"))
newnode=Node()
newnode.ladd=None
newnode.data=n
newnode.radd=None
temp.radd=newnode
newnode.ladd=temp
temp=temp.radd
self.count=self.count+1
choice=input("Want to continue")
# Display method of list
def displayList(self):
if(self.start==None):
print("List not found")
else:
temp=self.start
while(temp!=None):
print(temp.data,end=" ")
temp=temp.radd
print("\nTotal Node is : ",self.count)
# Method for insert first
def insertFirst(self):
if(self.start==None):
print("List not found")
else:
n=int(input("Enter an element for insert"))
newnode=Node()
newnode.ladd=None
newnode.data=n
newnode.radd=None
newnode.radd=self.start
self.start.ladd=newnode
self.start=newnode
self.count=self.count+1
# Method for insert node at last position
def insertLast(self):
if(self.start==None):
print("List not found")
else:
n=int(input("Enter an element for insert at last"))
newnode=Node()
newnode.ladd=None
newnode.data=n
newnode.radd=None
last=self.start
while(last.radd!=None):
last=last.radd
last.radd=newnode
newnode.ladd=last
self.count=self.count+1
#Method for insert middle position
def insertMiddle(self):
if(self.start==None):
print("List not found")
else:
n=int(input("Enter an element for insert at middle"))
newnode=Node()
newnode.ladd=None
newnode.data=n
newnode.radd=None
pos=int(input("Enter middle position"))
if(pos>self.count):
print("Invalid position total node only: ",self.count)
else:
i=1
next=self.start
while(i<pos):
prev=next
next=next.radd
i=i+1
prev.radd=newnode
newnode.ladd=prev
newnode.radd=next
next.ladd=newnode
self.count=self.count+1
# Method for delete First element
def deleteFirst(self):
if(self.start==None):
print("List not found")
else:
temp=self.start
self.start=self.start.radd
print("Deleted node is : ",temp.data)
temp.ladd=None
temp.radd=None
temp=None
self.count=self.count-1
# Method for delete Last element
def deleteLast(self):
if(self.start==None):
print("List not found")
else:
last=self.start
while(last.radd!=None):
last=last.radd
temp=last.ladd
print("Deleted node is: ",last.data)
self.count=self.count-1
temp.radd=None
last.ladd=None
last.radd=None
last=None
# Method for delete middle node
def deleteMiddle(self):
if(self.start==None):
print("List not found")
else:
pos=int(input("Enter middle position for delete"))
if(pos>self.count):
print("Invalid postion total node is: ",self.count)
else:
i=1
temp=self.start
while(i<pos):
temp=temp.radd
i=i+1
prev=temp.ladd
next=temp.radd
prev.radd=next
next.ladd=prev
print("Deleted node is:",temp.data)
temp.radd=None
temp.ladd=None
temp=None
self.count=self.count-1
# Method for search in list
def searchData(self):
if(self.start==None):
print("List not found")
else:
n=int(input("Enter an element for search"))
temp=self.start
flag=0
while(temp!=None):
if(n==temp.data):
flag=1
break
temp=temp.radd
if(flag==1):
print("Searching success")
else:
print("Searching not success")
#Method for reverse display
def reverseDisplay(self):
if(self.start==None):
print("List not found")
else:
temp=self.start
while(temp.radd!=None):
temp=temp.radd
while(temp!=None):
print(temp.data,end=" ")
temp=temp.ladd
print("\nTotal Node is : ",self.count)
#Main Menu
mylist=DoubleLinkedList()
while(1):
print("\n-------------------------Doubly Linked List-----------------------")
print("1. Create")
print("2. Display")
print("3. Reverse Display")
print("4. Insert First")
print("5. Insert Middle")
print("6. Insert Last")
print("7. Delete First")
print("8. Delete Middle")
print("9. Delete Last")
print("10.Serching")
print("11. Exit")
print("-------------------------------------------------------------------")
choice=int(input("Enter your choice"))
if(choice==1):
mylist.createList()
elif(choice==2):
mylist.displayList()
elif(choice==3):
mylist.reverseDisplay()
elif(choice==4):
mylist.insertFirst()
elif(choice==5):
mylist.insertMiddle()
elif(choice==6):
mylist.insertLast()
elif(choice==7):
mylist.deleteFirst()
elif(choice==8):
mylist.deleteMiddle()
elif(choice==9):
mylist.deleteLast()
elif(choice==10):
mylist.searchData()
else:
break
Tags: deletion and searching in doubly linked listdeletion and searching in doubly linked list in dsa pythondoubly linked list in dsa pythondsa pythondsa python practicaldsa python programdsa python program on deletion and searching in doubly linked listdsa using python
DataFlair Team
DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.