The LinkedList class extends AbstractSequentialList and implements the List, Deque, and Queue interfaces. It provides a linked-list data structure. LinkedList is a generic class that has this declaration:
class LinkedList
E specifies the type of objects that the list will hold.
A demonstration of a linked list of nodes
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class Main {
public static void main(String[] args) {
List ls = new LinkedList();
String[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
for (String weekDay : weekDays){
ls.add(weekDay);
}
dump("ls:", ls);
ls.add(1, "A");
ls.add(3, "B");
ls.add(5, "C");
dump("ls:", ls);
ListIterator li = ls.listIterator(ls.size());
while (li.hasPrevious()){
System.out.print(li.previous() + " ");
}
}
static void dump(String title, List ls) {
System.out.print(title + " ");
for (String s : ls){
System.out.print(s + " ");
}
System.out.println();
}
}
Constructor
LinkedList()
Creates an empty list.
LinkedList(Collection extends E> c)
Creates a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
Add element to LinkedList
boolean add(E e)
Appends the specified element to the end of this list.
void add(int index, E element)
Inserts the specified element at the specified position in this list.
boolean addAll(Collection extends E> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
boolean addAll(int index, Collection extends E> c)
Inserts all of the elements in the specified collection into this list, starting at the specified position.
void addFirst(E e)
Inserts the specified element at the beginning of this list.
void addLast(E e)
Appends the specified element to the end of this list.
boolean offer(E e)
Adds the specified element as the tail (last element) of this list.
boolean offerFirst(E e)
Inserts the specified element at the front of this list.
boolean offerLast(E e)
Inserts the specified element at the end of this list.