You are on page 1of 12

Doubly linked list!

May 10, 2022

1 DOUBLY LINKED LIST


[ ]: class node:
def __init__(self,data):
self.data=data
self.nref=None
self.pref=None
class dll:
def __init__(self):
self.head=None
def result(self):
if(self.head is None):
print("empty")
else:
n=self.head
while(n is not None):
print(n.data,"--->",end="")
n=n.nref
def back_result(self):
if(self.head is None):
print("empty")
else:
n=self.head
while(n.nref is not None):
n=n.nref
while n is not None:
print(n.data,"--->",end="")
n=n.pref
def add_begin(self,data):
new_node=node(data)
if self.head is None:
self.head=new_node
else:
new_node.nref=self.head
self.head.pref=new_node
self.head=new_node
def add_end(self,data):

1
new_node=node(data)
if(self.head is None):
self.head=new_node
else:
n=self.head
while n.nref is not None:
n=n.nref
n.nref=new_node
new_node.pref=n
def add_after(self,data,x):
if self.head is None:
print("LL is empty")
else:
n=self.head
while n is not None:
if x==n.data:
break
n=nref
if n is None:
print("given node is not present")
else:
new_node=node(data)
new_node.nref=n.nref
new_node.pref=n
if n.nref is not None:
n.nref.pref=new_node
n.nref=new_node
def add_before(self,data,x):
if self.head is None:
print("LL is empty")
else:
n=self.head
while n is not None:
if x==n.data:
break
n=n.nref
if n is None:
print("given node is not present")
else:
new_node=node(data)
new_node.nref=n
new_node.pref=n.pref
if n.pref is not None:
n.pref.nref=new_node
else:
self.head=new_node
n.pref=new_node

2
def delete_begin(self):
if self.head is None:
print("dll is empty")
return

if self.head.nref is None:

self.head=None
print("DLL is empty")
else:
self.head=self.head.nref
self.head.pref=None

def delete_end(self):
if self.head is None:
print("dll is empty")
return
if self.head.nref is None:
self.head=None
print("DLL is empty")
else:
n=self.head
while n.nref is not None:
n=n.nref
n.pref.nref=None
def delete_byvalue(self,x):
if self.head is None:
print("dll is empty ")
return
if self.head.nref is None:
if x==self.head.data:
self.head=None
else:
print("x is not present in dll")
return
if self.head.data==x:
self.head=self.head.nref
self.head.pref=None
return
n=self.head
while n.nref is not None:
if x==n.data:
break
n=n.nref
if n.nref is not None:
n.nref.pref=n.pref
n.pref.nref=n.nref

3
else:
if n.data==x:
n.pref.nref=None
else:
print("x is not present in dll")

obj=dll()
c = 1
print("Double Linked list!")
while c == 1:
v = int(input("""Enter your choice
1.Add element
2.Delete the element
3.Traverse
Enter the choice:"""))
if v == 1:
h = int(input("Enter the choice .\n1.Add at starting. \n2.Add element␣
,→at end. \n3.Add at middle. "))

if h == 1:
obj.add_begin(int(input("Enter the element you want to insert:")))
obj.result()
if h == 2:
obj.add_end(int(input("Enter the element you want to insert:")))
if h ==3:
obj.add_before(int(input("Enter the element you want to insert:")))
if v == 2:
f = int(input("Enter the choice. \n1.Delete the element in begin. \n2.
,→Delete the element in the end. \n3.Delete the element by the value"))

if f == 1:
obj.delete_begin()
obj.result()
if f ==2:
obj.delete_end()
obj.result()
if f ==3:
obj.delete_byvalue(int(input("Enter the element you want to ")))
obj.result()
if v == 3:
h = int(input("1.Forward \n2.Backward:"))
if h == 1:
obj.result()
else:
obj.result()
c = int(input("Enter 1 to continue...."))
if c == 1:
continue
else:

4
break

2 SINGLY LINKED LIST


[ ]: print("""Singly Linkede List Implementation
1.To show results
2.Add element in the Beginning
3.Add element in the End
4.Add after element
5.Add before the element
6.To delete the element in the Beginning
7.To delete the element in the End
8.To delete the element by the value
""")

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

class Linkedlist:
def __init__(self):
self.head = None
def result(self):
if(self.head is None):
print("The array is empty!")

else:
n = self.head
while(n is not None):
print(n.data,"--->",end = "")
n = n.next

def add_begin(self,data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node

def add_end(self,data):
new_node = Node(data)
if (self.head is None):
self.head = new_node

else:
n = self.head

5
while (n.next is not None):
n = n.next
n.next = new_node

def add_after(self,data,x):
n = n.next

while (n is not None):


if x == n.data:
break
n = n.next

if(n is None):
print("Node is empty in Linked List!")
else:
new_node = Node(data)
new_node.next = n.next
n.next = new_node

def add_before(self,data,x):

if (self.head is None):
print("Linked list is empty!")
return

if (self.head.data == x):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
return
n = self.head

while n.next is not None:

if (n.next.data == x):
break

n = n.next
if n.next is None:
print("Node is not found")
else:
new_node = Node(data)
new_node.next = n.next
n.next = new_node

def delete_beg(self):
if self.head is None:

6
print("2nd is empty")
else:
self.head = self.head.next

def delete_end(self):
if self.head is None:
print("Linked List is empty So it can't delete nodes!")
elif self.head.next is None:
self.head = None
else:
n = self.head
while n.next is not None:
n = n.next
n.next = None

def delete_by_Val(self,x):
if self.head is None:
print("Can't delete the linked list is empty!")
return

if (x==self.head.data):
self.head = self.head.next
return

n = self.head
while n.next is not None:
if x == n.next.data:
break
n = n.next

if n.next is None:
print("Node is not present!")

else:
n.next = n.next.next
ll = Linkedlist()

c = 1
while c == True:
choi = int(input("Enter the choice:"))
if choi == 1:
ll.result()
elif choi == 2:
ll.add_begin(int(input("Enter the data:")))
elif choi == 3:
ll.add_end(int(input("Enter the data:")))
elif choi == 4:

7
ll.add_after(int(input("Enter the data:")),int(input("Enter the␣
,→position")))
elif choi == 5:
ll.add_before(int(input("Enter the data:")),int(input("Enter the␣
,→position")))

elif choi == 6:
ll.delete_beg()
elif choi == 7:
ll.delete_end()
elif choi == 8:
ll.delete_by_Val(input("Enter the data:"))
else:
print("Enter any number between the choice:")
print("Do you want to continue...press 1 or press 0 to stop.")
c = int(input("Enter the choice:"))
if c == 1:
continue
else:
break

3 CIRCULAR LINKED LIST


[ ]: class Node:
def __init__(self, val):
self.value = val
self.left = None
self.right = None

class CDLL:
def __init__(self):
self.head1 = None
self.head2 = None

def add(self, val):


if self.empty() is True:
self.head1 = Node(val)
self.head2 = self.head1
self.head2.right = self.head1
self.head1.left = self.head2
print("Head value created")
return
opt = int(input(
"Enter 1 to add a Node at the beginning\nEnter 2 to add a Node at␣
,→the end\nEnter 3 to add a Node before "

" a node\nEnter 4 to add a Node after a node ::"))


if opt == 1:

8
a = Node(val)
a.right = self.head1
self.head1.left = a
self.head2.right = a
a.left = self.head2
self.head1 = self.head1.left
elif opt == 2:
a = Node(val)
self.head2.right = a
a.left = self.head2
a.right = self.head1
self.head1.left = a
self.head2 = self.head2.right
elif opt == 3:
pos = int(input("Enter the position ::"))
i = 1
n = self.head1.right
flag = 0
while n is not self.head1:
if i == pos:
flag = 1
break
n = n.right
i = i + 1
if flag == 1:
a = Node(val)
a.right = n
n.left.right = a
a.left = n.left
n.left = a
else:
print("Position not found")
elif opt == 4:
pos = int(input("Enter the position ::"))
i = 0
n = self.head1
flag = 0
while n.right is not self.head1:
if i == pos:
flag = 1
break
n = n.right
i = i + 1
if flag == 1:
a = Node(val)
n.right.left = a
a.right = n.right

9
a.left = n
n.right = a
else:
print("Position not found")
else:
print("Wrong option")

def delete(self):
if self.empty() is True:
print("Linked List empty")
return
elif self.head1.right is self.head2:
self.head1 = None
self.head2 = None
return
opt = int(input("Enter 1 to delete the beginning element\nEnter 2 to␣
,→delete the last element\nEnter 3 to "

"delete elements in between ::"))


if opt == 1:
self.head1 = self.head1.right
self.head1.left = self.head2
self.head2.right = self.head1
elif opt == 2:
self.head2 = self.head2.left
self.head2.right = self.head1
self.head1.left = self.head2
else:
op = int(input("Enter 1 to delete by position\nEnter 2 to delete by␣
,→value ::"))

if op == 1:
pos = int(input("Enter the position ::"))
i = 1
n = self.head1.right
flag = 0
while n.right is not self.head1:
if i == pos:
flag = 1
break
i = i + 1
n = n.right
if flag == 1:
n.left.right = n.right
n.right.left = n.left
else:
print("Position not found")
return
else:

10
val = int(input("Enter the value you want to delete ::"))
n = self.head1
flag = 0
while n.right is not self.head1:
if n.value == val:
flag = 1
break
n = n.right
if flag == 1:
n.left.right = n.right
n.right.left = n.left
else:
print("Value not found")
return

def clear(self):
self.head1 = None
self.head2 = None
print("Linked List cleared")

def empty(self):
if self.head1 is None:
return True
else:
return False

def display(self):
if self.empty() is True:
print("Linked List empty")
return
elif self.head1.right is self.head1:
print("LINKED LIST")
print(self.head1.value, "HEAD 1 & 2")
print("Linked List ends")
return
o = int(input("Enter 1 to print in forward direction\nEnter 2 to print␣
,→in backward direction ::"))

print("LINKED LIST")
if o == 1:
print(self.head1.value, " <== HEAD 1")
n = self.head1.right
while n is not self.head2:
print(n.value)
n = n.right
print(self.head2.value, " <== HEAD 2")
print("Linked List ends")
elif o == 2:

11
print(self.head2.value, " <== HEAD 2")
n = self.head2.left
while n is not self.head1:
print(n.value)
n = n.left
print(self.head1.value, " <== HEAD 1")
print("Linked List ends")
else:
print("Wrong option")

LL = CDLL()
while True:
option = int(input("Enter 1 to add an element\nEnter 2 to delete an␣
,→element\nEnter 3 to clear the Linked "

"List\nEnter 4 to display the Linked List\nEnter 5 to␣


,→exit :"))

if option == 1:
value = int(input("Enter the value you want to add :"))
LL.add(value)
continue
elif option == 2:
LL.delete()
continue
elif option == 3:
LL.clear()
continue
elif option == 4:
LL.display()
continue
elif option == 5:
print("Goodbye")
break
elif option == 6:
print(LL.empty())
else:
print("Wrong option")

12

You might also like