如何修复此错误消息 [英] How do I fix this error message

查看:89
本文介绍了如何修复此错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我以前帖子的延续,有了帮助和建议,我已经成功编写了我的程序的一部分而没有错误,但是现在因为我正在尝试合并排序linkList我现在收到如下错误...



As a continuation to my previous posts with the help and advise given I have managed to code a good part of my program without errors, but right now as I am attempting to merge sort the linkedList I am now getting an error like below...

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





我有什么试过:



我的现有代码如下:





What I have tried:

My Existing Code Is As Below..

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()                    





如果有人能告诉我如何解决这个错误,我将不胜感激。 />
谢谢



I would be grateful if anyone could enlighten me on how to fix this error.
Thanks

推荐答案

在<$上方移动 divideLists()函数的定义c $ c> mergeSort()定义它的使用位置。



你也在调用 divideLists()这里有一个参数

Move the definition of the divideLists() function above the mergeSort() definition where it is used.

You are also calling divideLists() with an argument here
l1, l2 = divideLists(head)

但函数定义不接受任何参数:

but the function definition does not accept any arguments:

def divideLists():

因此要么将函数定义更改为接受一个参数,或者在没有 head 参数的情况下调用它。

So either change the function definition to accept an argument, or call it without the head parameter.


这篇关于如何修复此错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆