Data structures - lists List, ArrayList, LinkedList


title: 'Data Structure - list List, the ArrayList, the LinkedList'
DATE: 2019-11-16 10:42:41
the Categories:

  • Data structure
    tags:
  • data structure
  • List

ADT Abstract Data Types

  • It is a collection of a number of objects with a set of operations

A particular abstract type - ADT table

What is a table it?

A simple integer form -> an array consisting of a group of integers, can be seen as a table

//表的简单数组实现
int[] arr = new int[10];
...
//对一个表的扩容
int[] newArr = new int[arr.length*2];
for(int i=0;i<arr.length;i++){
    newArr[i] = arr[i];
}
arr =newArr;

Achieve linear array is performed, for which the search operation is constant time, but insertions and deletions lurks expensive overhead. In both cases the worst case is O (N)

Simple list

So in order to avoid the overhead insertion and deletion there appeared list. Chain is composed of a node, and not to limit need to be stored in a contiguous memory.

node

Each node contains one table element, comprising a subsequent node in the chain Link.

//链表节点
LinkNode class{
    Object var;
    LinkNode next;
}

For the linked list node deletion and insertion, only a constant time.

First find the location you want to delete a node or nodes where you want to insert

To delete a node before the node, point to the successor of this node can be a

Find the insertion node is inserted before a node, a successor node before the node with the node point, a pointer to the node prior to a node

Doubly linked list

For a single list of deficiencies that deletion of the end node, you need to find the last item node, turned this into next chain changed to null, and then update the chain holding the last node. But in the classic linked list, each node is stored at a node, it does not provide any information precursor node of the last node.

To delete a single list need to rely on the secondary node, and doubly linked list can be deleted directly

For this, I thought of a doubly linked list, so that each node holds a pointer to its predecessor node chain in the table.

JAVA in the Collections API tables

Java with the concept of a collection of abstract interfaces Collection, it stores a set of objects of the same type

//源码参考
public interface Collection<E> extends Iterable<E> {
 	int size();
     boolean isEmpty();
    boolean contains(Object o);
    Iterator<E> iterator(); //该方法继承于Iterable接口内的Iterator方法
    boolean add(E e);
    boolean remove(Object o);
}

Collection interface Iterator interface has been extended to achieve Iterator interface in those classes have enhanced for loop.

//Iterator接口
public interface Iterator<E> {
    boolean hasNext(); 
    E next();
    default void remove() {
        throw new UnsupportedOperationException("remove");
    }
    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }
}

List interface, ArrayList class, LinkedList class

List interface is both abstract table, he inherited the Collection

public interface List<E> extends Collection<E> {
     int size();
    boolean isEmpty();
    boolean contains(Object o);
    Iterator<E> iterator(); 
    Object[] toArray();

    
    E get(int index);
    E set(int index, E element);
    void add(int index, E element);
    E remove(int index);
    
    / ** 
        *从列表中的指定位置开始,以正确的
        *顺序返回列表中元素的列表迭代器。
        *指定的索引表示首次调用{@link ListIterator#next next}将返回的第一个元素。 
        *初次调用{@link ListIterator#previous previous}*返回具有指定索引减一的元素。 
        * * @param index从列表迭代器返回的第一个元素的索引(通过调用{@link ListIterator#next next}* @在此列表中的元素上返回列表迭代器(按适当的顺序) ,从列表中的指定位置开始
        * @throws IndexOutOfBoundsException如果索引超出范围
        *{@code index <0 || index> size()}* /
    ListIterator<E> listIterator(int index); 

ArrayList and LinkedList implementation is two kinds List

ArrayList single list

  • It is a growable array realization

  • Advantage is that the cost of the call to get and set the time constant

  • The disadvantage is the cost of expensive add and remove

Implementation and Analysis of ArrayList

Doubly linked list LinkedList

  • Achieve a doubly-linked list
  • Advantage is that small add and remove overhead
  • And provides addFirst, removeFirst, addLast, removeLast, getFirst, getLast the like
  • The disadvantage is that he is not easy to index, call to get in is expensive

To be continued. . .

Published 98 original articles · won praise 105 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_34691097/article/details/103097013