Python program that finds the maximum and minimum value node from a circular linked list:
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def append(self, data):
if not self.head:
self.head = Node(data)
self.head.next = self.head
else:
current = self.head
while current.next != self.head:
current = current.next
new_node = Node(data)
current.next = new_node
new_node.next = self.head
def find_min_max(self):
min_node = None
max_node = None
current = self.head
if self.head:
min_node = self.head
max_node = self.head
current = self.head.next
while current != self.head:
if current.data > max_node.data:
max_node = current
if current.data < min_node.data:
min_node = current
current = current.next
return min_node, max_node
def display(self):
current = self.head
if self.head:
while current.next != self.head:
print(current.data)
current = current.next
print(current.data)
# create circular linked list
cll = CircularLinkedList()
# append some data to the list
cll.append(1)
cll.append(2)
cll.append(3)
cll.append(4)
cll.append(5)
# find minimum and maximum node
min_node, max_node = cll.find_min_max()
# display the minimum and maximum node
print("Minimum node value: ", min_node.data)
print("Maximum node value: ", max_node.data)
In this example, we first define a Node class with a constructor that initializes the data and next properties. The CircularLinkedList class is implemented using the Node class. The CircularLinkedList class has an append method that adds new data to the end of the linked list, a find_min_max method that finds the minimum and maximum value node in the linked list, and a display