0% found this document useful (0 votes)
5 views7 pages

DOUBLY

The document defines a doubly linked list implementation in Python, including methods for inserting, deleting, and updating nodes. It features various functionalities such as inserting before or after specific nodes, finding maximum and minimum values, and checking the integrity of the doubly linked structure. The provided code also includes a demonstration of the linked list operations.

Uploaded by

yuzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

DOUBLY

The document defines a doubly linked list implementation in Python, including methods for inserting, deleting, and updating nodes. It features various functionalities such as inserting before or after specific nodes, finding maximum and minimum values, and checking the integrity of the doubly linked structure. The provided code also includes a demonstration of the linked list operations.

Uploaded by

yuzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

class Node:

def __init__(self, data):


self.__prev = None
self.__data = data
self.__next = None

@property
def data(self):
return self.__data

@data.setter
def data(self, data):
self.__data = data

@property
def prev(self):
return self.__prev

@prev.setter
def prev(self, value):
self.__prev = value

@property
def next(self):
return self.__next

@next.setter
def next(self, value):
self.__next = value

class Doubly:
def __init__(self):
self.__head = None

def insert_beginning(self, data):


new_node = Node(data)

if self.__head is None:
self.__head = new_node

else:
self.__head.prev = new_node
new_node.next = self.__head
self.__head = new_node

def insert_end(self, data):


new_node = Node(data)

if self.__head is None:
self.__head = new_node

else:
current = self.__head

while current.next:
current = current.next

new_node.prev = current
current.next = new_node

def insert_before(self, key, data):


new_node = Node(data)

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

if self.__head.data == key:
self.__head.prev = new_node
new_node.next = self.__head
self.__head = new_node
return

current = self.__head

while current and current.data != key:


current = current.next

if current is None:
print("Key doesn't exist.")

else:
new_node.prev = current.prev
new_node.next = current
current.prev.next = new_node
current.prev = new_node

def insert_before_last_occurrence(self, key, data):


new_node = Node(data)

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

# Find the last occurrence of the key


current = self.__head
last_occurrence = None
while current:
if current.data == key:
last_occurrence = current
current = current.next

if last_occurrence is None:
print("Key doesn't exist.")
return

# If the last occurrence is the head node


if last_occurrence == self.__head:
self.insert_beginning(data)
return

# Insert before the last occurrence


new_node.prev = last_occurrence.prev
new_node.next = last_occurrence
if last_occurrence.prev:
last_occurrence.prev.next = new_node
last_occurrence.prev = new_node
def insert_after(self, key, data):
new_node = Node(data)

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

current = self.__head

while current and current.data != key:


current = current.next

if current is None:
print("Key doesn't exist.")

else:
new_node.prev = current
new_node.next = current.next

if current.next:
current.next.prev = new_node

current.next = new_node

def insert_after_last_occurrence(self, key, data):


new_node = Node(data)

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

current = self.__head
last_occurrence = None

while current:
if current.data == key:
last_occurrence = current

current = current.next

if last_occurrence is None:
print("Key doesn't exist.")
return

new_node.prev = last_occurrence
new_node.next = last_occurrence.next

if last_occurrence.next:
last_occurrence.next.prev = new_node

last_occurrence.next = new_node

def delete_beginning(self):
if self.__head is None:
print("Linked List is empty.")

else:
self.__head = self.__head.next

def delete_end(self):
if self.__head is None:
print("Linked List is empty.")

else:
current = self.__head

while current.next:
current = current.next

current.prev.next = None

def delete_specific(self, key):


if self.__head is None:
print("Linked List is empty.")
return

if self.__head.data == key:
self.__head = self.__head.next
return

current = self.__head

while current and current.data != key:


current = current.next

if current is None:
print("Key doesn't exist.")
return

if current.prev:
current.prev.next = current.next

if current.next:
current.next.prev = current.prev

def delete_specific_last_occurrence(self, key):


if self.__head is None:
print("Linked List is empty.")
return

if self.__head.data == key:
self.__head = self.__head.next
return

current = self.__head
last_occurrence = None

while current:
if current.data == key:
last_occurrence = current

current = current.next

if last_occurrence is None:
print("Key doesn't exist.")
return
if last_occurrence.prev:
last_occurrence.prev.next = last_occurrence.next

if last_occurrence.next:
last_occurrence.next.prev = last_occurrence.prev

def update_beginning(self, data):


if self.__head is None:
print("Linked list is empty. ")

else:
self.__head.data = data

def update_end(self, data):


if self.__head is None:
print("Linked list is empty. ")

else:
current = self.__head

while current.next:
current = current.next

current.data = data

def update_specific(self, key, data):


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

if self.__head.data == key:
self.__head.data = data
return

current = self.__head

while current and current.data != key:


current = current.next

if current is None:
print("Key doesn't exist.")

else:
current.data = data

def update_specific_last_occurrence(self, key, data):


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

if self.__head.data == key:
self.__head.data = data
return

current = self.__head
last_occurrence = None

while current:
if current.data == key:
last_occurrence = current

current = current.next

if last_occurrence is None:
print("Key doesn't exist.")

else:
last_occurrence.data = data

def max(self):
max_value = self.__head.data
current = self.__head

while current:
if current.data > max_value:
max_value = current.data

current = current.next

return max_value

def min(self):
min_value = self.__head.data
current = self.__head

while current:
if current.data < min_value:
min_value = current.data

current = current.next

return min_value

def is_Doubly(self):

current = self.__head

while current:
if current.next and current.next.prev != current:
return False

current = current.next

return True

def display(self):
current = self.__head
while current:
print(current.data, end=" ")
current = current.next

linked_list = Doubly()
linked_list.insert_end(10)
linked_list.insert_end(20)
linked_list.insert_end(15)
linked_list.insert_end(5)
linked_list.insert_beginning(5)
linked_list.insert_before(5, 0)
# linked_list.insert_before_last_occurrence(5, 0)
# linked_list.insert_after(5, 25)
# linked_list.insert_after_last_occurrence(5, 25)

# linked_list.delete_beginning()
# linked_list.delete_end()
# linked_list.delete_specific(5)
# linked_list.delete_specific_last_occurrence(5)

# linked_list.update_beginning("A")
# linked_list.update_end("Z")
# linked_list.update_specific(5, 50)
# linked_list.update_specific_last_occurrence(5, 50)

linked_list.display()

print("\nMax value:", linked_list.max())


print("Min value:", linked_list.min())
print("Is Doubly Linked List:", linked_list.is_Doubly())

You might also like