[Tutor] List problem

David Merrick merrickdav at gmail.com
Mon Jul 25 04:19:20 CEST 2011


class Node:
    def __init__(self,initdata):
        self.data = initdata
        self.next = None

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setdata(self,newData):
        self.data = newData

    def setNext(self,newnext):
       self.next = newnext

class UnorderedList:

    def __init__(self):
        self.head = None

    def isEmpty(self):
        return self.head == None

## Adds next item on to the head
    def add(self,item):
        temp = Node(item)
        temp.setNext(self.head)
        self.head = temp

    def length(self):
        current = self.head
        count = 0
        while current !=None:
            count = count + 1
            current = current.getNext()
        return count

    def search(self,item):
        current = self.head
        found = False
        while current != None and not found:
            if current.getData()== item:
                found =True
            else:
                current = current.getNext()
        return found


    def remove(self,item):
        '''Removes item from the List'''

        current = self.head
        previous = None
        found = False
        while not found:
            if current.getData() == item:
                found = True
            else:
                previous = current
                current = current.getNext()
        if previous == None:
            self.head = current.getNext()
        else:
            previous.setNext(current.getNext())

    def getIndex(self,item):
        current = self.head
        index = 0
        found = False
        while current != None and not found:
            if current.getData()== item:
                found = True
            else:
                current = current.getNext()
                index = index + 1
        return index

    def append(self,item):
         '''Adds an item to the end of the List'''

         current = self.head
         previous = None
         while current.getNext() != None:
             previous = current
             current = current.getNext()
         if current.getNext() == None:
             previous  = previous.setNext(current)
             current = current.setNext(item)


myList = UnorderedList()
myList.add(31)
myList.add(77)
myList.add(17)
myList.add(93)
myList.add(26)
myList.add(54)
print(myList.length())
myList.append(24)
print(myList.length())
myList.search(24)

Output

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
>>> [evaluate unorderedList.py]
6
builtins.AttributeError: 'int' object has no attribute 'getNext'
>>>

What do I need to do the append method to fix it?

-- 
Dave Merrick

merrickdav at gmail.com

Ph   03 3423 121
Cell 027 3089 169
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110725/190ed97d/attachment-0001.html>


More information about the Tutor mailing list