Java data structure and algorithm (1): linear form

Linear table is a simple data type, which is a finite sequence of n data elements with the same type of composition. As the form A 0 , A . 1 , ..., A n--. 1 . 0 size table is empty table, said A I successor A I-. 1 , and said A I-. 1 precursor A I .

printList print out the table of elements, makeEmpty home empty table, find returns the position of the first occurrence of an item, insert and remove are typically insert and delete an element from a position table; and findKth elements on a location is returned, next and previous position will be taken as a parameter and returns the precursor membered subsequent values ​​of the cells.

ArrayList implementation

All operations on the table can be achieved through an array. A schematic view of an array of memory as follows:

Features of this configuration is stored: the data is continuous, fast random access speed . printList performed in linear time, findKth operation calls the time constant. For insertion and deletion for efficiency is relatively low, in the worst case, all the elements need to move back one position in the inserted position 0.

Based on the list of the array to achieve:

public class MyArrayList<T> implements Iterable<T>{

    private static final int DEFAULT_CAPACITY=10;
    private int size;
    private T[] items;

    public MyArrayList() {
        doClear();
    }

    public void clear() {
        doClear();
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void trimToSize() {
        ensureCapacity(size);
    }

    public boolean add(T x) {
        add(size, x);
        return true;
    }

    public void add(int idx, T x) {
        if (items.length == size) {
            ensureCapacity(size * 2 + 1);
        }
        for (int i = size; i > idx; i--) {
            items[i] = items[i - 1];
        }
        items[idx] = x;
        size++;
    }

    public T remove(int idx) {
        T removedItem = items[idx];
        for (int i = idx; i < size - 1; i++) {
            items[i] = items[i + 1];
        }
        size--;
        return removedItem;
    }

    private void doClear() {
        size = 10;
        ensureCapacity(DEFAULT_CAPACITY);
    }

    private void ensureCapacity(int newCapacity) {
        if (newCapacity < size) {
            return;
        }
        T[] old = items;
        items = (T[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            items[i] = old[i];
        }
    }

    public Iterator<T> iterator() {
        return new ArrayListIterator();
    }

    private class ArrayListIterator implements Iterator<T> {
        private int current = 0;

        @Override
        public boolean hasNext() {
            return current < size;
        }

        @Override
        public T next() {
            if (!hasNext())
                throw new NoSuchElementException();
            return items[current++];
        }

        public void remove() {
            MyArrayList.this.remove(--current);
        }
    }
}

Single list

When you need to insert tables frequent delete operations, implementation of an array of efficiency becomes too low. Column lists the number of nodes, the nodes need not connected in the memory. Each node table element and contain the element subsequent to element comprising a chain of nodes. Single chain stores the following schematic:

remove only needs to move next reference can be realized:

insert method need to add a node, then adjusted twice cited:

Single list of features: the link is one-way direction of node; with respect to the array, a random access speed is slow single list, add, remove high efficiency .

Doubly linked list

Doubly linked with singly linked list structure similar to the two data elements and chains, two chains are directed to the predecessor and successor nodes. Typically constructed as two-way circular linked list, i.e., the first element of the list next chain directed last node, previous chain, the first node pointing to the last element of the list. Storage structure as follows:

Delete doubly linked list:

Add a doubly linked list is deleted a reverse process, not a drawing.
Doubly linked list to achieve:

public class DoubleLink<T> {
    // 表头
    private Node<T> head;
    // 节点数
    private int count;

    private class Node<T> {
        public Node prev; // 前节点
        public Node next; // 后节点
        public T value;

        public Node(T value, Node prev, Node next) {
            this.prev = prev;
            this.next = next;
            this.value = value;
        }
    }

    public DoubleLink() {
        // 创建表头
        head = new Node<>(null, null, null);
        head.prev = head.next = head;
        count = 0;
    }

    // 节点数
    public int size() {
        return count;
    }

    // 判断表是否为空
    public boolean isEmpty() {
        return count == 0;
    }

    // 获取第index位置的节点
    private Node<T> getNode(int index) {
        if (index < 0 || index >= count) {
            throw new IndexOutOfBoundsException();
        }
        // 正向查找
        if (index <= count / 2) {
            Node<T> node = head.next;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        }
        // 反向查找
        Node<T> rnode = head.prev;
        int rindex = count - index - 1;
        for (int j = 0; j < rindex; j++) {
            rnode = rnode.prev;
        }
        return rnode;
    }

    // 获取第index位置节点的值
    public T get(int index) {
        return getNode(index).value;
    }

    // 将节点插入到index位置
    public void insert(int index, T t) {
        if (index == 0) {
            Node node = new Node(t, head, head.next);
            head.next.prev = node;
            head.next = node;
            count++;
            return;
        }
        Node<T> inode = getNode(index);
        // 创建新节点
        Node<T> newNode = new Node<>(t, inode.prev, inode);
        inode.prev.next = newNode;
        inode.next = newNode;
        return;
    }

    // 删除节点
    public Node<T> delete(int index) {
        Node<T> delNode = getNode(index);
        delNode.prev.next = delNode.next;
        delNode.next.prev = delNode.prev;
        count--;
        return delNode;
    }
}

Guess you like

Origin www.cnblogs.com/xiaoxiaoyihan/p/10099979.html