Topic 3 - Linked List 1
Topic 3 - Linked List 1
Collection hierarchy
Array definition
Array implementation
ArrayList
User-defined
Linked list
Definition
Characteristics
Properties
LinkedList class
Linked list operation
Homogeneous
Node should be of the same structure.
Unlimited size
The node will be created upon addition. No predefined size.
Sequential Access
Traversing the linked list starting from first/head node.
Value
head 2000
head.info 17
head.link 2800
head.link.info 92
Value
current 2000
current.info 17
current.link 2800
current.link.info 92
Compiled & edited by: Zahid Zainal
Linked List Properties (cont.)
current = current.link;
Copies value of current.link (2800) into current
Value
current 2800
current.info 92
current.link 1500
current.link.info 63
Compiled & edited by: Zahid Zainal
Linked List Properties (cont.)
Value
head.link.link 1500
head.link.link.info 63
head.link.link.link 3600
head.link.link.link.info 45
current.link.link 3600
current.link.link.info 45
Runtime
current.link.link.link 0 (that is, NULL)
error
current.link.link.link.info Does not exist
Compiled & edited by: Zahid Zainal
Linked List Operation
Create
Insert element
Insert at the beginning, Insert at the end, Insert at specified
location
Remove element
Remove the first node, Remove the last node, Remove specified
node
Searching
import java.util.Scanner;
import java.util.LinkedList;
Create new
LinkedList object
public class BuiltInLL {
numList.removeLast();
Check whether
list is empty
public void printLL() {
if (head == null)
System.out.println("List is empty"); Reference variable
else { for traversal
Node temp = head;
while (temp != null) {
System.out.println(temp.data);
temp = temp.next;
}
}
}
Check whether
public void removeFirst() { list is empty
if (head == null)
System.out.println("Empty list");
else {
head = head.next;
size--;
}
}
//from previous
LL1.removeFirst();
LL1.removeLast();
LL1.printLL();
System.out.println("***");
LL1.removeLast();
LL1.removeLast();
LL1.printLL();
}
}