Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Computer Science 1001

Lecture 33

Lecture Outline

• Linked lists

– CS1001 Lecture 33 –
Linked lists

• Thus far, we can create an empty linked list, add


nodes to the beginning of the list, search for a value
in the list, and remove nodes containing a specified
data value.

• Suppose that we would like to add Nodes to the


end of the list? Or perhaps add a Node at a specific
location within the list?

• For the former operation we could provide an


append method as follows:

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

• For the latter operation we could provide an insert


method as in:
def insert(self,value,index):
current = self._top
if index > self._size-1: # insert at end of list
self.append(value)
else:
temp = Node(value) # create Node to insert
if index <= 0: # insert at start of list
temp.setNext(self._top)
self._top = temp
else: # move to correct index
for i in range(1,index):
print("i=",i)
current = current.getNext()
temp.setNext(current.getNext())
current.setNext(temp)
self._size += 1

• 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

• What happens if we try to use insert to add a


Node when the list is empty? Adjust the insert
method to correct the problem.

– CS1001 Lecture 33 – 3
Linked lists

• Suppose that we would like to sort the values in a


linked list in ascending order.

• Here, we will provide that functionality in the form


of a new class called SortedLL. Since a sorted linked
list is essentially a linked list with restricted ability
to add nodes to the list, we will implement this new
class as a subclass of the LinkedList class.

• We should only permit addition of nodes to


instances of SortedLL such that they are inserted
in the correct sorted order.

• This would mean that nodes cannot be added


to a specified location (such as the beginning or
end, or at a certain index) as was permitted for a
LinkedList.

– CS1001 Lecture 33 – 4
Linked lists

• We can override the add method of the superclass


to ensure that any nodes added to the list are in the
correct location:

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()

if previous == None: # insert at beginning of list


temp.setNext(self._top)
self._top = temp
else:
temp.setNext(current)
previous.setNext(temp)
self._size += 1

– CS1001 Lecture 33 – 5
Linked lists

• To remove the possibility of appending nodes using


the inherited append method, or inserting nodes
at a specific index using the inherited insert
method, we will provide overriding methods within
the SortedLL class as follows:

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)

• If append or insert is called on an instance of


SortedLL the overriding methods will be called,
each of which will call add to ensure that the new
node is inserted into the correct (sorted) location.

– 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.

• If we are looking for a particular value within a


sorted list we can stop looking as soon as we exceed
the value that we are looking for. Therefore, we
include a way to stop the search early:
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

• This new search method overrides the one inherited


from the LinkedList class.

– 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()

if previous == None: # insert at beginning of list


temp.setNext(self._top)
self._top = temp
else:
temp.setNext(current)
previous.setNext(temp)
self._size += 1

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

You might also like