Doubly Linked list in python

Doubly Linked list





The doubly linked list is similar to a single linked list, add a new pointer to this concept,The list attached to Doubly can be seen in both the front and back directions, as in the image,And the double link is using more space in the program, we will teach you how to make double link in python, doubly linked list have to use next point and previous,
In this image the prev is store the previous node value and The process is running continuously, so you check the code how to create a list with double links,


Code

class node:
def __init__(self,data=None):
self.data=data;
self.next=None;
self.prev=None;
class DoublyLinked ():
def __init__(self):
self.head=None;
def adddata(self,newnode):
Newnode=node(newnode);
Newnode.next=self.head;
self.head=Newnode;
if self.head is None:
self.head.prev=Newnode;
self.head=Newnode;
def insertdata(self,newnode):
Newnode=node(newnode);
Newnode.next=self.head;
self.head=Newnode;
if self.head is None:
self.head.next.prev=Newnode;
self.head=Newnode;
def insertmiddle(self,mid,newnode):
Newnode=node(newnode);
Newnode.next=mid.next;
mid.next=Newnode;
Newnode.prev=mid.next;
if Newnode.prev:
Newnode.next.prev=Newnode;
def insertlast(self,newnode):
Newnode=node(newnode);
Newnode.next=None;
if self.head is None:
Newnode.prev=None;
self.head=Newnode;
return;
last=self.head;
while last.next is not None:
last=last.next;
last.next=Newnode;
Newnode.prev=last;
def deletenode(self,remove):
Head=self.head;
if Head is not None:
if Head.data==remove:
self.head=Head.next;
Head=None;
return;
while Head is not None:
if Head.data==remove:
break;
last=Head;
Head=Head.next;
if Head==None:
return;
last.next=Head.next;
Head=None;
def display(self):
show=self.head;
while show:
print(show.data);
last=show;
show=show.next;
obj=DoublyLinked();
obj.adddata(int(input('enter the item =')));
obj.adddata(int(input('enter the item=')));
obj.adddata(int(input('enter the item=')));
obj.adddata(int(input('enter the item=')));
obj.adddata(int(input('enter the item=')));
obj.adddata(int(input('enter the item=')));
obj.display();
obj.insertdata(int(input('enter the item=')));
obj.display();
obj.insertmiddle(obj.head.next,int(input('enter the number==>')));
obj.display();
obj.insertlast(int(input('enter the number==>>')));
obj.display();
obj.deletenode(int(input('enter the number for delete==>>>>>')));
obj.display();
view raw python hosted with ❤ by GitHub

insert:

def insertdata(self,newnode):
Newnode=node(newnode);
Newnode.next=self.head;
self.head=Newnode;
if self.head is None:
self.head.next.prev=Newnode;
self.head=Newnode;
view raw python hosted with ❤ by GitHub

Add node Middle:

def insertmiddle(self,mid,newnode):
Newnode=node(newnode);
Newnode.next=mid.next;
mid.next=Newnode;
Newnode.prev=mid.next;
if Newnode.prev:
Newnode.next.prev=Newnode;
view raw python hosted with ❤ by GitHub

Add node last

def insertlast(self,newnode):
Newnode=node(newnode);
Newnode.next=None;
if self.head is None:
Newnode.prev=None;
self.head=Newnode;
return;
last=self.head;
while last.next is not None:
last=last.next;
last.next=Newnode;
Newnode.prev=last;
view raw python hosted with ❤ by GitHub

Delete Node

def deletenode(self,remove):
Head=self.head;
if Head is not None:
if Head.data==remove:
self.head=Head.next;
Head=None;
return;
while Head is not None:
if Head.data==remove:
break;
last=Head;
Head=Head.next;
if Head==None:
return;
last.next=Head.next;
Head=None;
view raw python hosted with ❤ by GitHub