class Link:
def __init__(self):
self.iData #data
self.dData #data
self.pNext #next link
class Link:
def __init__(self):
self.pItem #object holding data
self.pNext #next link
class Link:
#using Id instead of id as id is reserved in Python
def __init__(self, Id, dd): #special method to create objects
#with instances customized to a specific initial state
self.iData = Id #data item
self.dData = dd #data item
self.pNext = None #next link in list
def displayLink(self): #display ourself {22, 2.99}
print '{', self.iData, ',', self.dData, '}'
#end class Link
class LinkList:
def __init__(self): #special method to create objects
#with instances customized to a specific initial state
#as private instance variables don't exist in Python,
#hence using a convention: name prefixed with an underscore, to treat them as non-public part
self._pFirst = None #first link on list
#(no links on list yet)
def isEmpty(self): #true if list is empty
return self._pFirst == None
#other methods go here
#end class LinkList
#insert at start of list
def insertFirst(Id, dd):
#make new link
pNewLink = Link(Id, dd)
pNewLink.pNext = pFirst #newLink-->old first
pFirst = pNewLink #first-->newLink
#delete first link
def removeFirst():
#assumes list not empty
pTemp = pFirst #save first
pFirst = pFirst.pNext #unlink it: first-->old next
del pTemp #delete old first
def displayList():
print 'List (first-->last):',
pCurrent = pFirst #start at beginning of list
while pCurrent != None: #until end of list,
pCurrent.displayLink() #print data
pCurrent = pCurrent.pNext #move to next link
#demonstrates linked list
class Link:
#using Id instead of id as id is reserved in Python
def __init__(self, Id, dd): #special method to create objects
#with instances customized to a specific initial state
self.iData = Id #data item
self.dData = dd #data item
self.pNext = None #next link in list
def displayLink(self): #display ourself {22, 2.99}
print '{', self.iData, ',', self.dData, '}',
#end class Link
class LinkList:
def __init__(self): #special method to create objects
#with instances customized to a specific initial state
#as private instance variables don't exist in Python,
#hence using a convention: name prefixed with an underscore, to treat them as non-public part
self._pFirst = None #first link on list
#(no links on list yet)
def isEmpty(self): #true if list is empty
return self._pFirst == None
#insert at start of list
def insertFirst(self, Id, dd):
#make new link
pNewLink = Link(Id, dd)
pNewLink.pNext = self._pFirst #newLink-->old first
self._pFirst = pNewLink #first-->newLink
def getFirst(self): #return first link
return self._pFirst
def removeFirst(self): #delete first link
#assumes list not empty
pTemp = self._pFirst #save first
self._pFirst = self._pFirst.pNext #unlink it: first-->old next
del pTemp #delete old first
def displayList(self):
print 'List (first-->last):',
pCurrent = self._pFirst #start at beginning of list
while pCurrent: #until end of list,
pCurrent.displayLink() #print data
pCurrent = pCurrent.pNext #move to next link
#end class LinkList
theList = LinkList() #make new list
theList.insertFirst(22, 2.99) #insert four items
theList.insertFirst(44, 4.99)
theList.insertFirst(66, 6.99)
theList.insertFirst(88, 8.99)
theList.displayList() #display list
print
while not theList.isEmpty(): #until it's empty,
pTemp = theList.getFirst() #get first link
#display its key
print 'Removing link with key', pTemp.iData
theList.removeFirst() #remove it
theList.displayList() #display empty list
#end main
#demonstrates linked list
class Link:
#using Id instead of id as id is reserved in Python
def __init__(self, Id, dd): #special method to create objects
#with instances customized to a specific initial state
self.iData = Id #data item
self.dData = dd #data item
self.pNext = None #next link in list
def displayLink(self): #display ourself {22, 2.99}
print '{', self.iData, ',', self.dData, '}',
#end class Link
class LinkList:
def __init__(self): #special method to create objects
#with instances customized to a specific initial state
#as private instance variables don't exist in Python,
#hence using a convention: name prefixed with an underscore, to treat them as non-public part
self._pFirst = None #first link on list
#(no links on list yet)
def __del__(self): #destructor (deletes list)
self.__init__()
def insertFirst(self, Id, dd):
#make new link
pNewLink = Link(Id, dd)
pNewLink.pNext = self._pFirst #newLink-->old first
self._pFirst = pNewLink #first-->newLink
def find(self, key):
pCurrent = self._pFirst
while pCurrent.iData != key:
if not pCurrent.pNext:
return None
else:
pCurrent = pCurrent.pNext
return pCurrent
def remove(self, key):
pCurrent = self._pFirst
pPrevious = self._pFirst
while pCurrent.iData != key:
if not pCurrent:
return False
else:
pPrevious = pCurrent
pCurrent = pCurrent.pNext
if pCurrent == self._pFirst:
self._pFirst = self._pFirst.pNext
else:
pPrevious.pNext = pCurrent.pNext
del pCurrent
return True
def displayList(self):
print 'List (first-->last):',
pCurrent = self._pFirst #start at beginning of list
while pCurrent: #until end of list,
pCurrent.displayLink() #print data
pCurrent = pCurrent.pNext #move to next link
#end class LinkList
theList = LinkList() #make new list
theList.insertFirst(22, 2.99) #insert four items
theList.insertFirst(44, 4.99)
theList.insertFirst(66, 6.99)
theList.insertFirst(88, 8.99)
theList.displayList() #display list
print
findKey = 44
pFind = theList.find(findKey)
if pFind:
print 'Found link with key', pFind.iData
else:
print "Can't find link"
remKey = 66
remOK = theList.remove(remKey)
if remOK:
print 'Removed link with key', remKey
else:
print "Can't remove link"
theList.displayList()
#end