Как исправить это сообщение об ошибке
В продолжение моих предыдущих постов с помощью и советами мне удалось закодировать хорошую часть моей программы без ошибок, но прямо сейчас, когда я пытаюсь объединить сортировку linkedList, я получаю ошибку, как показано ниже...
Traceback (most recent call last): File "C:\Users\TP_baseline\Desktop\DSAG\ProjecTest4.py", line 124, in <module> head --> 30 --> 20 --> 10 --> End ll.mergeSort() File "C:\Users\TP_baseline\Desktop\DSAG\ProjecTest4.py", line 90, in mergeSort l1, l2 = divideLists(head) NameError: name 'divideLists' is not defined
Что я уже пробовал:
Мой Существующий Код Выглядит Следующим Образом..
class Node: def __init__(self, val, next_ref): self.val = val; self.next = next_ref; head= None; class LinkedList: def AddBookToFront(val): global head; new_node = Node(val, None) new_node.next = head head = new_node # inserts new node at specific position in singly linked list. def AddBookAtPosition(val, position): global head; current_node = head; while(position > 1): position -= 1; current_node = current_node.next; temp_next = current_node.next; node = Node(val, temp_next); current_node.next = node; # prints singly linked list values. def DisplayBook(): global head; print("Single linked list"); current_node = head; print ("head -->",); while(current_node is not None): print (current_node.val, "-->",); current_node = current_node.next; print ("End"); def RemoveBookAtPosition(position): global head; # If linked list is empty if head == None: return # Store head node temp = head # If head needs to be removed if position == 0: head = temp.next temp = None return # Find previous node of the node to be deleted for i in range(position -1 ): temp = temp.next if temp is None: break # If position is more than number of nodes if temp is None: return if temp.next is None: return # Node temp.next is the node to be deleted # store pointer to the next of node to be deleted next = temp.next.next # Unlink the node from linked list temp.next = None temp.next = next def mergeLists(l1, l2): temp = None if l1 is None: return l2 if l2 is None: return l1 if l1.data <= l2.data: temp = l1 temp.next = mergeLists(l1.next, l2) else: temp = l2 temp.next = mergeLists(l1, l2.next) return temp # Defining function which will sort the linked list using mergeSort def mergeSort(): global head; if head is None or head.next is None: return head l1, l2 = divideLists(head) l1 = mergeSort(l1) l2 = mergeSort(l2) head = mergeLists(l1, l2) return head # Defining function which will divide a linked list into two equal linked lists def divideLists(): global head; slow = head # slow is a pointer to reach the mid of linked list fast = head # fast is a pointer to reach the end of the linked list if fast: fast = fast.next while fast: fast = fast.next # fast is incremented twice while slow is incremented once per loop if fast: fast = fast.next slow = slow.next mid = slow.next slow.next = None return head, mid ll=LinkedList ll.AddBookToFront(10); ll.AddBookToFront(20); ll.DisplayBook(); ll.AddBookToFront(30); ll.DisplayBook(); ll.AddBookAtPosition(45, 2); print ("After insert node at 2"); ll.DisplayBook(); ll.RemoveBookAtPosition(2) print ("After removal of node @ 2nd position"); ll.DisplayBook(); ll.mergeSort()
Я был бы признателен, если бы кто-нибудь просветил меня о том, как исправить эту ошибку.
Спасибо