Linked List • A linked list consists of a number of links, each of which has a reference to the next link. • Adding and removing elements in the middle of a linked list is efficient. • Visiting the elements of a linked list in sequential order is efficient • Random access is not efficient
35
Embed
Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Linked List
• A linked list consists of a number of links, each of which has a reference to the next link.
• Adding and removing elements in the middle of a linked list is efficient.
• Visiting the elements of a linked list in sequential order is efficient
o void addFirst(Object obj) o void addLast(Object obj)o Object getFirst() o Object getSecond() o Object removeFirst() o Object removeLast()
listIterator provides a way to access all links: ListIterator listIterator(int index)
• listIterator method provides an object of type ListIterator ListIterator is an interface, so the return object from this method provides all the methods in this interface
Implementing Linked Lists • LinkedList class has a private inner class Link
class LinkedList {
private class Link {
public Object data; public Link next;
} }
Implementing Linked Lists • LinkedList class
o Holds a reference first to the first link
o Has a method to get the first element
Implementing Linked Lists class LinkedList {
public LinkedList() {
first = null; }
public Object getFirst() {
if (first == null) throw new
NoSuchElementException(); return first.data;
} . . . private Link first;
}
Adding a New First Element
• When a new link is added to the list o It becomes the head of the list o The old first link becomes the next
link
Adding a New First Element • The addFirst method
class LinkedList {
. . . public void addFirst(Object obj) {
Link newLink = new Link(); newLink.data = obj;
newLink.next = first; first = newLink;
} ...
}
Adding a Link to the Head of a Linked List
Removing the First Element
• When the first element is removed o The data of the first link are saved and later
returned as the method result
o The successor of the first link becomes the first link of the shorter list
o The link will be garbage collected when there are no further references to it
Removing the First Element • The removeFirst method
class LinkedList { . . .
public Object removeFirst() {
if (first == null) throw new NoSuchElementException(); Object obj = first.data; first = first.next; return obj;
} . . .
}
Removing the First Link from a Linked List
LinkedListIterator
• Private inner class of LinkedList
• Implements a simplified ListIterator interface
• Has access to the first field and private Link class
LinkedListIteratorThe LinkListIterator class
class LinkedList { . . .
public ListIterator listIterator() { return new LinkedListIterator(); } private class LinkedListIterator implements ListIterator { public LinkedListIterator() {
position = null; previous = null;
} . . . private Link position; private Link previous;
} . . .
}
LinkListIterator's next Method • position reference is advances to position.next
• Old position is remembered as previous
• If the iterator points before the first element of the list, then the old position is
null and position must be set to first
LinkListIterator's next Method private class LinkedListIterator implements ListIterator { . . .
public Object next() { if (!hasNext())
throw new NoSuchElementException(); previous = position; // remember for remove if (position == null)
position = first; else
position = position.next; return position.data; } . . .
}
LinkListIterator's hasnext Method
• The next method should only be called when the iterator is not at the end of the list
• The iterator is at the end
o if the list is empty (first == null)
o if there is no element after the current position (position.next == null)
LinkListIterator's hasnext Method
private class LinkedListIterator implements ListIterator {
. . . public boolean hasNext() {
if (position == null) return first != null;
else return position.next != null;
}
}
LinkListIterator's remove Method
• If the element to be removed is the first element, call removeFirst
• Otherwise, the link proceeding the element to be removed needs to have its next reference updated to skip the removed element
• If the previous reference is null:
o this call does not immediately follow a call to next