1.4 Linked List
1.4 Linked List
TOPIC 1: LIST
Content
1
Linked list Implementation
Object getFirst(){
if(size() > 0){
void insertAtFront(Object d){
current = head;
node temp = new node(d);
return head.data;
}else if(isEmpty()){
return null; head = current = temp;
} }else{
temp.next = head;
Object getNext(){ head = temp;
if(current.next!= null){ }
current = current.next; size++;
}
return current.data;
}else
return null;
}
LinkedList Variation
• LinkedList(singly Linkedlist)
• Double LinkedList
• Circular LinkedList
2
Singly LinkedList
class node{
Object data;
node next;
data next
public node(Object data){
this.data=data;
next=null; node
}
}
Singly LinkedList
class linkedlist{
node head;
node current;
int size;
public linkedlist(){
head=current=null;
size = 1;
}
public void insertAtFront(Object data){
current = new node(data);
if(head == null)
head = current;
else{
current.next = head.next;
head = current;
}
size++;
}}
3
Limitation
Doubly LinkedList
4
Doubly LinkedList
• Every node:
– has a next reference variable and a back reference variable
– contains the address of the next node (except the last node)
– contains the address of the previous node (except the first node)
Doubly LinkedList
10
5
Circular LinkedList
• A linked list in which the last node points to the first node is called
a circular linked list.
• In a circular linked list with more than one node, it is convenient to
make the reference variable first point to the last node of the list.
11
Circular LinkedList
• Advantages
12
6
CircularLinkedList
class circularlinkedlist{
...
public void insertAtBack(Object data){
node newNode= new node(data);
if(head == null){
head = newNode;
newNode.next = head;
else{
current = head;
for(int i=0;i<size-1;i++)
current = current.next;
• newNode.next = head;
• current.next = newNode;
}
...
}}
13
Summary
List Concepts
List in Collection Frameworks
14