Computer >> Máy Tính >  >> Lập trình >> Python

Chương trình Python để loại bỏ các phần tử trùng lặp khỏi Danh sách được Liên kết Đôi

Khi cần loại bỏ các phần tử trùng lặp trong danh sách được liên kết kép, lớp ‘Node’ cần được tạo. Trong lớp này, có ba thuộc tính, dữ liệu có trong nút, quyền truy cập vào nút tiếp theo của danh sách được liên kết và quyền truy cập vào nút trước đó của danh sách được liên kết.

Dưới đây là một minh chứng cho điều tương tự -

Ví dụ

Nút
class Node:
   def __init__(self, my_data):
      self.previous = 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 remove_duplicates(self):
      if(self.head == None):
         return
      else:
         curr = self.head;
         while(curr != None):
            index_val = curr.next
            while(index_val != None):
               if(curr.data == index_val.data):
                  temp = index_val
                  index_val.previous.next = index_val.next
                  if(index_val.next != None):
                     index_val.next.previous = index_val.previous
                  temp = None
               index_val = index_val.next
            curr = curr.next
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(24)
my_instance.print_it()
print("The elements in the list after removing duplicates are : ")
my_instance.remove_duplicates()
my_instance.print_it()

Đầu ra

Elements are being added to the doubly linked list
The nodes in the doubly linked list are :
10
24
54
77
24
The elements in the list after removing duplicates are :
The nodes in the doubly linked list are :
10
24
54
77

Giải thích

  • Lớp 'Node' đã được tạo.
  • Một lớp khác với các thuộc tính bắt buộc được tạo.
  • Một phương thức khác có tên là ‘remove_duplicates’ được xác định, được sử dụng để xóa các phần tử trùng lặp có trong danh sách được liên kết.
  • Một phương thức khác có tên là 'print_it' được xác định, phương thức này sẽ hiển thị các nút của danh sách liên kết vòng tròn.
  • Một đối tượng của lớp 'double_list' được tạo và các phương thức được gọi trên đó để thêm dữ liệu.
  • Phương thức 'init' được xác định, rằng nút đầu tiên và nút cuối cùng của danh sách liên kết vòng tròn thành Không.
  • Phương thức 'remove_duplicates' được gọi.
  • Nó lặp lại danh sách và kiểm tra xem có phần tử nào được lặp lại hay không.
  • Nếu điều đó là đúng, thì nó sẽ bị xóa.
  • Điều này được hiển thị trên bảng điều khiển bằng phương thức "print_it".