动态数组、栈、队列 底层依托于静态数组,靠 resize 解决固定容量问题
链表是真正的动态数据结构、最简单的动态数据结构
优点:不需要处理固定容量的问题
缺点:丧失了随机访问的能力(不能直接通过索引获取数据)
class Node {E e;Node next;}
实现链表
public class LinkedList<E> {private int size;private final Node dummyHead;class Node {public E e;public Node next;Node(E e, Node next) {this.e = e;this.next = next;}Node(E e) {this(e, null); }Node() { this(null, null); }@Overridepublic String toString() {return e.toString();}}public LinkedList() {this.dummyHead = new Node(null, null);size = 0;}// 获取链表中的元素个数public int getSize() { return size; }// 返回链表是否为空public boolean isEmpty() { return size == 0; }// O(n/2) = O(n)public void add(int index, E e) {if (index < 0 || index > size) {throw new IllegalArgumentException("add failed. Illegal index.");}Node prev = dummyHead;for (int i = 0; i < index; i++) {prev = prev.next;}prev.next = new Node(e, prev.next);size ++;}// O(1) 头部添加public void addFirst(E e) {add(0, e);}// O(n) 尾部添加public void addLast(E e) {add(size, e);}// O(n) 获得链表的第 index(0-based)个位置的元素// 在链表中不是一个常用操作public E get(int index) {if (index < 0 || index >= size) {throw new IllegalArgumentException("Get failed, Illegal index.");}Node cur = dummyHead.next;for(int i = 0; i < index; i++) {cur = cur.next;}return cur.e;}// O(n) 获得链表的最后一个元素public E getLast() {return get(size - 1);}// O(n) 修改链表的第 index(0-based)个位置的元素为 epublic void set(int index, E e) {if(index < 0 || index >= size) {throw new IllegalArgumentException("Set failed. Illegal index.");}Node cur = dummyHead.next;for(int i = 0; i < index; i++) {cur = cur.next;}cur.e = e;}// 查找元素中是否有元素 epublic boolean contains(E e) {Node cur = dummyHead.next;while (cur != null) {if (cur.e.equals(e)) {return true;}cur = cur.next;}return false;}// 删除 index(0-based)位置的元素,返回删除元素public E remove(int index) {if (index < 0 || index >= size) {throw new IllegalArgumentException("Remove failed, Index is Illegal.");}Node prev = dummyHead;for(int i = 0; i < index; i ++) {prev = prev.next;}Node retNode = prev.next;prev.next = retNode.next;retNode.next = null;size --;return retNode.e;}// O(1) 删除第一个元素,返回删除的元素public E removeFirst() {return remove(0);}// O(n) 删除最后一个元素,返回删除的元素public E removeLast() {return remove(size - 1);}@Overridepublic String toString() {StringBuilder res = new StringBuilder();Node cur = dummyHead.next;while (cur != null) {res.append(cur + "->");cur = cur.next;}res.append("null");return res.toString();}}
基于链表的栈
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();
}
}
