猿问

这个链表实现有什么问题?

运行脚本会产生错误:


Traceback (most recent call last):

  File "D:/PycharmProjects/GeeksforGeeks/Linked List/Insertion of Nodes in LinkedList.py", line 49, in <module>


llist.printList()

  File "D:/PycharmProjects/GeeksforGeeks/Linked List/Insertion of Nodes in LinkedList.py", line 39, in printList

违规行是:


print(temp.data,end=' ')

AttributeError: 'int' 对象没有属性 'data'


我的代码是:-

class Node:

    def __init__(self,data):

        self.data = data

        self.next = None


class LinkedList:

    def __init__(self):

        self.head = None


    def push(self,new_data):

        new_node = Node(new_data)

        new_node.next = self.head

        self.head = new_node


    def insertAfter(self,prev_node,new_data):

        if prev_node is None:

            return


        new_node = Node(new_data)

        new_node.next = prev_node.next

        prev_node.next = new_node


    def append(self,new_data):

        new_node = Node(new_data)


        if self.head is None:

            self.head = new_node

            return


        last = self.head

        while (last.next):

            last = last.next


        last.next = new_data


    def printList(self):

        temp = self.head

        while (temp):

            print(temp.data,end=' ')

            temp = temp.next


if __name__ == '__main__':

    llist = LinkedList()

    llist.append(6)

    llist.push(7)

    llist.push(1)

    llist.append(4)

    llist.insertAfter(llist.head.next,8)

    llist.printList()


德玛西亚99
浏览 113回答 2
2回答

呼唤远方

在你的方法中append,last.next应该是new_nodeclass Node:&nbsp; &nbsp; def __init__(self,data):&nbsp; &nbsp; &nbsp; &nbsp; self.data = data&nbsp; &nbsp; &nbsp; &nbsp; self.next = Noneclass LinkedList:&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; self.head = None&nbsp; &nbsp; def push(self,new_data):&nbsp; &nbsp; &nbsp; &nbsp; new_node = Node(new_data)&nbsp; &nbsp; &nbsp; &nbsp; new_node.next = self.head&nbsp; &nbsp; &nbsp; &nbsp; self.head = new_node&nbsp; &nbsp; def insertAfter(self,prev_node,new_data):&nbsp; &nbsp; &nbsp; &nbsp; if prev_node is None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; new_node = Node(new_data)&nbsp; &nbsp; &nbsp; &nbsp; new_node.next = prev_node.next&nbsp; &nbsp; &nbsp; &nbsp; prev_node.next = new_node&nbsp; &nbsp; def append(self,new_data):&nbsp; &nbsp; &nbsp; &nbsp; new_node = Node(new_data)&nbsp; &nbsp; &nbsp; &nbsp; if self.head is None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.head = new_node&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; last = self.head&nbsp; &nbsp; &nbsp; &nbsp; while (last.next):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last = last.next&nbsp; &nbsp; &nbsp; &nbsp; last.next = new_node&nbsp; &nbsp; def printList(self):&nbsp; &nbsp; &nbsp; &nbsp; temp = self.head&nbsp; &nbsp; &nbsp; &nbsp; while (temp):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(temp.data,end=' ')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = temp.nextif __name__ == '__main__':&nbsp; &nbsp; llist = LinkedList()&nbsp; &nbsp; llist.append(6)&nbsp; &nbsp; llist.push(7)&nbsp; &nbsp; llist.push(1)&nbsp; &nbsp; llist.append(4)&nbsp; &nbsp; llist.insertAfter(llist.head.next,8)&nbsp; &nbsp; llist.printList()输出:1 7 8 6 4

炎炎设计

函数中有一个错误append:&nbsp; &nbsp; def append(self,new_data):&nbsp; &nbsp; &nbsp; &nbsp; new_node = Node(new_data)&nbsp; &nbsp; &nbsp; &nbsp; if self.head is None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.head = new_node&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; last = self.head&nbsp; &nbsp; &nbsp; &nbsp; while last.next:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last = last.next&nbsp; &nbsp; &nbsp; &nbsp; last.next = new_node # should not be new_data
随时随地看视频慕课网APP

相关分类

Python
我要回答