सर्कुलर लिंक्ड लिस्ट में किसी एलिमेंट को सर्च करने के लिए, हमें एक 'Node' क्लास बनाने की आवश्यकता होती है। इस क्लास में दो एट्रिब्यूट्स होते हैं: नोड में मौजूद डेटा और लिंक्ड लिस्ट के अगले नोड का एक्सेस।
सर्कुलर लिंक्ड लिस्ट की संरचना
सर्कुलर लिंक्ड लिस्ट में, हेड (head) और रियर (rear) एक-दूसरे से जुड़े होते हैं। ये एक सर्कल बनाते हैं, और अंतिम नोड में '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
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
def search_value(self, elem_to_search):
curr = self.head
i = 1
flag_val = False
if self.head is None:
print("The list is empty")
else:
while True:
if curr.data == elem_to_search:
flag_val = True
break
curr = curr.next
i = i + 1
if curr == self.head:
break
if flag_val:
print("The element is present in list at position: " + str(i))
else:
print("The element is not present in list")
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)
my_cl.add_data(27)
print("The list is:")
my_cl.print_it()
print("Value 99 is being searched")
my_cl.search_value(99)
print("Value 0 is being searched")
my_cl.search_value(0)
आउटपुट
Nodes are being added to the list
The list is:
21
54
78
99
27
Value 99 is being searched
The element is present in list at position: 4
Value 0 is being searched
The element is not present in list
व्याख्या
- 'Node' क्लास बनाई गई है।
- आवश्यक एट्रिब्यूट्स के साथ एक और क्लास बनाई गई है।
- 'search_value' नामक एक मेथड परिभाषित किया गया है, जिसका उपयोग लिंक्ड लिस्ट में किसी विशिष्ट एलिमेंट को सर्च करने के लिए किया जाता है।
- 'print_it' नामक एक मेथड परिभाषित किया गया है, जो सर्कुलर लिंक्ड लिस्ट के नोड्स को डिस्प्ले करता है।
- 'list_creation' क्लास का एक ऑब्जेक्ट बनाया जाता है, और डेटा जोड़ने के लिए इसके मेथड्स को कॉल किया जाता है।
- एक 'init' मेथड परिभाषित किया गया है, जो सर्कुलर लिंक्ड लिस्ट के पहले और अंतिम नोड्स को None पर इनिशियलाइज़ करता है।
- 'search_value' मेथड को कॉल किया जाता है।
- यह लिस्ट के माध्यम से इटरेट करता है, और जांचता है कि सर्च किया जाने वाला एलिमेंट मिला है या नहीं।
- यदि मिल जाता है, तो उसका इंडेक्स डिस्प्ले किया जाता है।
- यह 'print_it' मेथड का उपयोग करके कंसोल पर डिस्प्ले किया जाता है।