0% found this document useful (0 votes)
69 views36 pages

Csc248 Topic 3 Linked List

Uploaded by

ameeraroslan550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views36 pages

Csc248 Topic 3 Linked List

Uploaded by

ameeraroslan550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

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;

public Node(E data) {


this.data = data;
}
}
7
Basic Concept:
Creating Linked List Structures using Nodes Definition
▪ Example of creating a linked list to hold three nodes. Each node stores a string element/data.

Step 1: declare head and tail:


Node <String> head = null;
Node <String> tail = null;

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:

head = new Node<String> (“A”);


tail = head; (head) (tail)

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.next = new Node(“B”);

(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.next = new Node(“C”);

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

Node <String> newNode = Node<>(“X”);


newNode.next = head.next;
(tail)
head.next = newNode;

(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

+LinkedList() Creates a default empty linked list.

+addFirst(o : E) : void Adds the object to the head of this list.

+addLast(o : E) : void Add the object to the tail of this list.

+getFirst() : E Returns the first element from this list.

+getLast() : E Returns the last element from this list.

Return the next element pointed by the current pointer in


+getNext() : E
the list.

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

public class LinkedList <E> {

private Node<E> head, current, tail;


public LinkedList() {
head = tail = current = null;
}
(head) (tail) (current)
public boolean isEmpty() {
return head == null;
}

………… NULL
…………

19
Implementation of LinkedList:
Definition of LinkedList ADTs (for a complete definition, refer to the ADTs provided)

public void addFirst(E data) {


Node<E> newNode = new Node<E>(data);
newNode.next = this.head;
this.head = newNode; (head)
if(this.tail == null) {
(tail)
this.tail = this.head;
}
(current)
}

data next

NULL

[newNode ] 20
Implementation of LinkedList:
Definition of LinkedList ADTs (for a complete definition, refer to the ADTs provided)

public void addLast(E data) {


Node<E> newNode = new Node<E>(data); (head)
if(this.tail == null) {
this.head = this.tail = newNode; (tail)
}
else { (current)
data next
this.tail.next = newNode;
this.tail = this.tail.next;
}
} NULL
data next

[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

Which node will be returned?


22
Implementation of LinkedList:
Definition of LinkedList ADTs (for a complete definition, refer to the ADTs provided)

public E getLast() {
if (this.isEmpty()) {
(head)
return null;
} (tail)

else {
(current)
return this.tail.data; data next

}
}
NULL
data next

Which node will be returned?


23
Implementation of LinkedList:
Definition of LinkedList ADTs (for a complete definition, refer to the ADTs provided)

public E getNext() { (head)


if (this.current == this.tail) {
(tail)
return null;
} (current)
data next
else {
this.current = this.current.next;
return this.current.data; NULL
data next
}
}

Which node will be returned?


24
Implementation of LinkedList:
Definition of LinkedList ADTs (for a complete definition, refer to the ADTs provided)
public E removeFirst() { (current)
if (this.isEmpty()) {
return null; } (head)

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

Which node will be returned? And what is the current head?


25
Implementation of LinkedList
LinkedList Structures and Sample Application
▪ This sample of program demonstrates the application of Nodes using LinkedList ADTs

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 =

public Node(E data) {


this.data = data;
} NULL
}

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.

Node <String> sharon = new Node <> (“Sharon”);

// link new node to its neighbor


sharon.next = sam; // step 1
sharon.prev = sam.prev; // step 2

//link old predecessor of sam to new predecessor


sam.prev.next = sharon // step 3

//link to new predecessor


sam.prev = sharon // step 4

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

You might also like