just started programming. I have this problem with linked list, where I can't get the head properly assigned. It should be the pushCar() function inside LinkedList, that's giving me trouble. If I try to add a car, the head never gets assigned. Just can't find the problem. If somebody could take a look and point out what's wrong, I would be much grateful.
Thanks
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()