Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

โปรแกรม Python ลบโหนดกลาง Circular Linked List


เมื่อต้องการลบโหนดจากกลางของ Circular Linked List ต้องสร้างคลาส 'Node' ที่มีแอตทริบิวต์สองอย่าง: ข้อมูลในโหนด และการเข้าถึงโหนดถัดไป

ใน Circular Linked List หัวและท้ายอยู่ติดกัน เชื่อมเป็นวงกลม ไม่มีค่า 'NULL' ในโหนดสุดท้าย

สร้างอีกคลาสหนึ่งมีฟังก์ชันเริ่มต้น หัวโหนดเริ่มต้นเป็น 'None' และตัวแปร size เริ่มต้นเป็น 0

มีฟังก์ชันที่ผู้ใช้กำหนดช่วยเพิ่มโหนด พิมพ์บนคอนโซล และลบโหนดจากตำแหน่งกลาง

ตัวอย่าง

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 = int(self.size) + 1

    def delete_from_mid(self):
        if self.head == None:
            return
        else:
            count = (self.size // 2) if (self.size % 2 == 0) else ((self.size + 1) // 2)
            if self.head != self.tail:
                temp = self.head
                curr = None
                for i in range(0, count - 1):
                    curr = temp
                    temp = temp.next
                if curr != None:
                    curr.next = temp.next
                    temp = None
                else:
                    self.head = self.tail = temp.next
                    self.tail.next = self.head
                    temp = None
            else:
                self.head = self.tail = None
            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()
    my_cl.add_data(11)
    my_cl.add_data(52)
    my_cl.add_data(36)
    my_cl.add_data(74)
    print("The original list is :")
    my_cl.print_it()
    while my_cl.head != None:
        my_cl.delete_from_mid()
        print("The list after updation is :")
        my_cl.print_it()

ผลลัพธ์

The original list is : 11 52 36 74
The list after updation is : 11 36 74
The list after updation is : 11 74
The list after updation is : 74
The list after updation is : The list is empty

คำอธิบาย

  • สร้างคลาส 'Node'
  • สร้างคลาสอื่นพร้อมแอตทริบิวต์ที่จำเป็น
  • กำหนดเมธอด 'add_data' สำหรับเพิ่มข้อมูลใน Circular Linked List
  • กำหนดเมธอด 'delete_from_middle' ลบองค์ประกอบทีละตัวจากกลางโดยลบการอ้างอิง
  • กำหนดเมธอด 'print_it' แสดงข้อมูล Linked List บนคอนโซล
  • สร้างออบเจ็กต์ของคลาส 'list_creation' และเรียกใช้เมธอด
  • เรียกใช้เมธอด 'delete_from_middle'
  • วนลูปผ่านโหนด หาตำแหน่งกลางและเริ่มลบองค์ประกอบ
  • แสดงผลด้วยเมธอด 'print_it'