0


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()
maranovot
  • 405
  • 1
  • 6
  • 17

1 Answers1

1

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())
reticentroot
  • 3,612
  • 2
  • 22
  • 39
  • In `push` why do you assign `.data` again? – tijko Nov 20 '16 at 23:03
  • @tijko oh I see what what you're talking about, I didn't notice that the Node instantiation passes it in as well. Updated the code just now, Thanks. – reticentroot Nov 20 '16 at 23:06
  • @reticentroot Sorry about that, just realised that code I posted just didn't make any sense. Now it should be correct. Could you check it? I want the cars to be sorted by price (from high to low). The problem with head still persists. – maranovot Nov 20 '16 at 23:09
  • @M.Novotny Your code seems fine. I'm not sure what you mean when you say you have a problem with the head.. If you want to print the head instead of the instance you need to print the data. def getDBHead(self): return self.head.data – reticentroot Nov 21 '16 at 00:20
  • @reticentroot When I run the code and try to add car the head never gets assigned, so the only code that runs inside the correctedPushCar is with the condition "if ref is None". When I try to debug it I can clearly see that the LinkedList head is always None. I'll mark it for you. – maranovot Nov 21 '16 at 10:43
  • But that's just not what I see. When I run your the code the head is being assigned. The cars are printing out in order of highest to lowest price. None of the nodes in the structure are None. So that means the head is being asigned. In fact if the head weren't getting assigned the sorting portion of your code wouldn't work, but it does. – reticentroot Nov 21 '16 at 14:36
  • @M.Novotny see the new code above and explanation. Hope this helps. – reticentroot Nov 21 '16 at 19:54