Professional Documents
Culture Documents
Agenda
1. Arrays
2. Singly Linked List
3. Doubly Linked List
4. Special Lists
2
RMIT Classification: Trusted
Arrays
RMIT Classification: Trusted
Array
• Arrays are objects of a “built-in” class in Java, which can be
used to store a fixed number of elements of the same type.
• There are other types of “arrays” implemented in packages
• Elements in an array can be accessed via indices, ranging
from 0 to (array.length – 1).
4
RMIT Classification: Trusted
Array
In Java - an array is formed via 3 steps: declare, create, and
initialise.
// declare -> no memory allocation at this point
int[] a;
// create -> allocate memory
a = new int[5];
// initialize ; only assign one element at a time
a[0] = 1;
…
// another way: int[] a = {1, 2, 3, 4, 5};
5
RMIT Classification: Trusted
Array
• Without initialisation, array elements are set to default
values
o 0 for numeric data types
o \u0000 for char types
o false for Boolean types
• If you want to change the number of elements in an array,
the only way is to create a new array then clone the data to
the new array
6
RMIT Classification: Trusted
Multi-dimensional Array
You can use a multi-dimensional array to store a matrix or a
table (first brackets are rows; second brackets are columns)
Multi-dimensional Array
• Elemental arrays can have different lengths, but
must be of the same type.
int[][] b = {
{1,2,3,4},
{0,100}
1 2 3 4
}; b[0]
b[1]
0 100
8
RMIT Classification: Trusted
Multi-dimensional Array
• Elements of array can also be arrays. Thus n-dimensional
arrays can be created:
9
RMIT Classification: Trusted
Multi-dimensional Array
// create integer array of size 2*3*4
int[][][] a = {
{{0,0,0,0}, {0,0,0,1}, {0,0,1,0}},
{{0,1,0,0}, {1,0,0,0}, {1,1,1,1}}
};
{0,0,0,0} {0,0,0,1} {0,0,1,0}
{0,1,0,0} {1,0,0,0} {1,1,1,1}
// 1st row, 2nd column, 1st element
a[0][1][0] = 0;
10
RMIT Classification: Trusted
Linked List
• Linked list is a sequence of nodes; each node has connection(s) to the
node(s) next to it.
• What is a node? Think about a cabin in a train
o Space to contain data (the cabin)
o Reference(s) to the next node(s) (the hooks)
• Add/insert/remove elements → just update the hooks
15
RMIT Classification: Trusted
16
RMIT Classification: Trusted
17
RMIT Classification: Trusted
SLL – Append
public class SLL {
Node head, tail;
int length;
SLL – Append
How about appending to the beginning of the SLL?
1. Create the new node and update its data
2. Hook it to the head
• newNode.next = head
3. Update head reference
• head = newNode
19
RMIT Classification: Trusted
21
RMIT Classification: Trusted
SSL – Search
• Find 𝑖𝑡ℎ node: starting from head node, then traverse the
list to reach the node of interest → it is O(n), however,
traversing is very fast.
Floyd’s Algorithm
The pattern of Hare
and Tortoise
movements are
shown below.
Hare Tortoise
1 1
3 2
Complexity: 5 3
Time Complexity: O(n) 7 4
Space 3 5
Complexity: O(1) 5 6
7 7
25
RMIT Classification: Trusted
26
RMIT Classification: Trusted
27
RMIT Classification: Trusted
class DNode {
// data of generic type T
T x;
// reference to adjacent DNodes
DNode prev, next;
}
29
RMIT Classification: Trusted
30
RMIT Classification: Trusted
32
RMIT Classification: Trusted
33
RMIT Classification: Trusted
DLL – Search
• Starting either from head node or tail node, then traverse
the list to reach the node of interest
• O(1 + min{i, n − i}) to reach ith node (twice faster than in
SLL)
• However, how you know from which end to start??
34
RMIT Classification: Trusted
DLL – Search
Node getNode(int i) { Does that mean DLL is
Node p = null;
if (i < n / 2) {
better than SLL?
p = head;
for (int j = 0; j < i; j++) → There are twice
p = p.next;
} else {
number of references
p = tail; to handle.
for (int j = n-1; j > i; j--)
p = p.prev;
}
return p; Get and set value of ith node:
} similar to SLL
35
RMIT Classification: Trusted
u.prev = w.prev;
u.next = w;
u.next.prev = u;
u.prev.next = u;
length++;
return u;
}
36
RMIT Classification: Trusted
37
RMIT Classification: Trusted
40
RMIT Classification: Trusted
Special Lists
RMIT Classification: Trusted
Array-based List
• Depend on the application, either the constant time access or the
changeable size gets favour
• If access time is more important, a list can be built on a array
• Array indices play role of references
• An array larger than the list size is normally used to reserve some
space
• If the list size grows exceeded array size, then new array is created
and data is transfer.
42
RMIT Classification: Trusted
Circular List
• In both SLL and DLL, we have to keep track of head and tail
references.
• Mistakes in updating these two references may result in data lost or
program failure
• One way to avoid these reference issues is to use a circular DLL.
• In this structure, the tail and head are connected to each other to make
a circle, and a dummy node (contains no data) is used as a starting
point of the list.
43
RMIT Classification: Trusted
Circular List
44
RMIT Classification: Trusted
To finish…
RMIT Classification: Trusted
46
RMIT Classification: Trusted
Learning Objectives
1. Understand the structure and operations of:
o Array
o Singly and Doubly Linked Lists
o Array-based and Circular Lists
2. Be able to implement the above data structures
47
RMIT Classification: Trusted