maranovot
maranovot

Reputation: 445

Linked list head doesn't get assigned


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()

Upvotes: 0

Views: 100

Answers (1)

reticentroot
reticentroot

Reputation: 3682

So after looking at your code again, I believe I see where you're having trouble. First you're code runs just fine. When you scroll to the pushCar method you'll see I added a print statement. If you run this code as is you'll see that the head was only every empty once and that you emptyCheck returns False. The issue arises if you print(db) first and then check the head. The reason being is in your __str__ definition. You iterate over the linked list until None, but never reset the Linked list. In other words you are consuming the data, and setting the head to None at the end. I went ahead and updated your __str__ method so that a copy of the data is stored before iteration and then using that copy the linked list is reset. Another way to patch it is instead of iterating over the self.head in your __str__ method is to use ref = self.head like you've been doing.

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())

Upvotes: 1

Related Questions