参考LinkedList,手写双向链表

package transferlinked;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * 双向链表
 * 

* Version 1.0.0 * * @author zhouyuyou *

* Date 2020/05/06 17:07 */ public class BothwayLinkedList { /** * 头节点 */ private Node head; /** * 尾节点 */ private Node tail; /** * 长度 */ private transient int size = 0; /** * 关键字transient 标识的字段的生命周期仅存于调用者的内存中而不会写到磁盘里持久化。 * 修改次数 */ private transient int modCount = 0; private static class Node{ E item; Node prev; Node next; public Node(E item, Node prev, Node next) { this.item = item; this.prev = prev; this.next = next; } } /*** * 尾部插入 * @param e */ public boolean add(E e){ addLast(e); return true; } public void add(int index,E e){ //校验参数 //如果是添加到末尾 if(index == size){ addLast(e); }else { // 找到该索引的元素 Node node = node(index); addBefore(e,node); } } public E get(int index){ return node(index).item; } public E remove(int index){ Node node = node(index); return unlink(node); } E unlink(Node node){ E item = node.item; Node prev = node.prev; Node next = node.next; //头节点 if(null == prev){ head = next; }else{ prev.next = next; node.prev = null; } if(null == node.next){ tail = prev; }else { next.prev = prev; node.next = null; } node.item = null; size--; modCount++; return item; } Node node(int index){ //判断是从头节点遍历还是尾节点 if(index < (size >> 1)){ Node x = head; for(int i = 0;i < index;i++ ){ x = x.next; } return x; }else { Node x = tail; for(int i = size - 1;i > index;i--){ x = x.prev; } return x; } } void addBefore(E e,Node node){ Node prev = node.prev; Node newNode = new Node<>(e,prev,node); if(null == prev){ head = newNode; }else { prev.next = newNode; } node.prev = newNode; size++; modCount++; } void addLast(E e){ final Node l = tail; final Node newNode = new Node<>(e,l,null); //将加入的新节点作为尾节点 tail = newNode; if(head == null){ //说明第一次加入 head = newNode; }else { l.next = newNode; } size++; modCount++; } private E unlinkFirst(){ Node f = head; E item = f.item; Node next = f.next; f.item = null; f.next = null; head = next; //说明只有一个节点 if(null == next){ //将尾结点设为null tail = null; }else { next.prev = null; } size--; modCount++; return item; } private E unlinkLast(){ Node f = tail; E item = f.item; Node prev = f.prev; f.item = null; f.prev = null; tail = prev; //说明只有一个节点 if(null == prev){ //将头结点设为null head = null; }else { prev.next = null; } size--; modCount++; return item; } public ListIterator listIterator(int index) { return new LinkedIterator(index); } private class LinkedIterator implements ListIterator{ private Node lastReturned; private Node next; private int nextIndex; private int expectedModCount = modCount; public LinkedIterator(int index) { next = (index == size)? null : node(index); this.nextIndex = index; } @Override public boolean hasNext() { return nextIndex < size; } @Override public E next() { //check 是否有并发修改 //check 是否有下一个元素 lastReturned = next; next = next.next; nextIndex++; return lastReturned.item; } @Override public boolean hasPrevious() { return nextIndex > 0; } @Override public E previous() { //check 是否有并发修改 //check 是否有上一个元素 lastReturned = next = (next == null)? tail : next.prev; nextIndex--; return lastReturned.item; } @Override public int nextIndex() { return nextIndex; } @Override public int previousIndex() { return nextIndex - 1; } @Override public void remove() { //check 并发修改、最后查询的节点不能为空 Node lastNext = lastReturned.next; unlink(lastReturned); if(next == lastReturned){ next = lastNext; }else { nextIndex--; } lastReturned = null; expectedModCount++; } @Override public void set(E e) { } @Override public void add(E e) { } } public static void main(String[] args) { BothwayLinkedList linkedList = new BothwayLinkedList<>(); linkedList.add(11); linkedList.add(22); linkedList.add(1,2222222); linkedList.add(33333); linkedList.add(4444); linkedList.remove(3); System.out.println(linkedList.get(3)); // ListIterator integerListIterator = linkedList.listIterator(0); // while (integerListIterator.hasNext()){ // Integer next = integerListIterator.next(); // if(22==next){ // integerListIterator.remove(); // continue; // } // System.out.println(next); // } // System.out.println(linkedList.size); } }

你可能感兴趣的:(参考LinkedList,手写双向链表)