transient int size;
transient LinkedList.Node<E> first;
transient LinkedList.Node<E> last;
//节点数据结构
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
有头有尾有长度的双向链表
实现Queue接口拥有队列特性(先进先出)
队列特性方法
//在尾部添加元素
boolean add(E e);//队列为满时,抛出异常IllegalStateException
boolean offer(E e);//队列为满时,返回false
//删除头部元素,返回头部元素,并且从队列中删除
E remove();//队列为空时,抛出异常NoSuchElementException
E poll();//队列为空时,返回null
//查看头部元素,返回头部元素,不改变队列
E element();//队列为空时,抛出异常NoSuchElementException
E peek();//队列为空时,返回null
实现Deque(双端队列)接口拥有栈(后进先出)、双端队列特性
栈特性方法
//入栈,在头部添加元素
void push(E e); //栈满时抛出异常IllegalStateException
//出栈,返回头部元素,并且从栈中删除
E pop();//如果为空抛出异常NoSuchElementException
//查看栈头部元素,不修改栈
E peek();//如果栈为空,返回null
LinkedList当做栈用法
Deque<String> stack = new LinkedList<>();
双端队列 (Deque)操作两端方法
void addFirst(E e);
void addLast(E e);
E getFirst();
E getLast();
boolean offerFirst(E e);
boolean offerLast(E e);
E peekFirst();
E peekLast();
E pollFirst();
E pollLast();
E removeFirst();
E removeLast();
迭代器
Iterator<E> descendingIterator();
//向后迭代代码
public E next() {
this.checkForComodification();
if(!this.hasNext()) {
throw new NoSuchElementException();
} else {
this.lastReturned = this.next;
this.next = this.next.next;
++this.nextIndex;
return this.lastReturned.item;
}
}