今天完成剩余的API:
返回值 | 方法 | 含义 |
---|---|---|
boolean | offer(E e) | 将指定的元素添加为此列表的尾部(最后一个元素) |
boolean | offerFirst(E e) | 在此列表的前面插入指定的元素 |
boolean | offerLast(E e) | 在该列表的末尾插入指定的元素 |
E | poll() | 检索并删除此列表的头 |
E | pollFirst() | 检索并删除此列表的第一个元素,如果此列表为空,则返回 null |
E | pollLast() | 检索并删除此列表的最后一个元素,如果此列表为空,则返回 null |
E | pop() | 从此列表表示的堆栈中弹出一个元素 |
void | push(E e) | 将元素推送到由此列表表示的堆栈上 |
E | remove() | 检索并删除此列表的头 |
E | remove(int index) | 删除该列表中指定位置的元素 |
boolean | removeFirstOccurrence(Object o) | 删除此列表中指定元素的第一个出现(从头到尾遍历列表时) |
boolean | removeLastOccurrence(Object o) | 删除此列表中指定元素的最后一次出现(从头到尾遍历列表时) |
//插入到LinkedList的尾部。内部还是调用的add方法
public boolean offer(E e) {
return add(e);
}
//头部插入
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
//尾部插入
public boolean offerLast(E e) {
addLast(e);
return true;
}
//删除第一个元素。并且返回。pop调用的就是removeFirst方法
public E pop() {
return removeFirst();
}
//删除头元素,并且返回
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
//删除头元素,并且返回,和poll()方法一致
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
//删除最后一个元素
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}
//将元素压入头部
public void push(E e) {
addFirst(e);
}
//删除第一个元素
public E remove() {
return removeFirst();
}
//删除指定位置的元素
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
//删除这个元素第一次出现的节点。从前往后遍历
public boolean removeFirstOccurrence(Object o) {
return remove(o);
}
//删除元素最后一次出现的节点,从后往前遍历
public boolean removeLastOccurrence(Object o) {
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}