Loading...

LinkedList的几个元素操作方法

看完这几个方法的源码,大概我们知道了,LinkedList不仅实现了队列先进先出 (使用 addremove )也实现了栈的 先进后出 (使用poppush)

  • remove 移除头部元素 Retrieves and removes the head (first element) of this list. @throws NoSuchElementException if this list is empty
public E remove() {
        return removeFirst();
    }
  • poll 移除头部元素 空队列返回null Retrieves and removes the head (first element) of this list.
public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }
  • pop 栈顶取出 也就是从队列最前面取出 Pops an element from the stack represented by this list. In other words, removes and returns the first element of this list.@throws NoSuchElementException if this list is empty
public E pop() {
        return removeFirst();
    }

 public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }
  • add 在队列最末尾添加Inserts the specified element at the specified position in this list.
public boolean add(E e) {
        linkLast(e);
        return true;
    }
  • push 压如栈顶。也就是队列最前面插入 Pushes an element onto the stack represented by this list. In other words, inserts the element at the front of this list.
public void push(E e) {
        addFirst(e);
    }
posted @ 2020-11-07 23:13  倒霉蛋蛋虾  阅读(164)  评论(0编辑  收藏  举报