0% found this document useful (0 votes)
10 views8 pages

Circular Doubly New

The document contains a Python implementation of a Circular Doubly Linked List with various methods for inserting, deleting, updating, and displaying nodes. It includes functionality to check for duplicates, find minimum and maximum values, and solve the Josephus problem. The code also provides methods for displaying the list in ascending and descending order.

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)
10 views8 pages

Circular Doubly New

The document contains a Python implementation of a Circular Doubly Linked List with various methods for inserting, deleting, updating, and displaying nodes. It includes functionality to check for duplicates, find minimum and maximum values, and solve the Josephus problem. The code also provides methods for displaying the list in ascending and descending order.

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/ 8

class Node:

def __init__(self, data):


self.__data = data
self.__next = None
self.__prev = 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 CircularDoublyLinkedList:
def __init__(self):
self.__head = None
self.__tail = None

def insert_at_beginning(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.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 insert_before_given_node(self, target, data):


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

# Check for duplicates


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

# Find target node


current = self.__head
while True:
if current.data == target:
new_node = Node(data)
new_node.next = current
new_node.prev = current.prev
current.prev.next = new_node
current.prev = new_node
if current == self.__head:
self.__head = new_node
return
current = current.next
if current == self.__head:
break
print(f"Node with value {target} not found.")

def insert_after_given_node(self, target, data):


if self.__head is None: # List is empty
print("List is empty.")
return
# Check for duplicates
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

# Find target node


current = self.__head
while True:
if current.data == target:
new_node = Node(data)
new_node.prev = current
new_node.next = current.next
current.next.prev = new_node
current.next = new_node
if current == self.__tail:
self.__tail = new_node
return
current = current.next
if current == self.__head:
break
print(f"Node with value {target} not found.")

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

def delete_specific_node(self, target):


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

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.")

def update_beginning(self, data):


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

# Check for duplicates


current = self.__head
while True:
if current != self.__head and current.data == data:
print(f"Duplicate value {data} not allowed.")
return
current = current.next
if current == self.__head:
break

self.__head.data = data

def update_end(self, data):


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

# Check for duplicates


current = self.__head
while True:
if current != self.__tail and current.data == data:
print(f"Duplicate value {data} not allowed.")
return
current = current.next
if current == self.__head:
break

self.__tail.data = data

def update_specific_node(self, target, data):


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

# Check for duplicates


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

# Find target node


current = self.__head
while True:
if current.data == target:
current.data = data
return
current = current.next
if current == self.__head:
break
print(f"Node with value {target} not found.")

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

def transfer_head(self, target):


if self.__head is None: # List is empty
print("List is empty.")
return
current = self.__head
while True:
if current.data == target:
self.__head = current
return

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

# Bubble sort approach to sort the values in ascending order


current = self.__head
# First, find the length of the list
length = 0
while True:
length += 1
current = current.next
if current == self.__head:
break

# Now perform bubble sort on the data in the list


for i in range(length):
current = self.__head
swapped = False
for j in range(length - i - 1):
if current.data > current.next.data:
# Swap data
current.data, current.next.data = current.next.data,
current.data
swapped = True
current = current.next
if not swapped:
break

# After sorting, display the values


current = self.__head
while True:
print(current.data, end=" ")
current = current.next
if current == self.__head:
break
print()

def display_descending_order(self):
if self.__head is None: # List is empty
print("List is empty.")
return

# Bubble sort approach to sort the values in descending order


current = self.__head
# First, find the length of the list
length = 0
while True:
length += 1
current = current.next
if current == self.__head:
break

# Now perform bubble sort on the data in the list


for i in range(length):
current = self.__head
swapped = False
for j in range(length - i - 1):
if current.data < current.next.data:
# Swap data
current.data, current.next.data = current.next.data,
current.data
swapped = True
current = current.next
if not swapped:
break

# After sorting, display the values in descending order


current = self.__head
while True:
print(current.data, end=" ")
current = current.next
if current == self.__head:
break
print()

def josephus_problem(self, k):


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

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

print(f"Last person standing: {self.__head.data}")


return self.__head.data

# 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)

You might also like