LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 235|回复: 0

LinkedList实现 - Deque 方法

[复制链接]
发表于 2023-12-20 15:15:50 | 显示全部楼层 |阅读模式

Deque 方法


  1.     /**
  2.      * Inserts the specified element at the front of this list.
  3.      *
  4.      * @param e the element to insert
  5.      * @return {@code true} (as specified by {@link Deque#offerFirst})
  6.      * @since 1.6
  7.      */
  8.     public boolean offerFirst(E e) {
  9.         addFirst(e);
  10.         return true;
  11.     }

  12.     /**
  13.      * Inserts the specified element at the end of this list.
  14.      *
  15.      * @param e the element to insert
  16.      * @return {@code true} (as specified by {@link Deque#offerLast})
  17.      * @since 1.6
  18.      */
  19.     public boolean offerLast(E e) {
  20.         addLast(e);
  21.         return true;
  22.     }

  23.     /**
  24.      * Retrieves, but does not remove, the first element of this list,
  25.      * or returns {@code null} if this list is empty.
  26.      *
  27.      * @return the first element of this list, or {@code null}
  28.      *         if this list is empty
  29.      * @since 1.6
  30.      */
  31.     public E peekFirst() {
  32.         final Node<E> f = first;
  33.         return (f == null) ? null : f.item;
  34.      }

  35.     /**
  36.      * Retrieves, but does not remove, the last element of this list,
  37.      * or returns {@code null} if this list is empty.
  38.      *
  39.      * @return the last element of this list, or {@code null}
  40.      *         if this list is empty
  41.      * @since 1.6
  42.      */
  43.     public E peekLast() {
  44.         final Node<E> l = last;
  45.         return (l == null) ? null : l.item;
  46.     }

  47.     /**
  48.      * Retrieves and removes the first element of this list,
  49.      * or returns {@code null} if this list is empty.
  50.      *
  51.      * @return the first element of this list, or {@code null} if
  52.      *     this list is empty
  53.      * @since 1.6
  54.      */
  55.     public E pollFirst() {
  56.         final Node<E> f = first;
  57.         return (f == null) ? null : unlinkFirst(f);
  58.     }

  59.     /**
  60.      * Retrieves and removes the last element of this list,
  61.      * or returns {@code null} if this list is empty.
  62.      *
  63.      * @return the last element of this list, or {@code null} if
  64.      *     this list is empty
  65.      * @since 1.6
  66.      */
  67.     public E pollLast() {
  68.         final Node<E> l = last;
  69.         return (l == null) ? null : unlinkLast(l);
  70.     }

  71.     /**
  72.      * Pushes an element onto the stack represented by this list.  In other
  73.      * words, inserts the element at the front of this list.
  74.      *
  75.      * <p>This method is equivalent to {@link #addFirst}.
  76.      *
  77.      * @param e the element to push
  78.      * @since 1.6
  79.      */
  80.     public void push(E e) {
  81.         addFirst(e);
  82.     }

  83.     /**
  84.      * Pops an element from the stack represented by this list.  In other
  85.      * words, removes and returns the first element of this list.
  86.      *
  87.      * <p>This method is equivalent to {@link #removeFirst()}.
  88.      *
  89.      * @return the element at the front of this list (which is the top
  90.      *         of the stack represented by this list)
  91.      * @throws NoSuchElementException if this list is empty
  92.      * @since 1.6
  93.      */
  94.     public E pop() {
  95.         return removeFirst();
  96.     }

  97.     /**
  98.      * Removes the first occurrence of the specified element in this
  99.      * list (when traversing the list from head to tail).  If the list
  100.      * does not contain the element, it is unchanged.
  101.      *
  102.      * @param o element to be removed from this list, if present
  103.      * @return {@code true} if the list contained the specified element
  104.      * @since 1.6
  105.      */
  106.     public boolean removeFirstOccurrence(Object o) {
  107.         return remove(o);
  108.     }

  109.     /**
  110.      * Removes the last occurrence of the specified element in this
  111.      * list (when traversing the list from head to tail).  If the list
  112.      * does not contain the element, it is unchanged.
  113.      *
  114.      * @param o element to be removed from this list, if present
  115.      * @return {@code true} if the list contained the specified element
  116.      * @since 1.6
  117.      */
  118.     public boolean removeLastOccurrence(Object o) {
  119.         if (o == null) {
  120.             for (Node<E> x = last; x != null; x = x.prev) {
  121.                 if (x.item == null) {
  122.                     unlink(x);
  123.                     return true;
  124.                 }
  125.             }
  126.         } else {
  127.             for (Node<E> x = last; x != null; x = x.prev) {
  128.                 if (o.equals(x.item)) {
  129.                     unlink(x);
  130.                     return true;
  131.                 }
  132.             }
  133.         }
  134.         return false;
  135.     }
复制代码


------
原文链接:https://pdai.tech/md/java/collection/java-collection-LinkedList.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表