刚开始编程。我有链表的问题,我无法正确分配头。它应该是LinkedList中的pushCar()函数,这给我带来了麻烦。如果我尝试添加汽车,头部永远不会被分配。只是找不到问题。如果有人可以看看并指出什么是错的,我将非常感激
感谢
class Node:
def __init__(self, carData, nextNode = None, prevNode = None):
self.next = nextNode
self.prev = prevNode
self.data = carData
class LinkedList:
def __init__(self):
self.head = None
def emptyCheck(self):
return self.head == None
def pushCar(self, carData):
ref = self.head
if ref is None:
self.head = Node(carData)
elif ref.data.price < carData.price:
newNode = Node(carData)
newNode.next = ref
self.head = newNode
else:
while ref.next is not None:
if ref.next.data.price > carData.price:
ref = ref.next
else:
newNode = Node(carData)
newNode.next = ref.next
newNode.prev = ref
ref.next.prev = newNode
ref.next = newNode
return
ref.next = Node(carData)
def popCar(self):
if self.head is None: return None
data = self.head.data
self.head = self.head.next
return data
def printDB(self):
i = 1
ref = self.head
while ref is not None:
print("myCar{} \n".format(i) + str(ref.data))
ref = ref.next
i += 1
def getDB(self):
return self
def getDBHead(self):
return self.head
def arrayPush(self, array):
for i in range(0, len(array)):
cars = Car(array[i][0], array[i][1], array[i][2], array[i][3], array[i][4])
self.pushCar(cars)
def singlePush(self, car):
car = Car(car[0], car[1], car[2], car[3], car[4])
self.pushCar(car)
def __str__(self):
retStr = "LinkedList: \n"
while self.head != None:
retStr += str(self.head.data)
self.head = self.head.next
return retStr
class Car:
def __init__(self, identification, name, brand, price, active):
self.id = identification
self.name = name
self.brand = brand
self.price = price
self.active = active
def __str__(self):
return "ID: %3d" % self.id + "\tNAME:" + self.name + "\tBRAND:" + self.brand + "\tPRICE: %3d" % self.price + "\tSTATUS:" + str(self.active) + "\n"
db = LinkedList()
答案 0 :(得分:1)
所以在再次查看您的代码之后,我相信我会看到您遇到麻烦的地方。首先你的代码运行得很好。滚动到pushCar方法时,您会看到我添加了一个print语句。如果您按原样运行此代码,您会看到头部只有一次空,并且您emptyCheck
返回False
。如果您先print(db)
然后检查头部,就会出现问题。原因在于__str__
定义。您遍历链接列表,直到无,但从不重置链接列表。换句话说,您正在使用数据,并在最后将头设置为None。我继续更新了您的__str__
方法,以便在迭代之前存储数据的副本,然后使用该副本重置链接列表。修补它的另一种方法是在你的__str__
方法中使用ref = self.head
而不是迭代self.head。就像你一直在做的那样。
class Node:
def __init__(self, carData, nextNode = None, prevNode = None):
self.next = nextNode
self.prev = prevNode
self.data = carData
class LinkedList:
def __init__(self):
self.head = None
def emptyCheck(self):
return self.head == None
def pushCar(self, carData):
ref = self.head
if ref is None:
print("Testing to see if head is emtpy, should see this only once")
self.head = Node(carData)
elif ref.data.price < carData.price:
newNode = Node(carData)
newNode.next = ref
self.head = newNode
else:
while ref.next is not None:
if ref.next.data.price > carData.price:
ref = ref.next
else:
newNode = Node(carData)
newNode.next = ref.next
newNode.prev = ref
ref.next.prev = newNode
ref.next = newNode
return
ref.next = Node(carData)
def popCar(self):
if self.head is None: return None
data = self.head.data
self.head = self.head.next
return data
def printDB(self):
i = 1
ref = self.head
while ref is not None:
print("myCar{} \n".format(i) + str(ref.data))
ref = ref.next
i += 1
def getDB(self):
return self
def getDBHead(self):
return self.head
def arrayPush(self, array):
for i in range(0, len(array)):
cars = Car(array[i][0], array[i][1], array[i][2], array[i][3], array[i][4])
self.pushCar(cars)
def singlePush(self, car):
car = Car(car[0], car[1], car[2], car[3], car[4])
self.pushCar(car)
def __str__(self):
retStr = "LinkedList: \n"
copy = self.head
while self.head != None:
retStr += str(self.head.data)
self.head = self.head.next
self.head = copy
return retStr
class Car:
def __init__(self, identification, name, brand, price, active):
self.id = identification
self.name = name
self.brand = brand
self.price = price
self.active = active
def __str__(self):
return "ID: %3d" % self.id + "\tNAME:" + self.name + "\tBRAND:" + self.brand + "\tPRICE: %3d" % self.price + "\tSTATUS:" + str(self.active) + "\n"
db = LinkedList()
db.pushCar(Car(213, 'smallcar', 'some germam car', 1000.0, 'yes'))
db.pushCar(Car(312, 'smallcar', 'some germam car', 2000.0, 'no'))
db.pushCar(Car(419, 'bigcar', 'some germam car', 19210.0, 'yes'))
db.pushCar(Car(520, 'bigcar', 'some germam car', 1234.0, 'no'))
print(db)
print(db.emptyCheck())
print('\n')
print(db)
print(db.emptyCheck())