链接列表中的类不起作用

问题描述 投票:0回答:2

做了一个功课,做了两个类的陈列室:Car&Node,而Node数据包含Car类及其数据。然后我必须制作一个链接列表,应该加倍,但我认为一个简单应该也可以,包含汽车的节点。链接列表中唯一的功能是插入一个带有新Car的新节点。它应该按汽车价格向上排序。

试过这个,但它一直在告诉我

TypeError:'NoneType'和'NoneType'实例之间不支持'<'

class Car:
    def __init__(self, id = None, name = None, brand = None, price = None, active = None):
        self.id = id
        self.name = name
        self.brand = brand
        self.price = price
        self.active = active

class Node:
    def __init__(self, data):
        self.next = None
        self.data = Car()

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

    def insertNode(self, car):
        newNode = Node(car)
        curNode = self.head
        if self.head.data is None:
            self.head = newNode
        if newNode.data.price < curNode.data.price:
            newNode.next = curNode
            self.head = newNode
        else:
            while curNode.next is not None and curNode.next.data.price <= newNode.data.price:
                curNode = curNode.next
            newNode.next = curNode.next
            curNode.next = newNode
db = LinkedList()

def init(cars):
    for car in cars:
        db.insertNode(car)

def add(car):
    db.insertNode(car)

我错过了什么,因为我认为它应该有效。

python class linked-list
2个回答
0
投票

Node.__init__中,你有self.data = Car()这一行,它使用默认值(都是Car)创建一个None实例。这意味着列表中每个Car的价格都是None。之后,当您尝试将价格与newNode.data.price < curNode.data.price进行比较时,您将获得例外,因为您无法比较None值。

您可能需要用其他东西替换self.data = Car()。从您当前的代码来看,我的汽车数据应该来自哪个格式或者它的格式是不明显的。如果你在顶级cars函数中迭代的init序列已经是Car实例的列表,那么你应该只是在self.data = dataNode.__init__。否则,您将需要执行其他操作来提取汽车数据并将其传递给Car构造函数。您可能还想考虑摆脱Car.__init__的默认参数,因为创建没有它们的汽车似乎没有多大意义。


0
投票

我在代码中看到了几个问题,主要是关于类型一致性。您应该重新考虑这里的类结构究竟应该做什么,如何设置默认值等。另外:您是否希望将下一个节点变量作为另一个节点?

假设如下:我们有汽车。每辆车都有名称,品牌和价格。我们希望通过Node连接两辆车。我们需要能够容纳节点并允许我们插入新节点的列表。应该进行插入,使得节点层次结构以递增的价格值连接汽车。两个相邻节点共享一辆汽车。你可以这样写:

class Car:
    def __init__(self, name = " ", brand = " ", price = 0):
        self.name = name
        self.brand = brand
        self.price = price

class Node:
    def __init__(self,car1,car2):
        assert type(car1) == type(car2) == Car
        if car1.price > car2.price:
            self.next = car1
            self.current = car2
        else:
            self.next = car2
            self.current = car1

class LinkedList:
    def __init__(self,firstnode):
        self.ListOfNodes = [firstnode]

    def insertNode(self, car):
        for i in range(0,len(self.ListOfNodes)):
            curnod = self.ListOfNodes[i]
            newnod = Node(car,car)
            if  car.price < curnod.current.price: 
                if i<len(self.ListOfNodes): #If its not the last node
                    newnod.next = curnod.current
                if i>0: #If its not the first node, i.e. it has a predecessor
                    ListOfNodes[i-1].next = newnod.current
                self.ListOfNodes.insert(i,newnod)
                return
            elif car.price < curnod.next.price: 
                newnod.current = curnod.current
                curnod.current = car 
                self.ListOfNodes.insert(i,newnod)
                return
        newnod.current = self.ListOfNodes[-1].next #this is only reached if the price is higher than all before
        self.ListOfNodes.append(newnod)




car1 = Car(name = "baby",brand= "honda", price = 1000)
car2 = Car(name = "yomomma",brand= "Benz", price = 10)
car3 = Car(name = "PussyWagon",brand= "Dodge", price = 100)
car4 = Car(name = "FakeTaxi",brand= "HellNah", price = 10000000)
car5 = Car(name = "LondonTaxi",brand= "ChumBucket", price = 1)
p = Node(car2,car1)
lst = LinkedList(p)
lst.insertNode(car3)
lst.insertNode(car4)
lst.insertNode(car5)

它非常通用,但它应该有效。

© www.soinside.com 2019 - 2024. All rights reserved.