1.3 Linked List
1.3 Linked List
TOPIC 1: LIST
Content
1
Linked List Concept
Node
2
Linked List Concept
• Program accesses a linked list via a reference to the first node in list
• Program accesses a subsequent nodes via the reference link stored in
the previous node
• Reference in the last node of a list is set to null
Reference link
Firstnode Lastnode
……
null
3
Linked List Concept
LinkedList Advantages
4
Differences with Array
ArrayList LinkedList
Fast access to elements Slower access to elements
10
11
5
LinkedList in Collection Framework
12
LinkedList Application
import java.util.LinkedList;
class app{
public static void main(String a[]){
LinkedList<Integer> lsNum = new LinkedList<>();
lsNum.add(10);
lsNum.add(20);
lsNum.addFirst(5);
System.out.println(lsNum);
int num = lsNum.get(1);
num = num+2;
lsNum.add(num);
System.out.println(lsNum);
13
6
User Defined Linked List
15
Node Properties
• A node must have data
• A node must have a reference to another node
LinkedList Properties
• Linked list must have a head (the first node)
• Linked list may have tail(the last node)
• Traversing linked list started from head to tail (moving from one node to
the next one) using a next node (next node)
16
7
LinkedList Operations
17
LinkedList Structure
• Class: LinkedList
• Attributes
first node
last node
current node //use to traverse the list
• Methods
constructor
boolean isEmpty() //check whether list is empty
void insertAtFront(Object data) //insert at front of list
void insertAtBack(Object data) // insert at the end of list
Object removeFromFront() //delete element form front
Object removeFromBack() //delete element form back
Object getFirst() //get the first node
Object getNext() //get the reference for the next node
18
8
Summary
List Concepts
List in Collection Frameworks
19