जब सर्कुलर लिंक्ड लिस्ट के बीच में एक नया नोड इन्सर्ट करने की आवश्यकता होती है, तो एक 'Node' क्लास बनाने की आवश्यकता होती है। इस क्लास में दो एट्रिब्यूट्स होते हैं: नोड में मौजूद डेटा, और लिंक्ड लिस्ट के अगले नोड तक पहुंच।
सर्कुलर लिंक्ड लिस्ट में, हेड और रियर एक-दूसरे के निकट होते हैं। वे एक सर्कल बनाने के लिए जुड़े होते हैं, और अंतिम नोड में 'NULL' वैल्यू नहीं होता।
एक और क्लास बनाने की आवश्यकता होती है जिसमें एक इनिशियलाइज़ेशन फ़ंक्शन हो, और नोड के हेड को 'None' से इनिशियलाइज़ किया जाता है।
लिंक्ड लिस्ट के बीच में नोड जोड़ने और नोड वैल्यूज़ प्रिंट करने के लिए यूज़र द्वारा कई मेथड्स परिभाषित किए जाते हैं।
इसके लिए एक प्रदर्शन नीचे दिया गया है:
उदाहरण
class Node:
def __init__(self, data):
self.data = data
self.next = None
class list_creation:
def __init__(self):
self.head = Node(None)
self.tail = Node(None)
self.head.next = self.tail
self.tail.next = self.head
self.size = 0
def add_data(self, my_data):
new_node = Node(my_data)
if self.head.data is None:
self.head = new_node
self.tail = new_node
new_node.next = self.head
else:
self.tail.next = new_node
self.tail = new_node
self.tail.next = self.head
self.size = self.size + 1
def add_in_between(self, my_data):
new_node = Node(my_data)
if self.head is None:
self.head = new_node
self.tail = new_node
new_node.next = self.head
else:
count = (self.size // 2) if (self.size % 2 == 0) else ((self.size + 1) // 2)
temp = self.head
for i in range(0, count):
curr = temp
temp = temp.next
curr.next = new_node
new_node.next = temp
self.size = self.size + 1
def print_it(self):
curr = self.head
if self.head is None:
print("The list is empty")
return
else:
print(curr.data)
while curr.next != self.head:
curr = curr.next
print(curr.data)
print("\n")
class circular_linked_list:
my_cl = list_creation()
print("Nodes are being added to the list")
my_cl.add_data(21)
my_cl.add_data(54)
my_cl.add_data(78)
my_cl.add_data(99)
print("The list is :")
my_cl.print_it()
my_cl.add_in_between(33)
print("The updated list is :")
my_cl.print_it()
my_cl.add_in_between(56)
print("The updated list is :")
my_cl.print_it()
my_cl.add_in_between(0)
print("The updated list is :")
my_cl.print_it()आउटपुट
Nodes are being added to the list The list is : 21 54 78 99 The updated list is : 21 54 33 78 99 The updated list is : 21 54 33 56 78 99 The updated list is : 21 54 33 0 56 78 99
व्याख्या
- 'Node' क्लास बनाई गई है।
- आवश्यक एट्रिब्यूट्स के साथ एक और क्लास बनाई गई है।
- 'add_in_between' नामक एक और मेथड परिभाषित किया गया है, जिसका उपयोग सर्कुलर लिंक्ड लिस्ट में बीच में डेटा जोड़ने के लिए किया जाता है, यानी सबसे मध्य स्थिति में।
- 'print_it' नामक एक और मेथड परिभाषित किया गया है, जो सर्कुलर लिंक्ड लिस्ट के नोड्स को प्रदर्शित करता है।
- 'list_creation' क्लास का एक ऑब्जेक्ट बनाया जाता है, और डेटा जोड़ने के लिए उस पर मेथड्स कॉल किए जाते हैं।
- एक 'init' मेथड परिभाषित किया गया है, जो सर्कुलर लिंक्ड लिस्ट के पहले और अंतिम नोड्स को None पर इनिशियलाइज़ करता है।
- 'add_in_between' मेथड को कॉल किया जाता है।
- यह लिस्ट के माध्यम से इटरेट करता है, और सबसे मध्य इंडेक्स प्राप्त करता है और इस पोजीशन पर एलिमेंट इन्सर्ट करता है।
- यह 'print_it' मेथड का उपयोग करके कंसोल पर प्रदर्शित होता है।