8: Linked Lists

Example 1: Page 146

In [1]:
class Link:
	def __init__(self):
		self.iData	#data
		self.dData	#data
		self.pNext	#next link

Example 2: Page 147

In [2]:
class Link:
	def __init__(self):
		self.pItem	#object holding data
		self.pNext	#next link

Example 3: Page 150

In [3]:
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

Example 4: Page 151

In [4]:
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

Example 5: Page 152

In [5]:
#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

Example 6: Page 153

In [6]:
#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

Example 7: Page 154

In [7]:
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

Example 8: Page 155

In [8]:
#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
List (first-->last): { 88 , 8.99 } { 66 , 6.99 } { 44 , 4.99 } { 22 , 2.99 }
Removing link with key 88
Removing link with key 66
Removing link with key 44
Removing link with key 22
List (first-->last):

Example 9: Page 158

In [9]:
#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
List (first-->last): { 88 , 8.99 } { 66 , 6.99 } { 44 , 4.99 } { 22 , 2.99 }
Found link with key 44
Removed link with key 66
List (first-->last): { 88 , 8.99 } { 44 , 4.99 } { 22 , 2.99 }