Circular Doubly New
Circular Doubly New
@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 CircularDoublyLinkedList:
def __init__(self):
self.__head = None
self.__tail = None
new_node = Node(data)
if self.__head is None: # List is empty
self.__head = self.__tail = new_node
self.__head.next = self.__head.prev = new_node
else:
new_node.next = self.__head
new_node.prev = self.__tail
self.__head.prev = new_node
self.__tail.next = new_node
self.__head = new_node
def insert_at_end(self, data):
# Check for duplicates
if self.__head:
current = self.__head
while True:
if current.data == data:
print(f"Duplicate value {data} not allowed.")
return
current = current.next
if current == self.__head:
break
new_node = Node(data)
if self.__head is None: # List is empty
self.__head = self.__tail = new_node
self.__head.next = self.__head.prev = new_node
else:
new_node.prev = self.__tail
new_node.next = self.__head
self.__tail.next = new_node
self.__head.prev = new_node
self.__tail = new_node
def delete_beginning(self):
if self.__head is None: # List is empty
print("List is empty.")
return
if self.__head == self.__tail: # Only one node
self.__head = self.__tail = None
else:
self.__head = self.__head.next
self.__head.prev = self.__tail
self.__tail.next = self.__head
def delete_end(self):
if self.__head is None: # List is empty
print("List is empty.")
return
if self.__head == self.__tail: # Only one node
self.__head = self.__tail = None
else:
self.__tail = self.__tail.prev
self.__tail.next = self.__head
self.__head.prev = self.__tail
current = self.__head
while True:
if current.data == target:
if current == self.__head:
self.delete_beginning()
elif current == self.__tail:
self.delete_end()
else:
current.prev.next = current.next
current.next.prev = current.prev
return
current = current.next
if current == self.__head:
break
print(f"Node with value {target} not found.")
self.__head.data = data
self.__tail.data = data
def min_value(self):
if self.__head is None: # List is empty
print("List is empty.")
return None
current = self.__head
minimum = self.__head.data
while True:
if current.data < minimum:
minimum = current.data
current = current.next
if current == self.__head:
break
return minimum
def max_value(self):
if self.__head is None: # List is empty
print("List is empty.")
return None
current = self.__head
maximum = self.__head.data
while True:
if current.data > maximum:
maximum = current.data
current = current.next
if current == self.__head:
break
return maximum
current = current.next
if current == self.__head:
break
print(f"Node with value {target} not found.")
def is_CircularDoubly(self):
if self.__head is None: # List is empty
print("The list is empty")
return False
# Check if tail's next points to head and head's prev points to tail
if self.__tail.next == self.__head and self.__head.prev == self.__tail:
return True
else:
return False
def display(self):
if self.__head is None: # List is empty
print("List is empty.")
return
current = self.__head
while True:
print(current.data, end=" ")
current = current.next
if current == self.__head:
break
print()
def reverse_display(self):
if self.__head is None: # List is empty
print("List is empty.")
return
current = self.__tail
while True:
print(current.data, end=" ")
current = current.prev
if current == self.__tail:
break
print()
def display_ascending_order(self):
if self.__head is None: # List is empty
print("List is empty.")
return
def display_descending_order(self):
if self.__head is None: # List is empty
print("List is empty.")
return
current = self.__head
while self.__head != self.__tail: # Stop when only one node is left
# Move k-1 steps forward
for i in range(k - 1):
current = current.next
print(f"Removing: {current.data}")
# Remove the current node
if current == self.__head:
self.delete_beginning()
current = self.__head
elif current == self.__tail:
self.delete_end()
current = self.__head
else:
next_node = current.next
current.prev.next = current.next
current.next.prev = current.prev
current = next_node
# Example usage
cdll = CircularDoublyLinkedList()
cdll.insert_at_beginning(0)
cdll.insert_at_end(1)
cdll.insert_at_end(10)
cdll.insert_at_end(2)
cdll.insert_at_end(9)
cdll.insert_at_end(3)
cdll.insert_at_end(8)
cdll.insert_at_end(4)
cdll.insert_at_end(7)
cdll.insert_at_end(5)
cdll.insert_at_end(6)
cdll.insert_at_end(2)
cdll.insert_at_end(2)
cdll.insert_at_end(1)
cdll.display()
k = 3 # Step size for Josephus problem
cdll.josephus_problem(k)