LinkedList 是基于链表实现的(内部类)
int size = 0; // 记录链表的长度
Node first; // 第一个子结点
Node last; // 最后一个子结点
class Node {
String str; // 节点内数据
Node prev; // 上一个节点的位置
Node next; // 下一个节点的位置
public Node(String str, Node prev, Node next) {
this.str = str;
this.prev = prev;
this.next = next;
}
}
ArrayList: 底层动态数组,通过索引查询速度快,但是操作数据满
LinkedList: 双向连表, 查询慢,操作块,而且内存利用率高
手写实现LinkedList
package com.ld.linkedList;
public class LinkedList {
int size = 0; // 记录链表的长度
Node first; // 第一个子结点
Node last; // 最后一个子结点
class Node {
String str; // 节点内数据
Node prev; // 上一个节点的位置
Node next; // 下一个节点的位置
public Node(String str, Node prev, Node next) {
this.str = str;
this.prev = prev;
this.next = next;
}
}
/**
* 添加
* @param str
*/
public void add (String str) {
// 首先判断是否为第一次添加 size = 0
Node node = new Node(str,null,null);
if(size == 0) {
// 第一次添加 first 为node的地址 last也为node的地址
this.first = node;
// this.last = node;
// size++;
}else {
// 第n次添加 first不变, last变为新的 并且需要拿到last的node将他的next改变
this.last.next = node;
// this.last = node; // 改变last的节点为新加的节点的地址
node.prev = this.last; // 上一个节点
// size++;
}
this.last = node;
size++;
}
public void add(int index, String str) {
// 判断下标是否越界
if(index<0 || index>size) {
throw new IllegalArgumentException("index错误");
}
// 拿到index位置的node 将他的next 变为新的node 拿到next下一个node 修改他的prev为新的node
// 新建的node 的prev 为index的node的位置 next为index+1的位置
Node node = new Node(str,null,null);
// 根据index的大小判断位置 首位,末尾 ,中间
if(index == size) { //尾部添加
// node.prev = this.last; // 先将原last的位置赋值给node的prev
// this.last.next = node; // 在将last的位置改变为node
// size++;
add(str);
return;
}else if(index == 0) { //首部添加
node.next = this.first; // 将原先的first的位置赋值给node的next
this.first.prev = node; // 在将first的位置改变为node
this.first = node;
} else { //中间添加 获取到index的节点 拿到index的prev,next prev修改为node的地址
// 获取index的node
Node no = getNode(index);
//创建一个新的node 用于存放 index位置的数据 index的str ,prev为index节点的位置,ne
// Node oldNode = new Node(no.str,no,no.next);
// no.str = str;
// no.next = oldNode;
no.prev.next = node;
node.prev = no.prev;
no.prev = node;
node.next = no;
}
size++;
}
public void out(int index) {
if(index<0||index>=size) {
throw new IllegalArgumentException("下标out");
}
}
public void remove(int index) {
out(index);
if(index == 0) { // 头部进行删除
this.first.next.prev = null;
this.first = this.first.next;
}else if(index ==size-1){ //尾部删除
this.last.prev.next = null;
this.last = this.last.prev;
} else {
Node no = getNode(index);
no.next.prev = no.prev;
no.prev.next = no.next;
}
size--;
}
/**
* 更具制定元素删除
* @param str
*/
public void remove(String str) {
int index = indexOf(str);
if(index!=-1) {
remove(index);
}
}
public int indexOf(String str) {
Node no = this.first;
for(int i=0;i<size;i++) {
if(str == no.str|| str!=null && no.str.equals(str)) {
return i;
}
no = no.next;
}
return -1;
}
public Node getNode(int index) {
Node no = this.first;
for(int i=0;i<index;i++) {
no = no.next;
}
return no;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
Node no = this.first;
for(int i=0;i<size;i++) {
sb.append(no.str).append(", ");
no = no.next;
}
String str = sb.toString();
if(size>0) {
str = str.substring(0,str.length()-2);
}
return str;
}
}