1. The structure of LinkedList
2. What is LinkedList?
- A LinkedList is a data structure that behaves as a dynamic double_linked list.
- LinkedList consist of a group of nodes which together represent a sequence, each node is composed of a data and two references.
- LinkedList is not synchronized
List list = Collections.synchronizedList(new LinkedList(...));
3. The definition of LinkedList
java.lang.Object --> java.util.AbstractCollection<E> --> java.util.AbstractList<E> --> java.util.AbstractSequentialList<E> --> java.util.LinkedList<E>
public class LinkedList<E> extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable {}
- LinkedList extends AbstractSequentialList, it can be used
- stack(LIFO)
queue method equal method
push(e) addFirst(e)
pop() removeFirst()
peek() peekFirst()
- queue(FIFO)
queue method equal method
add(e) addLast(e)
offer(e) offerLast(e)
remove() removeFirst()
poll() pollFirst()
element() getFirst()
peek() peekFirst()
- deque(double ended queue)
- LinkedList implement List, Deque, Cloneable, java.io.Serializable interface.
4. property
transient int size = 0;transient Node<E> first;
transient Node<E> last;
5. Constructor in LinkedList
LinkedList() // 0LinkedList(Collection<? extends E> collection)
6. Basic Functions
- linkFirst ----- unlinkFirst linkLast ----- unlinkLast
- linkBefore ----- unlink
- getFirst ----- getLast
- removeFirst ----- removeLast ----- addFirst ----- addLast
- contain -----> indexOf(Object)
- add ----- (element/index, element) addAll ----- (collection/index, collection)
- remove ----- (object/index)
- checkElementIndex checkPositionIndex ----- throw exception
isElementIndex isPositionIndex
7. Advance
- peek ----- element(exception)
- poll ----- remove(exception)
- offer ----- offerFirst -----offerLast (add ----- addFirst ----- addLast)
- peek ----- peekFirst ----- peekLast (retrieves, not remove)
- poll ----- pollFirst ----- pollLast (retrieves, remove)
- push ----- pop
8. LinkedList Traversal
-
for(Iterator iter = list.iterator(); iter.hasNext();) iter.next();
-
int size = list.size(); for (int i=0; i<size; i++) { list.get(i); }
-
while(list.pollFirst() != null) ;
-
while(list.pollLast() != null) ;
-
try { while(list.removeFirst() != null) ; } catch (NoSuchElementException e) { }
-
try { while(list.removeLast() != null) ; } catch (NoSuchElementException e) { }
Note:
// LinkedList的API
boolean add(E object)
void add(int location, E object)
boolean addAll(Collection<? extends E> collection)
boolean addAll(int location, Collection<? extends E> collection)
void addFirst(E object)
void addLast(E object)
void clear()
Object clone()
boolean contains(Object object)
Iterator<E> descendingIterator()
E element()
E get(int location)
E getFirst()
E getLast()
int indexOf(Object object)
int lastIndexOf(Object object)
ListIterator<E> listIterator(int location)
boolean offer(E o)
boolean offerFirst(E e)
boolean offerLast(E e)
E peek()
E peekFirst()
E peekLast()
E poll()
E pollFirst()
E pollLast()
E pop()
void push(E e)
E remove()
E remove(int location)
boolean remove(Object object)
E removeFirst()
boolean removeFirstOccurrence(Object o)
E removeLast()
boolean removeLastOccurrence(Object o)
E set(int location, E object)
int size()
<T> T[] toArray(T[] contents)
Object[] toArray()