- 当前链表的大小
 */
 transient int size;
/**
- 存入数据
 */
 public boolean add(E e) {
 linkLast(e);
 return true;
 }
/**
- 返回当前链表的大小
- @return
 */
 public int getSize() {
 return size;
 }
/**
- 将当前数据存入到链表的头部
- @param e
 */
 public void addFirst(E e) {
 Node h = head;
 //创建新节点
 Node newNode = new Node<>(null, e, head);
 head = newNode;
if (head == null) {
 tail = newNode;
 } else {
 h.pre = newNode;
 }
 size++;
 }
/**
- 根据索引搜索到的节点数据
- @param index
 */
 public E get(int index) {
 if (isPositionIndex(index)) {
 return searchNode(index).data;
 }
 return null;
 }
/**
- 根据索引添加数据
- @param index
- @param e
 */
 public void add(int index, E e) {
 addIndex(index, e);
 }
/**
- 删除第一个节点
 */
 public E remove() {
 return removeFirst();
 }
/**
- 删除头节点
- @return
 */
 private E removeFirst() {
 Node h = head;
 if (h == null)
 throw new NoSuchElementException();
 return unlinkFirst(h);
 }
private E unlinkFirst(Node h) {
 //删除的 数据
 E deleData = h.data;
 //找到要删除的后驱
 Node next = h.next;
//清理节点
 h.data = null;
 h.next = null;
//将当前要删除的后驱置为链表头
 head = next;
if (next == null) {
 tail = null;
 } else {
 next.pre = null;
 }
 h = null;
size–;
 return deleData;
 }
private void addIndex(int index, E e) {
 if (isPositionIndex(index)) {
 //找到当前需要插入的索引位置
 if (index == size) {
 linkLast(e);
 } else {
 add(e, searchNode(index));
 }
 }
 }
/**
- 添加新节点到 searchNode 前驱
- @param e
- @param searchNode
 */
 private void add(E e, Node searchNode) {
 //找到 searchNode 前驱节点
 Node snPre = searchNode.pre;
 //创建新节点
 Node newNode = new Node<>(snPre, e, searchNode);
 searchNode.pre = newNode;
 //这里判断 snPre 是否为空 入股为空说明 head 没有数据,如果有数据 就直接把 snPre . next() = newNode
 if (snPre == null) {
 head = newNode;
 } else {
 snPre.next = newNode;
 }
 size++;
 }
private Node searchNode(int index) {
 //优化寻找节点 如果 index > size / 2 就从尾部开始查询,反之从 head 开始遍历查询
 if (index > (size >> 1)) {
 Node t = tail;
 for (int i = size - 1; i > index; i–) {
 t = t.pre;
 }
 return t;
 } else {
 Node h = head;
 for (int i = 0; i < index; i++) {
 h = h.next;
 }
 return h;
 }
 }
/**
- 将数据存入到当前链表尾部
- @param e
 */
 private void linkLast(E e) {
 //拿到尾部的节点数据
 Node t = tail;
 //创建新的节点数据,因为是存在当前节点的尾部,
 // 那么直接默认将当前添加进来的 E 的前驱设置为
 // 当前链表中的尾部数据,现在已经形成单链表了
 // 下一步直接形成双向链表
 Node newNode = new Node<>(t, e, null);
 //现在是双向链表,要把新的节点指向当前的尾部节点,尾部节点指向新的节点
 tail = newNode;
//如果尾部节点为空 那么说明 head 也是空数据 那就吧新节点数据赋值给 head
 if (t == null) {
 head = newNode;
 } else {
 t.next = newNode;
 }
 size++;
 }
/**
- 创建一个空的构造者
 */
 public CustomLinkedList() {
 }
private boolean isPositionIndex(int index) {
 return index >= 0 && index <= size;
 }
/**
- 定义一个内部节点
-  
- 双向链表需要前驱,后驱,数据
 /
 public static class Node {
 /*
- 当前节点的前驱
 /
 private Node pre;
 /*
- 当前节点的数据
 /
 private E data;
 /*
- 当前节点的后驱
 */
 private Node next;
public Node(Node pre, E data, Node last) {
 this.pre = pre;
 this.data = data;
 this.next = last;
 }
 }
 }
- 测试代码
@Test
 public void testCustomLinkedList() {
 CustomLinkedList linkedList = new CustomLinkedList();
 linkedList.add(22);
 linkedList.add(2);
 linkedList.add(77);
 linkedList.add(6);
 linkedList.add(43);
 linkedList.add(76);
 linkedList.add(89);
linkedList.add(0,0);
for (int i = 0; i < linkedList.size; i++) {
 int integer = linkedList.get(i);
 System.out.println("–CustomLinkedList–CustomLinkedList" +integer+"");
 }
 System.out.println("\n\n");
 Integer remove = linkedList.remove();
 System.out.println("–CustomLinkedList–CustomLinkedList" +remove);
 Integer remove1 = linkedList.remove();
 System.out.println("–CustomLinkedList–CustomLinkedList" +remove1+"");
 Integer remove2 = linkedList.remove();
 System.out.println("–CustomLinkedList–CustomLinkedList" + remove2 + “”);
System.out.println("\n\n");
 for (int i = 0; i < linkedList.size; i++) {
 int integer = linkedList.get(i);
 System.out.println("–CustomLinkedList–CustomLinkedList" +integer+"");
 }
 }
- 测试结果

编写简单的 ArrayList CURD
public class CustomArrayList {
/**
- 默认的空元素对象
 */
 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
- 空元素数据
 */
 private static final Object[] EMPTY_ELEMENTDATA = {};
/**
- 默认的元素对象
 */
 private Object[] elementData = null;
/**
- 容量大小
 */
 private int size = 0;
/**
- 默认的容量大小
 */
 private static final int DEFAULT_CAPACITY = 10;
/**
- 最大的数量
- TODO------------减 8 是什么意思没有搞懂
 */
 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/**
- 赋值为一个空对象
 */
 public CustomArrayList(){
 elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
 }
/**
- 外部指定初始化一个容量大小
- @param initCapacity
 */
 public CustomArrayList(int initCapacity){
 if (initCapacity > 0){
 elementData = new Object[initCapacity];
 }else if (initCapacity == 0){
 elementData = EMPTY_ELEMENTDATA;
 }else {
 throw new IllegalArgumentException("Illegal Capacity: "+
 initCapacity);
 }
 }
/**
- 添加数据
 */
 public boolean add(E e){
 //判断是否需要开辟容量空间
 checkIsNeedCapacity(size + 1);
 //添加添加数据
 elementData[size++] = e;
return true;
 }
private void checkIsNeedCapacity(int minCapacity) {
 if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA){
 minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
 }
ensureExplicitCapacity(minCapacity);
 }
private void ensureExplicitCapacity(int minCapacity) {
// overflow-conscious code
 if (minCapacity - elementData.length > 0)
 grow(minCapacity);
 }
/**
- 开辟空间的核心代码
- @param minCapacity
 */
 private void grow(int minCapacity) {
 // overflow-conscious code
 int oldCapacity = elementData.length;
 int newCapacity = oldCapacity + (oldCapacity >> 1);
 if (newCapacity - minCapacity < 0)
 newCapacity = minCapacity;
 if (newCapacity - MAX_ARRAY_SIZE > 0)
 newCapacity = hugeCapacity(minCapacity);
 // minCapacity is usually close to size, so this is a win:
 elementData = Arrays.copyOf(elementData, newCapacity);
 }
private static int hugeCapacity(int minCapacity) {
 if (minCapacity < 0) // overflow
 throw new OutOf
 MemoryError();
 return (minCapacity > MAX_ARRAY_SIZE) ?
 Integer.MAX_VALUE :
 MAX_ARRAY_SIZE;
 }
 }
编写单向链表结构的 CURD

/**
- Created by yangk on 2019/2/19.
 */
public class SingleLinked {
/**
- 头节点
 /
 Node head;
 /*
- 链表长度
 */
 int size = 0;
/**
- 将数据添加到链表中
- @param data
 */
 图片转存中…(img-2GjremKp-1643622153004)]
 MemoryError();
 return (minCapacity > MAX_ARRAY_SIZE) ?
 Integer.MAX_VALUE :
 MAX_ARRAY_SIZE;
 }
 }
编写单向链表结构的 CURD
[外链图片转存中…(img-BoHjjyls-1643622153004)]
/**
- Created by yangk on 2019/2/19.
 */
public class SingleLinked {
/**
- 头节点
 /
 Node head;
 /*
- 链表长度
 */
 int size = 0;
/**
- 将数据添加到链表中
- @param data
 */










