【笔记】数据结构和算法

回眸只為那壹抹淺笑 提交于 2020-02-10 23:09:13

1.数据结构与算法——从零开始学习(一)基础概念篇
2. 大话数据结构 – 整理归纳(1)
3. Java数据结构


  1. 算法复杂度速查表
  2. 十大经典排序算法
  3. 程序员必须掌握的核心算法有哪些?

文章目录

线性表

public interface List<T> {

    public void add(T t);

    public void insert(T t, int index);

    public T remove(int index);

    public boolean isEmpty();

    public T get(int index);

    public void validate(int index);

    public int size();

}
public class ArrayList<T> implements List<T> {

    public Object[] objects = {};

    private int size;

    public ArrayList() {
    }

    public ArrayList(int len) {
        objects = new Object[len];
    }

    @Override
    public void add(T t) {
        size++;
        objects = Arrays.copyOf(objects, size);
        objects[size - 1] = t;
    }

    public void insert(T t, int index) {
        if (index < 0 || index > size) {
            throw new RuntimeException();
        }

        size++;
        objects = Arrays.copyOf(objects, size + 1);
        System.arraycopy(objects, index, objects, index + 1,
                size - index - 1);
        objects[index] = t;
    }

    public T remove(int index) {
        validate(index);
        T t = (T) objects[index];
        System.arraycopy(objects, index + 1, objects, index,
                size - index - 1);
        size--;
        objects = Arrays.copyOf(objects, size);
        return t;
    }

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

    public T get(int index) {
        validate(index);
        return (T) objects[index];

    }

    public void validate(int index) {
        if (index < 0 || index > size - 1) {
            throw new RuntimeException();
        }
    }

    public int size() {
        return size;
    }

}
public class LinkedList<T> implements List<T> {

    transient Node<T> first;

    transient Node<T> last;

    private int size;

    @Override
    public void add(T t) {
        final Node<T> l = last;
        final Node<T> temp = new Node<>(l, t, null);
        last = temp;
        if (l == null) {
            first = temp;
        } else {
            l.next = temp;
        }
        size++;
    }

    @Override
    public void insert(T t, int index) {
        // 获取index的节点
        Node<T> temp = getNode(index);
        // 创建新节点
        Node nodeNew = new Node(temp.prev, t, temp);
        temp.prev.next = nodeNew;
        temp.prev = nodeNew;

        size++;
    }

    @Override
    public T remove(int index) {
        // 获取index的节点
        Node<T> temp = getNode(index);
        temp.prev.next = temp.next;
        temp.next.prev = temp.prev;
        size--;
        return temp.item;
    }

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

    @Override
    public T get(int index) {
        Node<T> temp = getNode(index);
        return temp.item;
    }

    private Node<T> getNode(int index) {
        Node<T> temp;
        validate(index);
        if (index > size / 2) {
            temp = last;
            for (int i = size - 1; i > index; i--) {
                temp = temp.prev;
            }

        } else {
            temp = first;
            for (int i = 1; i <= index; i++) {
                temp = temp.next;
            }
        }
        return temp;
    }

    @Override
    public void validate(int index) {
        if (index < 0 || index > size - 1) {
            throw new RuntimeException();
        }
    }

    @Override
    public int size() {
        return size;
    }

    private static class Node<E> {

        E item;

        Node<E> next;

        Node<E> prev;

        Node(E element) {
            this.item = element;
        }

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.prev = prev;
            this.next = next;
        }

    }

    public T GetFirst() {
        final Node<T> f = first;
        if (f == null) {
            throw new RuntimeException();
        }
        return f.item;
    }

    public T GetLast() {
        final Node<T> f = last;
        if (f == null) {
            throw new RuntimeException();
        }
        return f.item;
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!