简体   繁体   English

无法访问python链表中分配的值

[英]Unable to access values assigned in linked list in python

I was given an assignment to delete the elements in a linked list whose sum is 0. I have tried this code. 给我分配了删除链表中总和为0的元素的任务。我已经尝试过此代码。 But I get an error like "AttributeError: 'NoneType' object has no attribute 'data'" 但是我收到类似“ AttributeError:'NoneType'对象没有属性'data'的错误”

import random
class Node :
    def __init__( self, data ) :
        self.data = data
        self.next = None
        self.prev = None
    def givedata(self):
        return self.data    

class LinkedList :
    def __init__( self ) :
        self.head = None        

    def add( self, data ) :
        node = Node( data )
        if self.head == None :  
            self.head = node
        else :
            node.next = self.head
            node.next.prev = node                       
            self.head = node            

    def search( self, k ) :
        p = self.head
        if p != None :
            while p.next != None :
                if ( p.data == k ) :
                    return p                
                p = p.next
            if ( p.data == k ) :
                return p
        return None

    def remove( self, p ) :
        tmp = p.prev
        p.prev.next = p.next
        p.prev = tmp

    def list_print(self):
        node = self.head 
        while node:
            print (node.data,end=" ")
            node = node.next

    def getdata(self):
        self.nodedata=self.head
        return self.nodedata.data

script: 脚本:

ll=LinkedList()
for i in range(random.randint(2,10)):
    ll.add(random.randint(-50,50))  
ll.list_print()
print('\n')
l1=LinkedList()
while ll:
  while l1:
     if l1.getdata()+ll.getdata()==0:
            print("0\n")#just to know if this works or not
        l1=l1.head.next
   ll=ll.head.next

I tried to access data in the linked list of a particular element. 我试图访问特定元素的链表中的数据。 Will you please help me in traversing through linked list 您能帮我遍历链表吗

You may choose to omit some but you cannot choose to add further attributes beyond those defined. 您可以选择省略一些属性,但不能选择添加除已定义属性之外的其他属性。

This error is simply saying that the attribute data-width cannot be used where you used it. 这个错误只是在说不能在使用属性的地方使用数据宽度。

You are getting this error because 您收到此错误是因为

l1=LinkedList()

creates a new linked list named l1 and you have not added any element to it, it is an empty linked list whose head is None. 创建一个名为l1的新链表,并且您尚未添加任何元素,它是一个空链表,其头为None。

When you call 你打电话时

l1.getdata()

you see that error 你看到那个错误

Edit 编辑

ll=LinkedList()
for i in range(random.randint(2,10)):
    ll.add(random.randint(-50,50))  
ll.list_print()
print('\n')
ptr1 = ll.head
while ptr1:
  ptr2 = ptr1.next
  while ptr2:
     if ptr1.data+ptr2.data == 0:
            print("0\n")#just to know if this works or not
        ptr2 = ptr2.next
   ptr1 = ptr1.next

This code should do what you want 这段代码应该做你想要的

When you create l1 = LinkedList() , the .head is initialized to None . 创建l1 = LinkedList().head初始化为None This causes a problem when you call .getdata() , since that method is trying to call self.head.data , which is equivalent to calling None.data . 当您调用.getdata() ,这会导致问题,因为该方法正在尝试调用self.head.data ,这等效于调用None.data

You can fix the .getdata method using: 您可以使用以下方法修复.getdata方法:

def getdata(self):
    self.nodedata=self.head
    if not self.nodedata:
        return 0
    return self.nodedata.data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM