Professional Documents
Culture Documents
DSA610S Theory 2
DSA610S Theory 2
DSA610S Theory 2
Technology
Faculty of Computing and Informatics
Department of Computer Science
2
Objectives
•To understand the nature of dynamic data
structures in the form of singly-linked list,
3
Rationale
• We need to understand these data structures in order
to be able to represent data in a form that we can
operate on them
• Once we have represented these data, then it becomes
easier to develop an algorithm focusing on the given
problem using the chosen data structure
• To be able to select the appropriate data structure for a
given problem
4
Nature of Dynamic Data Structures
• Dynamic data structures can grow and they can shrink
one element at a time
5
List
A list is an ordered collection of elements
supporting random access to each element, much
like an array
• Like arrays, lists make no attempt to preserve the
uniqueness of values, meaning a list may contain
duplicate values
• For example, if you had a list containing the values
“Windhoek”, “Swakopmund”, and “Walvis Bay” and you
were to add “Windhoek” again, you would now find
that the list had grown to include two copies of
“Windhoek”
6
List
These days it is usual to indicate the start of an
index from 0; using java language style of
indexing as follows
Index (memory Value
location)
0 Windhoek
1 Swakopmund
2 Walvis Bay
3 Windhoek
7
Components of a Linked List
• Nodes/Objects:
-Contain data values and object references
• Object references:
-Variables that store the addresses of objects
-Pointer is another name for object reference
-object references can be used to create links between
objects
8
Components of a Singly Linked List (continued…)
The Node
Data
Address to next
Data value(s) node
9
Components of a Singly Linked List
(continued…)
Example of Singly Linked List
7 9 5
Head Tail
10
Operations on a List
Operation Description
Add Adds a value to the end of the list. The size of the list
will increase by one
Insert Inserts a value into a list at a specified position (0, 1,
2, . ., n). The size of the list will increase by one. This
means the position must be valid (exists in the current
list)
Delete Deletes the value at a specified position (0, 1, 2, . ., n) in
a list and returns whatever value was contained therein.
The size of the list will decrease by one
Get Obtains the value at a specified position (0, 1, 2, . ., n) in
the list
Size Obtains the number of elements in the list
11
Operations on a Singly Linked List:
Example for Delete Operation
Delete node at the tail position
Head Tail
7 9 5
1 2
Pointer to tail
Pointer to head node
node
12
Operations on a Singly Linked List:
Example for Delete Operation (cont…)
Delete node at the head position
Head Tail
7 9 5
2
1
Pointer to tail
Pointer to head node
node
13
Operations on a Singly Linked List:
Example for Delete Operation (cont…)
Delete node at a middle position
7 9 5
Head Tail
14
Operations on a Singly Linked List:
Code-Oriented Examples
Operation on the List
Current ‘students’ List Operation Description
Index Value students.add(“Moses”) It will add Moses at index
0 Johannes 5
1 Hosea students.insert(2, “Moses”) It will insert Moses just
after Hosea; at index 2,
2 Maria this means items above
it will all shift one space
3 Mary down
4 Paul students.delete(4) It will delete Paul
students.get(3) It will returns the student
at index 3; which is Mary
students.size() returns 5
Data
Address to Address to
adjacent node adjacent node
Data value(s)
16
Components of a Doubly Linked List
The Node, the Data, and the Reference
Data
Address to Address to
adjacent node adjacent node
Data value(s)
17
Components of a Doubly Linked List
(continued…)
Example of Doubly Linked List
7 9 5
18
Operations on a Doubly Linked List
Delete node at the tail of the list
Head Tail
7 9 5
1 2
Pointer to tail
Pointer to head node
node
19
Operations on a Doubly Linked List
(continued…)
Delete node at the head position
Head Tail
7 9 5
2
1
Pointer to tail
Pointer to head node
node
7 9 5
Head Tail
21
Operations on a Linked Lists
22
Operations on a Linked Lists
7 5
Newly created
Node
23
Operations on a Linked Lists
(Continued…)
7 9 5
24
Questions/Comments
?!
25
Take Home Exercise
26
End of Lecture
27