2019.10.19双向链表


import java.util.NoSuchElementException;

public class LinkedListT<E> {
private Node<E> first;

private Node<E> last;

long size = 0l;

private void linkLast(E e) {
Node<E> temp = last;
Node<E> newNode = new Node<E>(temp, e, null);
last = newNode;
if (temp == null) {
first = newNode;
} else {
temp.next = newNode;
}
size++;
}

public boolean add(E e) {
linkLast(e);
return true;
}

public boolean add(long index, E e) {
checkInsertIndex(index);
if (index > 0 && index < size) {
//从前到后的查法
/*long j = 0;
for (Node<E> i = first; i != null; i = i.next, j++) {
if (j == index) {
Node<E> newNode = new Node<>(i.prev, e, i);
i.prev.next = newNode;
i.prev = newNode;
}
}*/
//分前后查找法
Node<E> node = indexNode(isSize(index), index);
Node<E> newNode = new Node<>(node.prev, e, node);
node.prev.next = newNode;
node.prev = newNode;
size++;
} else if (index == 0) {
addFirst(e);
}
if (index == size) {
linkLast(e);
}
return true;
}

public void checkInsertIndex(long index) {
if (index > size || index < 0) {
throw new IndexOutOfBoundsException();
}
}

public Boolean addFirst(E e) {
if (size == 0) {
linkLast(e);
} else {
Node<E> newNode = new Node<>(null, e, first);
first.prev = newNode;
first = newNode;
size++;
}
return true;
}

public Boolean addLast(E e) {
linkLast(e);
return true;
}

public E getFirst() {
if (first == null) {
throw new NoSuchElementException();
}
return first.item;
}

public E getLast() {
if (last == null) {
throw new NoSuchElementException();
}
return last.item;
}

public long getSize() {
return size;
}

public E get(long index) {
E result = null;
checkInsertIndex(index);
long j = 0;
for (Node<E> i = first; i != null; i = i.next, j++) {
if (j == index) {
result = i.item;
}
}
return result;
}

public void removeAll() {
first = null;
last = null;
size = 0;
}

public E remove(long index) {
E e = null;
checkRemoveIndex(index);
if (index != 0 && index != size - 1){
Node<E> node = indexNode(isSize(index), index);
// Node<E> prev = node.prev;
node.prev.next = node.next;
node.next.prev = node.prev;
e = node.item;
node = null;
size--;
}else if(index == 0){
e = removeFirst();
}else {
e = removeLast();
}
return e;
}

private void checkRemoveIndex(long index){
if (index >= size || index < 0) {
throw new IndexOutOfBoundsException();
}
}

private Node<E> indexNode(boolean flag, long index) {
long j = 0;
Node<E> node = null;
if (flag) {
for (Node<E> i = first; i != null; i = i.next, j++) {
if (index == j) {
node = i;
}
}
} else {
j = size - 1;
for (Node<E> i = last; i != null; i = i.prev, j--) {
if (index == j){
node = i;
}
}
}
return node;
}

private boolean isSize(long index) {
if (size / 2 >= index) {
return true;
} else {
return false;
}
}

public E remove(Object obj) {
E e = null;
if (obj == null){
for ( Node<E> i = first;i != null;i = i.next){
if (i.item == obj){
e = unlink(i);
}
}
}else {
for ( Node<E> i = first;i != null;i = i.next){
if (i.item.equals(obj)){
e = unlink(i);
}
}
}
return e;
}

E unlink(Node<E> x) {
E element = x.item;
Node<E> next = x.next;
Node<E> prev = x.prev;

if (prev == null) {
first = next;
} else {
prev.next = next;
x.prev = null;
}

if (next == null) {
last = prev;
} else {
next.prev = prev;
x.next = null;
}

x.item = null;
size--;
return element;
}

public E removeFirst() {
if (size == 0) {
throw new IndexOutOfBoundsException();
}
Node<E> second = first.next;
E e = first.item;
first.next = null;
if (second == null) {
last = null;
} else {
second.prev = null;
}
first = second;
size--;
return e;
}

public E removeLast() {
if (size == 0) {
throw new IndexOutOfBoundsException();
}
Node<E> secondLast = last.prev;
E e = last.item;
last.prev = null;
if (secondLast == null) {
first = null;
} else {
secondLast.next = null;
}
last = secondLast;
size--;
return e;
}

private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;

Node(Node<E> prev, E item, Node<E> next) {
this.item = item;
this.next = next;
this.prev = prev;
}
}

@Override
public String toString() {
StringBuffer str = new StringBuffer("");
for (Node<E> i = first; i != null; i = i.next) {
str.append(i.item);
if (i.next != null) {
str.append(",");
}
}
return "[" + str.toString() + "]";
}
}
点赞