Professional Documents
Culture Documents
Lecture 33
Lecture 33
Lecture 33
Lecture Outline
• Linked lists
– CS1001 Lecture 33 –
Linked lists
def append(self,value):
current = self._top
while current.getNext() != None:
current = current.getNext()
# current now refers to the final Node
temp = Node(value)
current.setNext(temp)
self._size += 1
– CS1001 Lecture 33 – 1
Linked lists
• Sample output:
llst = LinkedList() # empty
llst.add(42) # 42
llst.add(35) # 35 42
llst.add(98) # 98 35 42
llst.add(77) # 77 98 35 42
llst.remove(77) # 98 35 42
– CS1001 Lecture 33 – 2
llst.insert(50,1) # 98 50 35 42
llst.insert(100,0) # 100 98 50 35 42
llst.insert(20,-1) # 20 100 98 50 35 42
llst.insert(200,10) # 20 100 98 50 35 42 200
llst.insert(300,4) # 20 100 98 50 300 35 42 200
llst.append(12) # 20 100 98 50 300 35 42 200 12
– CS1001 Lecture 33 – 3
Linked lists
– CS1001 Lecture 33 – 4
Linked lists
def add(self,value):
temp = Node(value) # create Node to insert
current = self._top
previous = None
found = False
while current != None and not found:
# Find insertion point for value (ascending order)
if current.getData() >= value:
found = True
else: # move to next node
previous = current
current = current.getNext()
– CS1001 Lecture 33 – 5
Linked lists
def insert(self,value,index):
print("Calling add to insert node in correct location")
self.add(value)
def append(self,value):
print("Calling add to insert node in correct location")
self.add(value)
– CS1001 Lecture 33 – 6
Linked lists
• Note that SortedLL also inherited the
search, printList, and remove methods from
LinkedList. Each of these methods will work fine,
however, the search method could be improved.
– CS1001 Lecture 33 – 7
Linked lists
sllst = SortedLL() # empty
sllst.add(10) # 10
sllst.add(20) # 10 20
sllst.add(15) # 10 15 20
sllst.add(5) # 5 10 15 20
sllst.add(30) # 5 10 15 20 30
print(sllst.getSize()) # 5
print(sllst.search(25)) # False
sllst.remove(30) # 5 10 15 20
sllst.remove(5) # 10 15 20
print(sllst.search(15)) # True
sllst.remove(15) # 10 20
sllst.insert(25,1) # Calling add to insert node in correct location
# 10 20 25
sllst.append(12) # Calling add to insert node in correct location
# 10 12 20 25
– CS1001 Lecture 33 – 8
Linked lists
class SortedLL(LinkedList):
def __init__(self):
super().__init__()
def add(self,value):
temp = Node(value) # create Node to insert
current = self._top
previous = None
found = False
while current != None and not found:
# Find insertion point for value (ascending order)
if current.getData() >= value:
found = True
else: # move to next node
previous = current
current = current.getNext()
def insert(self,value,index):
print("Calling add to insert node in correct location")
self.add(value)
def append(self,value):
– CS1001 Lecture 33 – 9
print("Calling add to insert node in correct location")
self.add(value)
def search(self,value):
current = self._top
found = False
stop = False
while current != None and not found and not stop:
if current.getData() == value:
found = True
else:
if current.getData() > value:
stop = True
else:
current = current.getNext()
return found
– CS1001 Lecture 33 – 10