1.数据结构与算法——从零开始学习(一)基础概念篇
2. 大话数据结构 – 整理归纳(1)
3. Java数据结构
文章目录
线性表
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;
}
}
来源:CSDN
作者:Jolin's knight
链接:https://blog.csdn.net/qq_25046005/article/details/104255125