Complete the remaining API today:
return value | method | meaning |
---|---|---|
boolean | offer(E e) | Add the specified element to the end of this list (the last element) |
boolean | offerFirst(E e) | Insert the specified element at the front of this list |
boolean | offerLast (E e) | Insert the specified element at the end of the list |
E | poll() | Retrieve and delete the head of this list |
E | pollFirst() | Retrieve and delete the first element of this list, if this list is empty, return null |
E | pollLast() | Retrieve and delete the last element of this list, if this list is empty, return null |
E | pop() | Pop an element from the stack represented by this list |
void | push (E and) | Push the element onto the stack represented by this list |
E | remove() | Retrieve and delete the head of this list |
E | remove(int index) | Delete the element at the specified position in the list |
boolean | removeFirstOccurrence(Object o) | Delete the first occurrence of the specified element in this list (when traversing the list from beginning to end) |
boolean | removeLastOccurrence(Object o) | Delete the last occurrence of the specified element in this list (when traversing the list from beginning to end) |
//插入到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;
}