जब एक डबल लिंक्ड सूची की शुरुआत से किसी नोड को हटाने की आवश्यकता होती है, तो एक 'नोड' वर्ग बनाने की आवश्यकता होती है। इस वर्ग में, तीन विशेषताएं हैं, डेटा जो नोड में मौजूद है, लिंक की गई सूची के अगले नोड तक पहुंच और लिंक की गई सूची के पिछले नोड तक पहुंच है।
नीचे उसी के लिए एक प्रदर्शन है -
उदाहरण
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_beginning(self):
if(self.head == None):
return;
else:
if(self.head != self.tail):
self.head = self.head.next;
self.head.previous = 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_beginning();
print("The list after deleting the element from the beginning 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 beginning is : The nodes in the doubly linked list are : 24 54 77 92 The list after deleting the element from the beginning is : The nodes in the doubly linked list are : 54 77 92 The list after deleting the element from the beginning is : The nodes in the doubly linked list are : 77 92 The list after deleting the element from the beginning is : The nodes in the doubly linked list are : 92 The list after deleting the element from the beginning is : The list is empty
स्पष्टीकरण
- 'नोड' वर्ग बनाया गया है।
- आवश्यक विशेषताओं वाला एक और वर्ग बनाया गया है।
- 'add_data' नाम की एक विधि परिभाषित की गई है, जिसका उपयोग डबल लिंक्ड सूची में डेटा जोड़ने के लिए किया जाता है।
- 'print_it' नाम की एक अन्य विधि परिभाषित की गई है, जो सर्कुलर लिंक्ड सूची के नोड्स को प्रदर्शित करती है।
- 'delete_from_beginning' नाम की एक और विधि परिभाषित की गई है, जो नोड को शुरुआत से हटाती है, यानी 'हेड' नोड और सर्कुलर लिंक्ड लिस्ट में अगले नोड को हेड नोड के रूप में बनाती है।
- 'डबल_लिस्ट' क्लास का एक ऑब्जेक्ट बनाया जाता है, और डबल लिंक्ड लिस्ट की शुरुआत से एक नोड को हटाने के लिए उस पर विधियों को बुलाया जाता है।
- एक 'init' विधि को परिभाषित किया गया है, कि दोहरी लिंक की गई सूची के रूट, हेड और टेल नोड्स किसी से नहीं।
- सूची को पुनरावृत्त किया जाता है, और शुरुआत से प्रत्येक नोड को खाली होने तक हटा दिया जाता है।
- यह 'print_it' पद्धति का उपयोग करके कंसोल पर प्रदर्शित होता है।