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

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


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

ใน Circular Linked List หัวและท้ายจะอยู่ติดกัน เชื่อมต่อเป็นวงกลม ไม่มีค่า '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
        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 = self.size + 1

    def add_in_between(self, my_data):
        new_node = Node(my_data)
        if self.head == None:
            self.head = new_node
            self.tail = new_node
            new_node.next = self.head
        else:
            count = (self.size // 2) if (self.size % 2 == 0) else ((self.size + 1) // 2)
            temp = self.head
            for i in range(0, count):
                curr = temp
                temp = temp.next
            curr.next = new_node
            new_node.next = temp
        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()
    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)
    print("The list is :")
    my_cl.print_it()
    my_cl.add_in_between(33)
    print("The updated list is :")
    my_cl.print_it()
    my_cl.add_in_between(56)
    print("The updated list is :")
    my_cl.print_it()
    my_cl.add_in_between(0)
    print("The updated list is :")
    my_cl.print_it()

ผลลัพธ์

Nodes are being added to the list
The list is : 21 54 78 99
The updated list is : 21 54 33 78 99
The updated list is : 21 54 33 56 78 99
The updated list is : 21 54 33 0 56 78 99

คำอธิบาย

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