C175 09 W21 LinkedLists
C175 09 W21 LinkedLists
Post-test
Objectives
Learn about an implementation of Lists called
a Singly-Linked List
We will first draw lists and discuss them
We will then create a class for elements of
a list, and a class for the list itself
March 3, 2021 © Osmar R. Zaïane : University of Alberta In collaboration with Duane Szafron 2
External Container Diagrams
We can draw an external or
implementation-independent diagram of
a container by just showing its elements.
"Fred"
"Wilma"
"Barney"
Computer Memory
List RAM: Random Access Memory
[“Fred”, 175, True, 2.77]
Float
Integer 3.14
175 String
Object “Hello”
“Fred” Character
8/14/1976 ‘z’
Array of 10 Integers
101 175 272 ? ? ? ? ? ? ?
272
272
101 175
175
101
175 P a u l
272
272 Al i
101 C l a ude
175
101
Complex List Node Diagrams
If the elements of a list are complex objects, it is
not always possible to draw the elements inside
the node.
In this case, an arrow is used in the node to
represent a reference to the element.
This diagram is actually more accurate since the
node always contains a reference to an object
instead of an object.
“Fred”
8/14/1976
March 3, 2021 © Osmar R. Zaïane : University of Alberta 12
Terminology: Elements & Nodes
In these notes we use the term element
to refer to the individual values or objects
that collectively make up the list.
node
element “Fred”
8/14/1976
March 3, 2021 © Osmar R. Zaïane : University of Alberta 13
Singly-Linked Lists
“Fred”
8/14/1976
data next
“Fred” “Wilma” “Barney”
8/14/1976 10/14/1979 3/10/1978
March 3, 2021 © Osmar R. Zaïane : University of Alberta 15
Interface for ADT: Node
setData(element) - set element as the new data
setNext(reference) - set reference as the new next
getData() - returns the data element
getNext() - returns the reference to the next node
getData() getNext()
data next
setData() setNext()
def isEmpty(self):
return self.head == None
def length(self):
return self.size
def length(self):
current = self.head
count = 0
while current != None:
If we do not cache the size count = count + 1
we could traverse the list current = current.getNext()
and count the elements
return count
March 3, 2021
SLinkedList – add(item)
def add(self, item):
# adds an item to list at the beginning We could
temp = SLinkedListNode(item,None)
also have
temp.setNext(self.head) done the
self.head = temp following:
self.size += 1
def add(self, item):
temp = SLinkedListNode(item,self.head)
self.head = temp
add(“Fred”) self.size += 1
size head
2 3 “Wilma” “Barney”
Step 1: construct a node with “Fred”
Step 2: link the next of this node to what head points to
“Fred” Step 3: head is now pointing to this new node
Step 4: update size
March 3, 2021 © Osmar R. Zaïane : University of Alberta 24
SLinkedList – search(item)
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
search(“Wilma”)
3 “Fred” “Wilma” “Barney”
append(“Pebbles”)
3
4 current temp “Pebbles”
current
current = self.head
previous = None
while (current.getNext() != None):
previous = current
current = current.getNext()
if (previous == None):
self.head = None
else:
previous.setNext(None)
self.size -= 1
return current.getData()
3
2 previous current
None
Left as an exercise
There could be other methods such as
peek(), clear(), etc.
visited = []
for i in range(12): #initialize to False, True when visited
visited.append(False)
def getNext(self):
return self.next
def getPrevious(self):
return self.previous
getData() getPrevious()
def setData(self, newData):
self.data = newData
setNext() data next getNext()
def setNext(self, newNext):
self.next= newNext previous
self.head == None
def isEmpty(self):
return self.size == 0
def length(self):
return self.size
return found
return found
Note that we could also
start the traversal from
the tail and use
3 previous
“Wilma” “Barney”
March 3, 2021 © Osmar R. Zaïane : University of Alberta 46
DLinkedList – add(item)
def add(self, item):
# adds an item to list at the beginning
append(“Pebbles”)
34 temp “Pebbles”
Left as an exercise
Try to adapt the methods from SLinkedList Class
10 240 75