nodepositionlist-first.html
来自「经典的数据结构源代码(java 实现)」· HTML 代码 · 共 44 行
HTML
44 行
<html><head><title>Code Fragment</title></head><body text=#000000><center></center><br><br><dl><dd><pre> <font color = #ff0080>/** Returns the number of elements in the list; O(1) time */</font> <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>int</font> <font color=#0000ff>size</font>() { <font color=#8000a0><font color=#ff8000>return</font> </font>numElts; } <font color = #ff0080>/** Returns whether the list is empty; O(1) time */</font> <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>boolean</font> <font color=#0000ff>isEmpty</font>() { <font color=#ff8000>return</font><font color=#0000ff> </font>(numElts == 0); } <font color = #ff0080>/** Returns the first position in the list; O(1) time */</font> <font color=#8000a0><font color=#8000a0>public</font> </font>Position<E> <font color=#0000ff>first</font>() <font color=#8000a0><font color=#ff8000>throws</font> </font>EmptyListException { <font color=#ff8000>if</font><font color=#0000ff> </font>(<font color=#0000ff>isEmpty</font>()) <font color=#8000a0><font color=#ff8000>throw</font> </font><font color=#ff8000>new</font> <font color=#0000ff>EmptyListException</font>(<font color=#008000>"List is empty"</font>); <font color=#8000a0><font color=#ff8000>return</font> </font>header.<font color=#0000ff>getNext</font>(); } <font color = #ff0080>/** Returns the position before the given one; O(1) time */</font> <font color=#8000a0><font color=#8000a0>public</font> </font>Position<E> <font color=#0000ff>prev</font>(Position<E> p) <font color=#8000a0><font color=#ff8000>throws</font> </font>InvalidPositionException, BoundaryViolationException { DNode<E> v = <font color=#0000ff>checkPosition</font>(p); DNode<E> prev = v.<font color=#0000ff>getPrev</font>(); <font color=#ff8000>if</font><font color=#0000ff> </font>(prev == header) <font color=#8000a0><font color=#ff8000>throw</font> </font><font color=#ff8000>new</font> BoundaryViolationException<font color=#0000ff> </font>(<font color=#008000>"Cannot advance past the beginning of the list"</font>); <font color=#8000a0><font color=#ff8000>return</font> </font>prev; } <font color=#ff0080>/** Insert the given element before the given position, returning * the new position; O(1) time */</font> <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>addBefore</font>(Position<E> p, <font color=#8000a0>E </font>element) <font color=#8000a0><font color=#ff8000>throws</font> </font>InvalidPositionException { <font color=#ff0080>// </font> DNode<E> v = <font color=#0000ff>checkPosition</font>(p); numElts++; DNode<E> newNode = <font color=#8000a0><font color=#ff8000>new</font> </font>DNode<E><font color=#0000ff></font>(v.<font color=#0000ff>getPrev</font>(), v, element); v.<font color=#0000ff>getPrev</font>().<font color=#0000ff>setNext</font>(newNode); v.<font color=#0000ff>setPrev</font>(newNode); }</dl></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?