# 实现链表添加删除操作统一

## 概述

@Override
public void add(int index, E element) {
/*
* 最好：O(1)
* 最坏：O(n)
* 平均：O(n)
*/

if (index == 0) {
first = new Node<>(element, first);
} else {
Node<E> prev = node(index - 1);
prev.next = new Node<>(element, prev.next);
}
size++;
}

@Override
public E remove(int index) {
/*
* 最好：O(1)
* 最坏：O(n)
* 平均：O(n)
*/
rangeCheck(index);

Node<E> node = first;
if (index == 0) {
first = first.next;
} else {
Node<E> prev = node(index - 1);
node = prev.next;
prev.next = node.next;
}
size--;
return node.element;
}


## 解决方案

### 代码实现

package com.study.singlelinkDemo;

/**
* Created by Zsy on 2020/8/3.
*/
public class SingleLinkedList2<E> extends AbstractList<E> {

private Node<E> first;

private static class Node<E> {
private E element;
Node<E> next;

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

this.first = new Node<E>(null, null);
}

@Override
public void clear() {
this.first = null;
size = 0;
}

@Override
public E get(int index) {

return findNode(index).element;
}

public Node<E> findNode(int index) {
rangeCheck(index);
Node<E> node = first;
for (int i = 0; i < index; i++) {
node = node.next;
}
return node;
}

@Override
public E set(int index, E element) {
rangeCheck(index);
Node<E> node = findNode(index);
E oldNode = node.element;
node.element = element;

return oldNode;
}

@Override
public void add(int index, E element) {

Node<E> preNode =index==0?first: findNode(index - 1);// 对index=0再执行减法导致的outboundException的处理

preNode.next = new Node<E>(element, preNode.next);

size++;

}

@Override
public E remove(int index) {
rangeCheck(index);
Node<E> node = findNode(index);

Node<E> preNode =index==0?first: findNode(index - 1);// 对index=0再执行减法导致的outboundException的处理
preNode.next=node.next;
//        if (index == 0) {
//            first = first.next;
//        } else {
//            Node<E> preNode = findNode(index - 1);
//            preNode.next = node.next;
//        }
size--;
return node.element;
}

@Override
public int indexOf(E element) {

Node<E> node = first;
if (element == null) {
for (int i = 0; i < size; i++) {
if (node == null) return i;
node = node.next;
}
} else {
for (int i = 0; i < size; i++) {
if (element.equals(node.element))//防止空指针异常
return i;
node = node.next;
}
}

return ELEMENT_NOT_FOUND;
}

@Override
public String toString() {
Node<E> node = this.first;
String statrStr = String.format("size: %d [", size);
StringBuilder result = new StringBuilder();
result.append(statrStr);
for (int i = 0; i < size; i++) {
if (i != 0) result.append(",");
result.append(node.element);
node = node.next;

}
result.append("]");
return result.toString();
}
}



# 小结

## 复杂度分析注意事项

1. 关于复杂度的分析，我们需要从：最好、最坏、平均复杂度三个角度进行分析
2. 关于复杂度的n并不是循环的参数n，而是该算法的操作数据规模

## 动态数组和单向链表的复杂度分析

### public E get(int index)// 获取对应位置的元素

#### 动态数组的实现代码


public E get(int index){
return elements[index];
}


#### 单向链表的实现

 @Override
public E get(int index) {

return findNode(index).element;
}


### public E set(int index,E element) // 修改对应位置的元素

#### 动态数组的实现代码

 public E set(int index,E element){
rangeCheck(index);
E oldElement=elements[index];
elements[index]=element;
return oldElement;
}


#### 单向链表的实现

 @Override
public E set(int index, E element) {
rangeCheck(index);
Node<E> node = findNode(index);
E oldNode = node.element;
node.element = element;

return oldNode;
}


### public void add(int index, E element)// 在指定索引位置插入元素

#### 动态数组的实现代码

public void add(int index, E element) {
ensureCapacity(size + 1);
for (int i = size - 1; i >= index; i--) {
elements[i + 1] = elements[i];
}
elements[index] = element;
size++;

}


#### 单向链表的实现

 @Override
public void add(int index, E element) {

if (index == 0) {
first = new Node<E>(element, first);
} else {
Node<E> preNode = findNode(index - 1);
preNode.next = new Node<E>(element, preNode.next);

}
size++;

}


### public E remove(int index)// 移除对应位置的元素

#### 动态数组的实现代码

  public E remove(int index){
rangeCheck(index);
E oldElement=elements[index];
for (int i = index; i <size-1 ; i++) {
elements[i]=elements[i+1];
}
elements[--size]=null;
return oldElement;
}


#### 单向链表的实现

 @Override
public E remove(int index) {
rangeCheck(index);
Node<E> node = findNode(index);
if (index == 0) {
first = first.next;
} else {
Node<E> preNode = findNode(index - 1);
preNode.next = node.next;
}
size--;
return node.element;
}


### public void add(E element)// 将元素添加到线性表末尾

#### 动态数组的实现代码

   *  数学计算：扩容前设计操作次数就是1次，而扩容时的操作次数是n，可得计算机式子：1+1+1...+n=2n/n=>2=>O(1)
* 均摊复杂度：这种理解适合那些连续多次复杂度较低的操作后，出现一次个别复杂度高的情况。使用均摊复杂度分析下方代码的方式也很简单，将扩容操作的复杂度平均给之前的复杂度低的清空，如下图所示：


    public void add(E element) {
ensureCapacity(size + 1);

}


#### 单向链表的实现

public void add(E element) {
}


# 补充动态数组的缩容

## 代码范例

private void trim() {

int oldCapacity = elements.length;

int newCapacity = oldCapacity >> 1;
if (size > (newCapacity) || oldCapacity <= DEFAULT_CAPACITY) return;

E[] newElements = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
newElements[i] = elements[i];
}
elements = newElements;

System.out.println(oldCapacity + "缩容为" + newCapacity);
}


01-29

09-28
06-15 400
11-14 52
09-12 713
07-08 2597
03-22 57