LinkedList

1. The structure of LinkedList

 http://images.cnitblog.com/blog/497634/201401/272345393446232.jpg

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() // 0
LinkedList(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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值