Csc248 Topic 3 Linked List
Csc248 Topic 3 Linked List
Prepared by:
Roslan Sadjirin
1
Topic 3: Linked List
Learning Outcomes
At the end of the Topic 3, students should be able to:
▪ Understand the concept of List and Linked List.
▪ Understand the concept of variation of linked list: single linked list, double linked list and circular
linked list.
▪ Define and use the ADT of Linked List and its operations.
▪ List, explain and apply the Linked List in solving a problem
2
Topic 3: Linked List
In topic 3, students will be exposed to the following lessons
▪ The basic concept of Node and its implementation
▪ The basic concept of Linked List and its implementation
▪ The variation of Linked List: Double Linked List, and Circular Linked List.
The lesson will include the implementation of Linked List with Head/First, Tail/Last pointers in Single
Linked List, Double Linked List, and Circular Linked List.
3
Basic Concept:
Linked List Class
Interface
Collection
List
Abstract Class
AbstractList
Concrete Class
LinkedList
4
Basic Concept:
Collection of List
Interface
Collection
List
Abstract Class
AbstractList <implements>
Concrete Class
<extends>
ArrayList LinkedList
5
Basic Concept:
Linked List Structures
▪ LinkedList stores elements anywhere in memory and is linked by a reference/pointer.
▪ LinkedList is a linear collection of objects (called nodes) connected by the reference links
▪ Reference in the last node of a list is set to null.
▪ The variable head refers to the first node in the list, and the variable tail refers to the last node.
▪ If the list is empty, both head and tail are pointing to null.
6
Basic Concept:
Nodes Structures and Definition
▪ In a linked list, each element/data is contained in a structure, called the node.
▪ When a new element/data is added to the list, a node is created to contain it.
▪ Each node is linked to its next neighbourhood.
//Node definition
public class Node <E> {
E data;
Node <E> next;
The head and tail are both null. The list is empty.
(head) (tail)
NULL
8
Basic Concept:
Creating Linked List Structures using Nodes Definition
Step 2: Create the first node and append it to the list:
After the first node is inserted in the list, the head and tail point to this node, as shown below:
A next NULL
9
Basic Concept:
Creating Linked List Structures using Nodes Definition
Step 3: Create the second node and append it into the list:
To append the second node to the list, link the first node with the new node.
(tail)
(head)
NULL
A next B next
10
Basic Concept:
Creating Linked List Structures using Nodes Definition
The new node is now the tail node. So, we should move the tail to point to this new node.
tail = tail.next;
(tail)
(head)
NULL
A next B next
11
Basic Concept:
Creating Linked List Structures using Nodes Definition
Step 4: Create the third node and append it into the list:
To append the new node to the list, link the last node in the list with the new node.
(tail)
(head)
C next
B next
A next NULL
12
Basic Concept:
Creating Linked List Structures using Nodes Definition
The new node is now the tail node. Again, we should move the tail to point to this new node.
tail = tail.next;
(tail)
(head)
C next
B next
A next NULL
13
Basic Concept:
Creating Linked List Structures using Nodes Definition
To add new node (data) as the second element (after the head), we use the following statement:
(head)
C next
B next
A next X next NULL
14
Basic Concept:
Creating Linked List Structures using Nodes Definition
▪ Each node contains the element/data, and a field named next that points to the next node.
▪ If the node is the last in the list, its next pointer contains the null value or in other words is
pointing to a null value.
▪ We can use this property to detect the last node.
▪ For example, we may write the following loop to traverse all the nodes in the list:
Node<String> current = head;
while (current != null) {
System.out.println(current.data);
current = current.next;
}
▪ The variable current points initially to the first node in the list.
▪ In the loop, the element of the current is retrieved, and the current point to the next node.
▪ The loop continues until the current node is null.
15
Basic Concept:
Creating Linked List Structures using Nodes Definition
▪ Write statements to demonstrate the following:
Question 1: How do we remove the first element (head) and assign the second element as the new
head?
Question 2: How do we remove the last element (tail) and assign the second last element as the new
tail?
Question 3: Display the output afterwards that indicates the newly assigned head and tail.
(tail)
(head)
C next
B next
A next X next NULL
16
Basic Concept:
Nodes Structures & Another Sample Application
▪ This sample of program demonstrates the application of Nodes without using LinkedList ADTs
Output:
17
Implementation of LinkedList:
Some of the Linked List Methods / Operations of LinkedList ADTs
– (for a complete definition, refer to the ADTs provided)
Methods Description
+removeFirst() : E Returns and removes the first element from this list.
+removeLast(): E Returns and removes the last elements from this list. 18
Implementation of LinkedList:
Definition of LinkedList ADTs (for a complete definition, refer to the ADTs provided)
………… NULL
…………
19
Implementation of LinkedList:
Definition of LinkedList ADTs (for a complete definition, refer to the ADTs provided)
data next
NULL
[newNode ] 20
Implementation of LinkedList:
Definition of LinkedList ADTs (for a complete definition, refer to the ADTs provided)
[newNode]
21
Implementation of LinkedList:
Definition of LinkedList ADTs (for a complete definition, refer to the ADTs provided)
public E getFirst() {
if (this.isEmpty()) {
return null; (head)
} (tail)
else {
this.current = this.head; (current)
data next
return this.current.data;
}
} NULL
data next
public E getLast() {
if (this.isEmpty()) {
(head)
return null;
} (tail)
else {
(current)
return this.tail.data; data next
}
}
NULL
data next
else {
this.current = this.head;
data next (tail)
this.head = this.head.next;
if (this.head == null)
this.tail = null;
data next
return current.data;
}
}
NULL
Output:
26
Variation of LinkedList
Limitation of Single-LinkedList
▪ Insertion at the front of the list is O(1).
Insertion at other position is O(n), where n
is the size of the list.
▪ We can insert a node only after the node
for which we have a reference.
❑ For example, to insert “Bob”, we needed
a reference to the node containing
“Harry”.
❑ If we wanted to insert “Bob” before
“Sam” but did not have a reference to
“Harry”, we would have to start at the
beginning of the list and search until we
found a node whose next node was
“Sam”.
27
Variation of LinkedList
Limitation of Single-LinkedList
▪ We can remove a node only we have a
reference to its predecessor node.
❑ For example, to remove “Dick” in Figure 2,
we needed a reference to the node
containing “Tom”.
❑ If we wanted to remove “Dick” without
having this reference, we would have to
start at the beginning of the list and search
until we found a node whose next node
was “Dick”.
▪ We can traverse the list only in the forward
direction, whereas with an ArrayList we can
move forward (or backward) by incrementing
(or decrementing) the index.
28
Variation of LinkedList
Double-LinkedList
▪ We can overcome these limitations
by adding a reference to the
previous node in the Node class,
called double-linked list.
▪ A double-linked list object would
consists of:
1. A separate object data field
2. Head (a reference to first list Node)
3. Tail (reference to the last list Node)
▪ Because both ends of the list are
directly accessible, now insertion at
either end is O(1);
29
Variation of LinkedList
Node Definition of Double-LinkedList
public class Node <E> {
E data;
DoubleLinkedList
Node <E> next = null;
head =
Node <E> prev = null; tail =
30
Variation of LinkedList
Inserting into a Double-LinkedList
▪ If sam is a reference to the node containing “Sam”, we can insert a new
node containing “Sharon” into the list before “Sam” using the following
statements.
❑ Before the insertion, we can refer to the predecessor of sam as sam.prev.
❑ After the insertion, the node will be referenced by sharon.prev.
31
(I)
Variation of LinkedList
Removing from a Double-LinkedList
▪ If we have reference, harry to the node that
contains “Harry”, we can remove that node without
having a named reference to its predecessor:
harry.prev.next = harry.next; // step 1
harry.next.prev = harry.prev // step 2
(III) (II)
(IV)
32
Variation of LinkedList
Removing from a Double-LinkedList
(I)
▪ If we have reference, harry to the node that
contains “Harry”, we can remove that node without
having a named reference to its predecessor:
harry.prev.next = harry.next; // step 1
harry.next.prev = harry.prev // step 2
(II)
33
Variation of LinkedList
Circular-LinkedList
▪ We can create circular list from a
double-linked list by linking the last
node to the first node (and the first
node to the last one).
▪ If head references the first list node
and tail references the last list node,
the statements:
head.prev = tail;
tail.next = head;
▪ We could also create a circular list
from a single-linked list by executing
statement:
tail.next = head;
34
Variation of LinkedList
Circular-LinkedList: Advantage
Advantage:
▪ We can continue to traverse in the
forward (or backward) direction even
after you have passed the last (or first)
list node. This enables you to visit all
the list elements from any starting
point.
▪ A second advantage of a circular list is
that you can never fall off the end of
the list.
Disadvantage:
▪ We must be careful not to set up an
infinite loop.
35
References
1. Y. Daniel Liang (2019). Introduction to Java Programming and Data Structures, Comprehensive
Version (11th Edition). United Kingdom: Pearson Education Limited.
2. Peter Drake (2014). Data Structures and Algorithm in Java (1st Edition). USA: Pearson Education
Limited.
Approved by:
Zuriati Ismail
Resource Person CSC248
36