动态数组、栈、队列 底层依托于静态数组,靠 resize 解决固定容量问题
链表是真正的动态数据结构、最简单的动态数据结构
优点:不需要处理固定容量的问题
缺点:丧失了随机访问的能力(不能直接通过索引获取数据)

  1. class Node {
  2. E e;
  3. Node next;
  4. }

实现链表

  1. public class LinkedList<E> {
  2. private int size;
  3. private final Node dummyHead;
  4. class Node {
  5. public E e;
  6. public Node next;
  7. Node(E e, Node next) {
  8. this.e = e;
  9. this.next = next;
  10. }
  11. Node(E e) {this(e, null); }
  12. Node() { this(null, null); }
  13. @Override
  14. public String toString() {
  15. return e.toString();
  16. }
  17. }
  18. public LinkedList() {
  19. this.dummyHead = new Node(null, null);
  20. size = 0;
  21. }
  22. // 获取链表中的元素个数
  23. public int getSize() { return size; }
  24. // 返回链表是否为空
  25. public boolean isEmpty() { return size == 0; }
  26. // O(n/2) = O(n)
  27. public void add(int index, E e) {
  28. if (index < 0 || index > size) {
  29. throw new IllegalArgumentException("add failed. Illegal index.");
  30. }
  31. Node prev = dummyHead;
  32. for (int i = 0; i < index; i++) {
  33. prev = prev.next;
  34. }
  35. prev.next = new Node(e, prev.next);
  36. size ++;
  37. }
  38. // O(1) 头部添加
  39. public void addFirst(E e) {
  40. add(0, e);
  41. }
  42. // O(n) 尾部添加
  43. public void addLast(E e) {
  44. add(size, e);
  45. }
  46. // O(n) 获得链表的第 index(0-based)个位置的元素
  47. // 在链表中不是一个常用操作
  48. public E get(int index) {
  49. if (index < 0 || index >= size) {
  50. throw new IllegalArgumentException("Get failed, Illegal index.");
  51. }
  52. Node cur = dummyHead.next;
  53. for(int i = 0; i < index; i++) {
  54. cur = cur.next;
  55. }
  56. return cur.e;
  57. }
  58. // O(n) 获得链表的最后一个元素
  59. public E getLast() {
  60. return get(size - 1);
  61. }
  62. // O(n) 修改链表的第 index(0-based)个位置的元素为 e
  63. public void set(int index, E e) {
  64. if(index < 0 || index >= size) {
  65. throw new IllegalArgumentException("Set failed. Illegal index.");
  66. }
  67. Node cur = dummyHead.next;
  68. for(int i = 0; i < index; i++) {
  69. cur = cur.next;
  70. }
  71. cur.e = e;
  72. }
  73. // 查找元素中是否有元素 e
  74. public boolean contains(E e) {
  75. Node cur = dummyHead.next;
  76. while (cur != null) {
  77. if (cur.e.equals(e)) {
  78. return true;
  79. }
  80. cur = cur.next;
  81. }
  82. return false;
  83. }
  84. // 删除 index(0-based)位置的元素,返回删除元素
  85. public E remove(int index) {
  86. if (index < 0 || index >= size) {
  87. throw new IllegalArgumentException("Remove failed, Index is Illegal.");
  88. }
  89. Node prev = dummyHead;
  90. for(int i = 0; i < index; i ++) {
  91. prev = prev.next;
  92. }
  93. Node retNode = prev.next;
  94. prev.next = retNode.next;
  95. retNode.next = null;
  96. size --;
  97. return retNode.e;
  98. }
  99. // O(1) 删除第一个元素,返回删除的元素
  100. public E removeFirst() {
  101. return remove(0);
  102. }
  103. // O(n) 删除最后一个元素,返回删除的元素
  104. public E removeLast() {
  105. return remove(size - 1);
  106. }
  107. @Override
  108. public String toString() {
  109. StringBuilder res = new StringBuilder();
  110. Node cur = dummyHead.next;
  111. while (cur != null) {
  112. res.append(cur + "->");
  113. cur = cur.next;
  114. }
  115. res.append("null");
  116. return res.toString();
  117. }
  118. }

基于链表的栈

public class LinkedListStack<E> implements Stack<E> {

    private LinkedList<E> list;

    public LinkedListStack() {
        list = new LinkedList<>();
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public void push(E e) {
        list.addFirst(e);
    }

    @Override
    public E pop() {
        return list.removeFirst();
    }

    @Override
    public E peek() {
        return list.getFirst();
    }

    @Override
    public String toString() {
        return "Stack: top " + list;
    }
}

基于链表的队列

public class LinkedListQueue<E> implements Queue<E> {

    private class Node {
        public E e;
        public Node next;

        public Node (E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }
        public Node() {
            this(null, null);
        }

        @Override
        public String toString() { return e.toString(); }
    }

    private Node head, tail;
    private int size;

    public LinkedListQueue() {
        head = null;
        tail = null;
        size = 0;
    }


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

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

    @Override
    public void enqueue(E e) {
        if (tail == null) {
            tail = new Node(e);
            head = tail;
        } else  {
            tail.next = new Node(e);
            tail = tail.next;
        }
        size ++;
    }

    @Override
    public E dequeue() {
        if (isEmpty()) {
            throw new IllegalArgumentException("Cannot dequeue from an empty queue");
        }
        Node retNode = head;
        head = head.next;
        retNode.next = null;
        if (head == null) {
            tail = null;
        }
        size --;
        return retNode.e;
    }

    @Override
    public E getFront() {
        if (isEmpty()) {
            throw new IllegalArgumentException("Cannot dequeue from an empty queue");
        }
        return head.e;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Queue: front ");
        Node cur = head;
        while (cur != null) {
            res.append(cur + "->");
            cur = cur.next;
        }
        res.append("NULL tail");
        return res.toString();
    }
}