जब दोहरी लिंक की गई सूची के अंत से किसी नोड को हटाना आवश्यक होता है, तो एक 'नोड' वर्ग बनाने की आवश्यकता होती है। इस वर्ग में, तीन विशेषताएं हैं, डेटा जो नोड में मौजूद है, लिंक की गई सूची के अगले नोड तक पहुंच और लिंक की गई सूची के पिछले नोड तक पहुंच है।
नीचे उसी के लिए एक प्रदर्शन है -
उदाहरण
class Node:
def __init__(self, my_data):
self.prev = None
self.data = my_data
self.next = None
class double_list:
def __init__(self):
self.head = None
self.tail = None
def add_data(self, my_data):
new_node = Node(my_data)
if(self.head == None):
self.head = self.tail = new_node;
self.head.previous = None;
self.tail.next = None;
else:
self.tail.next = new_node;
new_node.previous = self.tail;
self.tail = new_node;
self.tail.next = None;
def print_it(self):
curr = self.head
if (self.head == None):
print("The list is empty")
return
print("The nodes in the doubly linked list are :")
while curr != None:
print(curr.data)
curr = curr.next
def delete_from_end(self):
if(self.head == None):
return;
else:
if(self.head != self.tail):
self.tail = self.tail.previous
self.tail.next = None;
else:
self.head = self.tail = None;
my_instance = double_list()
print("Elements are being added to the doubly linked list")
my_instance.add_data(10)
my_instance.add_data(24)
my_instance.add_data(54)
my_instance.add_data(77)
my_instance.add_data(92)
my_instance.print_it()
while(my_instance.head != None):
my_instance.delete_from_end();
print("The list after deleting the element from the end is : ");
my_instance.print_it(); आउटपुट
Elements are being added to the doubly linked list The nodes in the doubly linked list are : 10 24 54 77 92 The list after deleting the element from the end is : The nodes in the doubly linked list are : 10 24 54 77 The list after deleting the element from the end is : The nodes in the doubly linked list are : 10 24 54 The list after deleting the element from the end is : The nodes in the doubly linked list are : 10 24 The list after deleting the element from the end is : The nodes in the doubly linked list are : 10 The list after deleting the element from the end is : The list is empty
स्पष्टीकरण
- 'नोड' वर्ग बनाया गया है।
- आवश्यक विशेषताओं वाला एक और वर्ग बनाया गया है।
- 'add_data' नाम की एक विधि परिभाषित की गई है, जिसका उपयोग डबल लिंक्ड सूची में डेटा जोड़ने के लिए किया जाता है।
- 'print_it' नाम की एक अन्य विधि परिभाषित की गई है, जो सर्कुलर लिंक्ड सूची के नोड्स को प्रदर्शित करती है।
- 'delete_from_end' नाम की एक और विधि परिभाषित की गई है, जो नोड को अंत से हटाती है, यानी 'टेल' नोड और पिछले नोड को सर्कुलर लिंक्ड लिस्ट में टेल नोड के रूप में बनाती है।
- डबल_लिस्ट' वर्ग का एक ऑब्जेक्ट बनाया जाता है, और डबल लिंक्ड सूची की शुरुआत से नोड को हटाने के लिए विधियों को बुलाया जाता है।
- एक 'init' विधि को परिभाषित किया गया है, कि रूट, हेड, और टेल नोड्स डबल लिंक्ड लिस्ट का कोई नहीं से जुड़ा हुआ है।
- सूची को पुनरावृत्त किया जाता है, और लिंक की गई सूची के अंत से प्रत्येक नोड को खाली होने तक हटा दिया जाता है।
- यह 'print_it' पद्धति का उपयोग करके कंसोल पर प्रदर्शित होता है।