เมื่อจำเป็นต้องลบโหนดออกจากตรงกลางของรายการที่เชื่อมโยงแบบทวีคูณ จะต้องสร้างคลาส 'โหนด' ในคลาสนี้มีแอตทริบิวต์สามรายการ ได้แก่ ข้อมูลที่มีอยู่ในโหนด การเข้าถึงโหนดถัดไปของรายการที่เชื่อมโยง และการเข้าถึงโหนดก่อนหน้าของรายการที่เชื่อมโยง
ต้องสร้างคลาสอื่นที่จะมีฟังก์ชันเริ่มต้น และส่วนหัวของโหนดจะถูกเตรียมข้อมูลเบื้องต้นเป็น "ไม่มี" ภายในส่วนนี้
ผู้ใช้กำหนดวิธีการหลายวิธีในการเพิ่มโหนดในรายการที่เชื่อมโยง เพื่อแสดงโหนด และเพื่อลบโหนดออกจากจุดสิ้นสุดของรายการที่เชื่อมโยงแบบทวีคูณ
ในรายการที่เชื่อมโยงแบบทวีคูณ โหนดมีตัวชี้ โหนดปัจจุบันจะมีตัวชี้ไปยังโหนดถัดไปและโหนดก่อนหน้า ค่าสุดท้ายในรายการจะมีค่า 'NULL' ในพอยน์เตอร์ถัดไป สามารถเดินทางได้ทั้งสองทิศทาง
ด้านล่างนี้เป็นการสาธิตสำหรับสิ่งเดียวกัน -
ตัวอย่าง
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
self.size = 0;
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_middle(self):
if(self.head == None):
return;
else:
curr = self.head
mid = (self.size//2) if(self.size % 2 == 0) else((self.size+1)//2)
for i in range(1, mid):
curr = curr.next;
if(curr == self.head):
self.head = curr.next
self.tail.next = None
elif(curr == self.tail):
self.tail = self.tail.prev;
else:
curr.prev.next = curr.next;
curr.next.prev = curr.prev;
current = None;
self.size = self.size - 1;
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_middle();
print("The list after deleting the element from the middle 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 middle is : The nodes in the doubly linked list are : 24 54 77 92 The list after deleting the element from the middle is : The nodes in the doubly linked list are : 54 77 92 The list after deleting the element from the middle is : The nodes in the doubly linked list are : 77 92 The list after deleting the element from the middle is : The nodes in the doubly linked list are : 92 The list after deleting the element from the middle is : The list is empty
คำอธิบาย
- สร้างคลาส 'โหนด' แล้ว
- สร้างคลาสอื่นที่มีคุณสมบัติที่จำเป็นแล้ว
- มีการกำหนดวิธีการชื่อ 'add_data' ซึ่งใช้ในการเพิ่มข้อมูลไปยังรายการที่เชื่อมโยงแบบทวีคูณ
- มีการกำหนดวิธีการอื่นที่เรียกว่า 'print_it' ซึ่งแสดงโหนดของรายการที่เชื่อมโยงแบบวงกลม
- มีการกำหนดวิธีการอื่นที่ชื่อว่า 'delete_from_middle' ซึ่งจะลบโหนดออกจากตรงกลาง และทำให้โหนดก่อนหน้าเป็นโหนดปัจจุบันในรายการเชื่อมโยงแบบวงกลม
- อ็อบเจ็กต์ของคลาส 'double_list' ถูกสร้างขึ้น และมีการเรียกใช้เมธอดเพื่อลบโหนดจากจุดเริ่มต้นของรายการที่เชื่อมโยงแบบทวีคูณ
- มีการกำหนดวิธีการ 'init' ที่โหนดรูท ส่วนหัว และส่วนท้ายของรายการที่เชื่อมโยงแบบทวีคูณเป็นไม่มี
- รายการถูกทำซ้ำ และทุกโหนดจากดัชนีตรงกลางของรายการที่เชื่อมโยงจะถูกลบออกจนกว่าจะว่าง
- แสดงบนคอนโซลโดยใช้วิธี "print_it"