You are on page 1of 20

Linked List

Session 5 part 2
Linked list in java

 What is a linked list?


 A link is basically an object
 Each link has reference to the other link in the list
 The linkedList has only a reference to the last link added to it
Linked list in java

 What is a linked list?


 Each element (we will call it a node) of a list is comprising of two items - the data and a
reference to the next node.
 The last node has a reference to null.
 The entry point into a linked list is called the head of the list.
 It should be noted that head is not a separate node, but the reference to the first node.
 If the list is empty then the head is a null reference.
 A linked list is a dynamic data structure.
 The number of nodes in a list is not fixed and can grow and shrink on demand
Linked list in java

 What is a linked list?


 One disadvantage of a linked list against an array is that it does not allow direct access to
the individual elements.
 If you want to access a particular item then you have to start at the head and follow the
references until you get to that item.
 Another disadvantage is that a linked list uses more memory compare with an array 
Types of LinkedList

 A singly linked list is described in the preeding slides


 A doubly linked list is a list that has two references, one to the next node and another to
previous node.
Types of LinkedList

 Another important type of a linked list is called a circular linked list where last node of
the list points back to the first node (or the head) of the list.
The Structure of Linked Lists

 To understand the inefficiency of arrays and the need for a more efficient data structure,
imagine a program that maintains a sequence of employee names.
 If an employee leaves the company, the name must be removed.
 In an array, the hole in the sequence needs to be closed up by moving all objects that come
after it
 Conversely, suppose an employee is added in the middle of the sequence.
 Then all names following the new hire must be moved toward the end.
 Moving a large number of elements can involve a substantial amount of processing time.
 A linked list structure avoids this movement.
The Structure of Linked Lists

 .
The Structure of Linked Lists

 When you insert a new node into a linked list, only the neighboring node references need
to be updated
 The same is true when you remove a node.
 What’s the catch?
 Linked lists allow efficient insertion and removal, but element access can be inefficient. .
Working with Linked Lists
List Iterators

 An iterator encapsulates a position anywhere inside the linked list.


 Conceptually, you should think of the iterator as pointing between two elements, just as the
cursor in a word processor points between two characters
 Note that the iterator class is also a generic type.
 A ListIterator iterates through a list of strings; a ListIterator visits the elements in a
LinkedList. Initially, the iterator points before the first element.
 You can move the iterator position with the next method:
List Iterators

 iterator.next();
 The next method throws a NoSuchElementException if you are already past the end of the
list.
 You should always call the iterator’s hasNext method before calling next—it returns true if
there is a next element.
 if (iterator.hasNext()) {
 iterator.next();
 }
Methods of the Iterator and ListIterator
Interfaces
Methods of the Iterator and ListIterator
Interfaces
 The nodes of the LinkedList class store two links: one to the next element and one to the
previous one.
 Such a list is called a doubly-linked list.
 You can use the previous and hasPrevious methods of the ListIterator interface to move the
iterator position backward.
 The add method adds an object after the iterator, then moves the iterator position past the
new element. iterator.add("Juliet");
 You can visualize insertion to be like typing text in a word processor.
 Each character is inserted after the cursor, then the cursor moves past the inserted character
Methods of the Iterator and ListIterator
Interfaces
 The remove method removes the object that was returned by the last call to next or
previous.
 For example, this loop removes all names that fulfill a certain condition:
 while (iterator.hasNext()) {
 String name = iterator.next();
 if (condition is fulfilled for name) {
 iterator.remove(); }
 }
Linked list example

public class ListDemo {


void display(){
LinkedList<String> student = new LinkedList<String>();
student.addLast("Diana");
student.addLast("Harry");
student.addLast("Romeo");
student.addLast("Tom");

ListIterator<String> iterator = student.listIterator(); // |Diana,Harry,Romeo, Tom


iterator.next(); // Diana|Harry,Romeo,Tom
iterator.next(); // Diana,Harry|Romeo,Tom

// Add more elements after second element


Linked list example

public class ListDemo {


void display(){
iterator.add("Juliet");
iterator.add("Nina");

iterator.next();

// Remove last traversed element


iterator.remove(); // DHJN|T

// Print all elements


System.out.println(student);
System.out.println("Expected: [Diana, Harry, Juliet, Nina, Tom]");
}
Linked list example

public static void main(String[] args) {


ListDemo p = new ListDemo();
p.display();
}

}
Task 1

Suppose the list letters contains elements "A", "B", "C", and "D".
Draw the contents of the list and the iterator position for the following operations:
ListIterator iter = letters.iterator();
iter.next();
iter.next();
iter.remove();
iter.next();
iter.add("E");
iter.next();
iter.add("F");
Task 2

Write a loop that removes all strings with length less than four from a linked list of strings
called words.

You might also like