Jan-09-2018, 01:02 PM
(Jan-09-2018, 12:58 PM)mpd Wrote: First of all, you're doing your class kind of wrong. Every class method has aself
argument with which you reference variables and methods within that class.
So instead of:
head = None class Linked: def foo(): global head head = Node()You should have this:
class Linked: def __init__(self): """This is the class constructor. You can initialize "member variables" here. """ self.head = None def AddBookToFront(self, val): # you can reference head with self.head tmp = self.head self.head = Node() self.head.next = tmpThen to reference other methods within the same class, you also use self:
class Linked: def __init__(self): """This is the class constructor. You can initialize "member variables" here. """ self.head = None def mergeSort(self): # do stuff... self.divideList(self.head)In any event you can't call divideList because it's part of the LinkedList class. It might work if you callLinkedList.divideList(head)
but you should really consider making the other changes I mentioned because that's the correct way to write classes in Python.
Edited to add:
You don't create class instances ("instantiate") a class withll = LinkedList
; that just creates an alias or another way to refer to LinkedList.
You create a class instance with:ll = LinkedList()
which invokes the constructor.
Thanks MPD, still trying to figure my way through Python. Would you mind if I PM you my rectified code after I have it rectified ?
Thanks A Million
Jay