|
|
|
@ -1751,7 +1751,7 @@
|
|
|
|
|
<p align="center"> Fig. 双向队列的操作 </p>
|
|
|
|
|
|
|
|
|
|
<h2 id="531">5.3.1. 双向队列常用操作<a class="headerlink" href="#531" title="Permanent link">¶</a></h2>
|
|
|
|
|
<p>双向队列的常用操作见下表,方法名需根据语言来确定,此处以 Java 为例。</p>
|
|
|
|
|
<p>双向队列的常用操作见下表,方法名需根据语言来确定。</p>
|
|
|
|
|
<div class="center-table">
|
|
|
|
|
<table>
|
|
|
|
|
<thead>
|
|
|
|
@ -1773,12 +1773,12 @@
|
|
|
|
|
<td><span class="arithmatex">\(O(1)\)</span></td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td>pollFirst()</td>
|
|
|
|
|
<td>popFirst()</td>
|
|
|
|
|
<td>删除队首元素</td>
|
|
|
|
|
<td><span class="arithmatex">\(O(1)\)</span></td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td>pollLast()</td>
|
|
|
|
|
<td>popLast()</td>
|
|
|
|
|
<td>删除队尾元素</td>
|
|
|
|
|
<td><span class="arithmatex">\(O(1)\)</span></td>
|
|
|
|
|
</tr>
|
|
|
|
@ -1814,8 +1814,8 @@
|
|
|
|
|
<a id="__codelineno-0-13" name="__codelineno-0-13" href="#__codelineno-0-13"></a><span class="kt">int</span><span class="w"> </span><span class="n">peekLast</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">deque</span><span class="p">.</span><span class="na">peekLast</span><span class="p">();</span><span class="w"> </span><span class="c1">// 队尾元素</span>
|
|
|
|
|
<a id="__codelineno-0-14" name="__codelineno-0-14" href="#__codelineno-0-14"></a>
|
|
|
|
|
<a id="__codelineno-0-15" name="__codelineno-0-15" href="#__codelineno-0-15"></a><span class="cm">/* 元素出队 */</span>
|
|
|
|
|
<a id="__codelineno-0-16" name="__codelineno-0-16" href="#__codelineno-0-16"></a><span class="kt">int</span><span class="w"> </span><span class="n">pollFirst</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">deque</span><span class="p">.</span><span class="na">pollFirst</span><span class="p">();</span><span class="w"> </span><span class="c1">// 队首元素出队</span>
|
|
|
|
|
<a id="__codelineno-0-17" name="__codelineno-0-17" href="#__codelineno-0-17"></a><span class="kt">int</span><span class="w"> </span><span class="n">pollLast</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">deque</span><span class="p">.</span><span class="na">pollLast</span><span class="p">();</span><span class="w"> </span><span class="c1">// 队尾元素出队</span>
|
|
|
|
|
<a id="__codelineno-0-16" name="__codelineno-0-16" href="#__codelineno-0-16"></a><span class="kt">int</span><span class="w"> </span><span class="n">popFirst</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">deque</span><span class="p">.</span><span class="na">pollFirst</span><span class="p">();</span><span class="w"> </span><span class="c1">// 队首元素出队</span>
|
|
|
|
|
<a id="__codelineno-0-17" name="__codelineno-0-17" href="#__codelineno-0-17"></a><span class="kt">int</span><span class="w"> </span><span class="n">popLast</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">deque</span><span class="p">.</span><span class="na">pollLast</span><span class="p">();</span><span class="w"> </span><span class="c1">// 队尾元素出队</span>
|
|
|
|
|
<a id="__codelineno-0-18" name="__codelineno-0-18" href="#__codelineno-0-18"></a>
|
|
|
|
|
<a id="__codelineno-0-19" name="__codelineno-0-19" href="#__codelineno-0-19"></a><span class="cm">/* 获取双向队列的长度 */</span>
|
|
|
|
|
<a id="__codelineno-0-20" name="__codelineno-0-20" href="#__codelineno-0-20"></a><span class="kt">int</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">deque</span><span class="p">.</span><span class="na">size</span><span class="p">();</span>
|
|
|
|
@ -2023,9 +2023,9 @@
|
|
|
|
|
<a id="__codelineno-8-14" name="__codelineno-8-14" href="#__codelineno-8-14"></a><span class="kd">let</span> <span class="nv">peekLast</span> <span class="p">=</span> <span class="n">deque</span><span class="p">.</span><span class="bp">last</span><span class="p">!</span> <span class="c1">// 队尾元素</span>
|
|
|
|
|
<a id="__codelineno-8-15" name="__codelineno-8-15" href="#__codelineno-8-15"></a>
|
|
|
|
|
<a id="__codelineno-8-16" name="__codelineno-8-16" href="#__codelineno-8-16"></a><span class="cm">/* 元素出队 */</span>
|
|
|
|
|
<a id="__codelineno-8-17" name="__codelineno-8-17" href="#__codelineno-8-17"></a><span class="c1">// 使用 Array 模拟时 pollFirst 的复杂度为 O(n)</span>
|
|
|
|
|
<a id="__codelineno-8-18" name="__codelineno-8-18" href="#__codelineno-8-18"></a><span class="kd">let</span> <span class="nv">pollFirst</span> <span class="p">=</span> <span class="n">deque</span><span class="p">.</span><span class="n">removeFirst</span><span class="p">()</span> <span class="c1">// 队首元素出队</span>
|
|
|
|
|
<a id="__codelineno-8-19" name="__codelineno-8-19" href="#__codelineno-8-19"></a><span class="kd">let</span> <span class="nv">pollLast</span> <span class="p">=</span> <span class="n">deque</span><span class="p">.</span><span class="bp">removeLast</span><span class="p">()</span> <span class="c1">// 队尾元素出队</span>
|
|
|
|
|
<a id="__codelineno-8-17" name="__codelineno-8-17" href="#__codelineno-8-17"></a><span class="c1">// 使用 Array 模拟时 popFirst 的复杂度为 O(n)</span>
|
|
|
|
|
<a id="__codelineno-8-18" name="__codelineno-8-18" href="#__codelineno-8-18"></a><span class="kd">let</span> <span class="nv">popFirst</span> <span class="p">=</span> <span class="n">deque</span><span class="p">.</span><span class="n">removeFirst</span><span class="p">()</span> <span class="c1">// 队首元素出队</span>
|
|
|
|
|
<a id="__codelineno-8-19" name="__codelineno-8-19" href="#__codelineno-8-19"></a><span class="kd">let</span> <span class="nv">popLast</span> <span class="p">=</span> <span class="n">deque</span><span class="p">.</span><span class="bp">removeLast</span><span class="p">()</span> <span class="c1">// 队尾元素出队</span>
|
|
|
|
|
<a id="__codelineno-8-20" name="__codelineno-8-20" href="#__codelineno-8-20"></a>
|
|
|
|
|
<a id="__codelineno-8-21" name="__codelineno-8-21" href="#__codelineno-8-21"></a><span class="cm">/* 获取双向队列的长度 */</span>
|
|
|
|
|
<a id="__codelineno-8-22" name="__codelineno-8-22" href="#__codelineno-8-22"></a><span class="kd">let</span> <span class="nv">size</span> <span class="p">=</span> <span class="n">deque</span><span class="p">.</span><span class="bp">count</span>
|
|
|
|
@ -2046,7 +2046,7 @@
|
|
|
|
|
<p>回忆上节内容,由于可以方便地删除链表头结点(对应出队操作),以及在链表尾结点后添加新结点(对应入队操作),因此我们使用普通单向链表来实现队列。</p>
|
|
|
|
|
<p>而双向队列的头部和尾部都可以执行入队与出队操作,换言之,双向队列的操作是“首尾对称”的,也需要实现另一个对称方向的操作。因此,双向队列需要使用「双向链表」来实现。</p>
|
|
|
|
|
<p>我们将双向链表的头结点和尾结点分别看作双向队列的队首和队尾,并且实现在两端都能添加与删除结点。</p>
|
|
|
|
|
<div class="tabbed-set tabbed-alternate" data-tabs="2:5"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">LinkedListDeque</label><label for="__tabbed_2_2">pushLast()</label><label for="__tabbed_2_3">pushFirst()</label><label for="__tabbed_2_4">pollLast()</label><label for="__tabbed_2_5">pollFirst()</label></div>
|
|
|
|
|
<div class="tabbed-set tabbed-alternate" data-tabs="2:5"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">LinkedListDeque</label><label for="__tabbed_2_2">pushLast()</label><label for="__tabbed_2_3">pushFirst()</label><label for="__tabbed_2_4">popLast()</label><label for="__tabbed_2_5">popFirst()</label></div>
|
|
|
|
|
<div class="tabbed-content">
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="基于链表实现双向队列的入队出队操作" src="../deque.assets/linkedlist_deque.png" /></p>
|
|
|
|
@ -2058,10 +2058,10 @@
|
|
|
|
|
<p><img alt="linkedlist_deque_push_first" src="../deque.assets/linkedlist_deque_push_first.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="linkedlist_deque_poll_last" src="../deque.assets/linkedlist_deque_poll_last.png" /></p>
|
|
|
|
|
<p><img alt="linkedlist_deque_pop_last" src="../deque.assets/linkedlist_deque_pop_last.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="linkedlist_deque_poll_first" src="../deque.assets/linkedlist_deque_poll_first.png" /></p>
|
|
|
|
|
<p><img alt="linkedlist_deque_pop_first" src="../deque.assets/linkedlist_deque_pop_first.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
@ -2132,7 +2132,7 @@
|
|
|
|
|
<a id="__codelineno-10-61" name="__codelineno-10-61" href="#__codelineno-10-61"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-10-62" name="__codelineno-10-62" href="#__codelineno-10-62"></a>
|
|
|
|
|
<a id="__codelineno-10-63" name="__codelineno-10-63" href="#__codelineno-10-63"></a><span class="w"> </span><span class="cm">/* 出队操作 */</span>
|
|
|
|
|
<a id="__codelineno-10-64" name="__codelineno-10-64" href="#__codelineno-10-64"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="n">Integer</span><span class="w"> </span><span class="nf">poll</span><span class="p">(</span><span class="kt">boolean</span><span class="w"> </span><span class="n">isFront</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-10-64" name="__codelineno-10-64" href="#__codelineno-10-64"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="n">Integer</span><span class="w"> </span><span class="nf">pop</span><span class="p">(</span><span class="kt">boolean</span><span class="w"> </span><span class="n">isFront</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-10-65" name="__codelineno-10-65" href="#__codelineno-10-65"></a><span class="w"> </span><span class="c1">// 若队列为空,直接返回 null</span>
|
|
|
|
|
<a id="__codelineno-10-66" name="__codelineno-10-66" href="#__codelineno-10-66"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">isEmpty</span><span class="p">())</span>
|
|
|
|
|
<a id="__codelineno-10-67" name="__codelineno-10-67" href="#__codelineno-10-67"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
|
|
|
|
@ -2163,13 +2163,13 @@
|
|
|
|
|
<a id="__codelineno-10-92" name="__codelineno-10-92" href="#__codelineno-10-92"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-10-93" name="__codelineno-10-93" href="#__codelineno-10-93"></a>
|
|
|
|
|
<a id="__codelineno-10-94" name="__codelineno-10-94" href="#__codelineno-10-94"></a><span class="w"> </span><span class="cm">/* 队首出队 */</span>
|
|
|
|
|
<a id="__codelineno-10-95" name="__codelineno-10-95" href="#__codelineno-10-95"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">Integer</span><span class="w"> </span><span class="nf">pollFirst</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-10-96" name="__codelineno-10-96" href="#__codelineno-10-96"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">poll</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-10-95" name="__codelineno-10-95" href="#__codelineno-10-95"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">Integer</span><span class="w"> </span><span class="nf">popFirst</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-10-96" name="__codelineno-10-96" href="#__codelineno-10-96"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">pop</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-10-97" name="__codelineno-10-97" href="#__codelineno-10-97"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-10-98" name="__codelineno-10-98" href="#__codelineno-10-98"></a>
|
|
|
|
|
<a id="__codelineno-10-99" name="__codelineno-10-99" href="#__codelineno-10-99"></a><span class="w"> </span><span class="cm">/* 队尾出队 */</span>
|
|
|
|
|
<a id="__codelineno-10-100" name="__codelineno-10-100" href="#__codelineno-10-100"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">Integer</span><span class="w"> </span><span class="nf">pollLast</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-10-101" name="__codelineno-10-101" href="#__codelineno-10-101"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">poll</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-10-100" name="__codelineno-10-100" href="#__codelineno-10-100"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">Integer</span><span class="w"> </span><span class="nf">popLast</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-10-101" name="__codelineno-10-101" href="#__codelineno-10-101"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">pop</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-10-102" name="__codelineno-10-102" href="#__codelineno-10-102"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-10-103" name="__codelineno-10-103" href="#__codelineno-10-103"></a>
|
|
|
|
|
<a id="__codelineno-10-104" name="__codelineno-10-104" href="#__codelineno-10-104"></a><span class="w"> </span><span class="cm">/* 访问队首元素 */</span>
|
|
|
|
@ -2268,7 +2268,7 @@
|
|
|
|
|
<a id="__codelineno-11-70" name="__codelineno-11-70" href="#__codelineno-11-70"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-11-71" name="__codelineno-11-71" href="#__codelineno-11-71"></a>
|
|
|
|
|
<a id="__codelineno-11-72" name="__codelineno-11-72" href="#__codelineno-11-72"></a><span class="w"> </span><span class="cm">/* 出队操作 */</span>
|
|
|
|
|
<a id="__codelineno-11-73" name="__codelineno-11-73" href="#__codelineno-11-73"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">poll</span><span class="p">(</span><span class="kt">bool</span><span class="w"> </span><span class="n">isFront</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-11-73" name="__codelineno-11-73" href="#__codelineno-11-73"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">pop</span><span class="p">(</span><span class="kt">bool</span><span class="w"> </span><span class="n">isFront</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-11-74" name="__codelineno-11-74" href="#__codelineno-11-74"></a><span class="w"> </span><span class="c1">// 若队列为空,直接返回 -1</span>
|
|
|
|
|
<a id="__codelineno-11-75" name="__codelineno-11-75" href="#__codelineno-11-75"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">isEmpty</span><span class="p">())</span>
|
|
|
|
|
<a id="__codelineno-11-76" name="__codelineno-11-76" href="#__codelineno-11-76"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
|
|
|
|
@ -2299,13 +2299,13 @@
|
|
|
|
|
<a id="__codelineno-11-101" name="__codelineno-11-101" href="#__codelineno-11-101"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-11-102" name="__codelineno-11-102" href="#__codelineno-11-102"></a>
|
|
|
|
|
<a id="__codelineno-11-103" name="__codelineno-11-103" href="#__codelineno-11-103"></a><span class="w"> </span><span class="cm">/* 队首出队 */</span>
|
|
|
|
|
<a id="__codelineno-11-104" name="__codelineno-11-104" href="#__codelineno-11-104"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">pollFirst</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-11-105" name="__codelineno-11-105" href="#__codelineno-11-105"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">poll</span><span class="p">(</span><span class="nb">true</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-11-104" name="__codelineno-11-104" href="#__codelineno-11-104"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">popFirst</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-11-105" name="__codelineno-11-105" href="#__codelineno-11-105"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">pop</span><span class="p">(</span><span class="nb">true</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-11-106" name="__codelineno-11-106" href="#__codelineno-11-106"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-11-107" name="__codelineno-11-107" href="#__codelineno-11-107"></a>
|
|
|
|
|
<a id="__codelineno-11-108" name="__codelineno-11-108" href="#__codelineno-11-108"></a><span class="w"> </span><span class="cm">/* 队尾出队 */</span>
|
|
|
|
|
<a id="__codelineno-11-109" name="__codelineno-11-109" href="#__codelineno-11-109"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">pollLast</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-11-110" name="__codelineno-11-110" href="#__codelineno-11-110"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">poll</span><span class="p">(</span><span class="nb">false</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-11-109" name="__codelineno-11-109" href="#__codelineno-11-109"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">popLast</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-11-110" name="__codelineno-11-110" href="#__codelineno-11-110"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">pop</span><span class="p">(</span><span class="nb">false</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-11-111" name="__codelineno-11-111" href="#__codelineno-11-111"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-11-112" name="__codelineno-11-112" href="#__codelineno-11-112"></a>
|
|
|
|
|
<a id="__codelineno-11-113" name="__codelineno-11-113" href="#__codelineno-11-113"></a><span class="w"> </span><span class="cm">/* 访问队首元素 */</span>
|
|
|
|
@ -2384,7 +2384,7 @@
|
|
|
|
|
<a id="__codelineno-12-50" name="__codelineno-12-50" href="#__codelineno-12-50"></a><span class="w"> </span><span class="sd">""" 队尾入队 """</span>
|
|
|
|
|
<a id="__codelineno-12-51" name="__codelineno-12-51" href="#__codelineno-12-51"></a> <span class="bp">self</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-12-52" name="__codelineno-12-52" href="#__codelineno-12-52"></a>
|
|
|
|
|
<a id="__codelineno-12-53" name="__codelineno-12-53" href="#__codelineno-12-53"></a> <span class="k">def</span> <span class="nf">poll</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">is_front</span><span class="p">:</span> <span class="nb">bool</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-12-53" name="__codelineno-12-53" href="#__codelineno-12-53"></a> <span class="k">def</span> <span class="nf">pop</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">is_front</span><span class="p">:</span> <span class="nb">bool</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-12-54" name="__codelineno-12-54" href="#__codelineno-12-54"></a><span class="w"> </span><span class="sd">""" 出队操作 """</span>
|
|
|
|
|
<a id="__codelineno-12-55" name="__codelineno-12-55" href="#__codelineno-12-55"></a> <span class="c1"># 若队列为空,直接返回 None</span>
|
|
|
|
|
<a id="__codelineno-12-56" name="__codelineno-12-56" href="#__codelineno-12-56"></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>
|
|
|
|
@ -2410,13 +2410,13 @@
|
|
|
|
|
<a id="__codelineno-12-76" name="__codelineno-12-76" href="#__codelineno-12-76"></a> <span class="bp">self</span><span class="o">.</span><span class="n">__size</span> <span class="o">-=</span> <span class="mi">1</span> <span class="c1"># 更新队列长度</span>
|
|
|
|
|
<a id="__codelineno-12-77" name="__codelineno-12-77" href="#__codelineno-12-77"></a> <span class="k">return</span> <span class="n">val</span>
|
|
|
|
|
<a id="__codelineno-12-78" name="__codelineno-12-78" href="#__codelineno-12-78"></a>
|
|
|
|
|
<a id="__codelineno-12-79" name="__codelineno-12-79" href="#__codelineno-12-79"></a> <span class="k">def</span> <span class="nf">poll_first</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-12-79" name="__codelineno-12-79" href="#__codelineno-12-79"></a> <span class="k">def</span> <span class="nf">pop_first</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-12-80" name="__codelineno-12-80" href="#__codelineno-12-80"></a><span class="w"> </span><span class="sd">""" 队首出队 """</span>
|
|
|
|
|
<a id="__codelineno-12-81" name="__codelineno-12-81" href="#__codelineno-12-81"></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">poll</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-12-81" name="__codelineno-12-81" href="#__codelineno-12-81"></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-12-82" name="__codelineno-12-82" href="#__codelineno-12-82"></a>
|
|
|
|
|
<a id="__codelineno-12-83" name="__codelineno-12-83" href="#__codelineno-12-83"></a> <span class="k">def</span> <span class="nf">poll_last</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-12-83" name="__codelineno-12-83" href="#__codelineno-12-83"></a> <span class="k">def</span> <span class="nf">pop_last</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-12-84" name="__codelineno-12-84" href="#__codelineno-12-84"></a><span class="w"> </span><span class="sd">""" 队尾出队 """</span>
|
|
|
|
|
<a id="__codelineno-12-85" name="__codelineno-12-85" href="#__codelineno-12-85"></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">poll</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-12-85" name="__codelineno-12-85" href="#__codelineno-12-85"></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-12-86" name="__codelineno-12-86" href="#__codelineno-12-86"></a>
|
|
|
|
|
<a id="__codelineno-12-87" name="__codelineno-12-87" href="#__codelineno-12-87"></a> <span class="k">def</span> <span class="nf">peek_first</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-12-88" name="__codelineno-12-88" href="#__codelineno-12-88"></a><span class="w"> </span><span class="sd">""" 访问队首元素 """</span>
|
|
|
|
@ -2461,7 +2461,7 @@
|
|
|
|
|
<a id="__codelineno-13-22" name="__codelineno-13-22" href="#__codelineno-13-22"></a><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-13-23" name="__codelineno-13-23" href="#__codelineno-13-23"></a>
|
|
|
|
|
<a id="__codelineno-13-24" name="__codelineno-13-24" href="#__codelineno-13-24"></a><span class="cm">/* 队首元素出队 */</span>
|
|
|
|
|
<a id="__codelineno-13-25" name="__codelineno-13-25" href="#__codelineno-13-25"></a><span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">s</span><span class="w"> </span><span class="o">*</span><span class="nx">linkedListDeque</span><span class="p">)</span><span class="w"> </span><span class="nx">pollFirst</span><span class="p">()</span><span class="w"> </span><span class="kt">any</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-13-25" name="__codelineno-13-25" href="#__codelineno-13-25"></a><span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">s</span><span class="w"> </span><span class="o">*</span><span class="nx">linkedListDeque</span><span class="p">)</span><span class="w"> </span><span class="nx">popFirst</span><span class="p">()</span><span class="w"> </span><span class="kt">any</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-13-26" name="__codelineno-13-26" href="#__codelineno-13-26"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nx">s</span><span class="p">.</span><span class="nx">isEmpty</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-13-27" name="__codelineno-13-27" href="#__codelineno-13-27"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span>
|
|
|
|
|
<a id="__codelineno-13-28" name="__codelineno-13-28" href="#__codelineno-13-28"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
@ -2471,7 +2471,7 @@
|
|
|
|
|
<a id="__codelineno-13-32" name="__codelineno-13-32" href="#__codelineno-13-32"></a><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-13-33" name="__codelineno-13-33" href="#__codelineno-13-33"></a>
|
|
|
|
|
<a id="__codelineno-13-34" name="__codelineno-13-34" href="#__codelineno-13-34"></a><span class="cm">/* 队尾元素出队 */</span>
|
|
|
|
|
<a id="__codelineno-13-35" name="__codelineno-13-35" href="#__codelineno-13-35"></a><span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">s</span><span class="w"> </span><span class="o">*</span><span class="nx">linkedListDeque</span><span class="p">)</span><span class="w"> </span><span class="nx">pollLast</span><span class="p">()</span><span class="w"> </span><span class="kt">any</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-13-35" name="__codelineno-13-35" href="#__codelineno-13-35"></a><span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">s</span><span class="w"> </span><span class="o">*</span><span class="nx">linkedListDeque</span><span class="p">)</span><span class="w"> </span><span class="nx">popLast</span><span class="p">()</span><span class="w"> </span><span class="kt">any</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-13-36" name="__codelineno-13-36" href="#__codelineno-13-36"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nx">s</span><span class="p">.</span><span class="nx">isEmpty</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-13-37" name="__codelineno-13-37" href="#__codelineno-13-37"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span>
|
|
|
|
|
<a id="__codelineno-13-38" name="__codelineno-13-38" href="#__codelineno-13-38"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
@ -2573,7 +2573,7 @@
|
|
|
|
|
<a id="__codelineno-14-56" name="__codelineno-14-56" href="#__codelineno-14-56"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-14-57" name="__codelineno-14-57" href="#__codelineno-14-57"></a>
|
|
|
|
|
<a id="__codelineno-14-58" name="__codelineno-14-58" href="#__codelineno-14-58"></a><span class="w"> </span><span class="cm">/* 队尾出队操作 */</span>
|
|
|
|
|
<a id="__codelineno-14-59" name="__codelineno-14-59" href="#__codelineno-14-59"></a><span class="w"> </span><span class="nx">pollLast</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-14-59" name="__codelineno-14-59" href="#__codelineno-14-59"></a><span class="w"> </span><span class="nx">popLast</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-14-60" name="__codelineno-14-60" href="#__codelineno-14-60"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="err">#</span><span class="nx">queSize</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-14-61" name="__codelineno-14-61" href="#__codelineno-14-61"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-14-62" name="__codelineno-14-62" href="#__codelineno-14-62"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
@ -2590,7 +2590,7 @@
|
|
|
|
|
<a id="__codelineno-14-73" name="__codelineno-14-73" href="#__codelineno-14-73"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-14-74" name="__codelineno-14-74" href="#__codelineno-14-74"></a>
|
|
|
|
|
<a id="__codelineno-14-75" name="__codelineno-14-75" href="#__codelineno-14-75"></a><span class="w"> </span><span class="cm">/* 队首出队操作 */</span>
|
|
|
|
|
<a id="__codelineno-14-76" name="__codelineno-14-76" href="#__codelineno-14-76"></a><span class="w"> </span><span class="nx">pollFirst</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-14-76" name="__codelineno-14-76" href="#__codelineno-14-76"></a><span class="w"> </span><span class="nx">popFirst</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-14-77" name="__codelineno-14-77" href="#__codelineno-14-77"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="err">#</span><span class="nx">queSize</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-14-78" name="__codelineno-14-78" href="#__codelineno-14-78"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-14-79" name="__codelineno-14-79" href="#__codelineno-14-79"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
@ -2698,7 +2698,7 @@
|
|
|
|
|
<a id="__codelineno-15-56" name="__codelineno-15-56" href="#__codelineno-15-56"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-15-57" name="__codelineno-15-57" href="#__codelineno-15-57"></a>
|
|
|
|
|
<a id="__codelineno-15-58" name="__codelineno-15-58" href="#__codelineno-15-58"></a><span class="w"> </span><span class="cm">/* 队尾出队操作 */</span>
|
|
|
|
|
<a id="__codelineno-15-59" name="__codelineno-15-59" href="#__codelineno-15-59"></a><span class="w"> </span><span class="nx">pollLast</span><span class="p">()</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-15-59" name="__codelineno-15-59" href="#__codelineno-15-59"></a><span class="w"> </span><span class="nx">popLast</span><span class="p">()</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-15-60" name="__codelineno-15-60" href="#__codelineno-15-60"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">queSize</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-15-61" name="__codelineno-15-61" href="#__codelineno-15-61"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-15-62" name="__codelineno-15-62" href="#__codelineno-15-62"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
@ -2715,7 +2715,7 @@
|
|
|
|
|
<a id="__codelineno-15-73" name="__codelineno-15-73" href="#__codelineno-15-73"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-15-74" name="__codelineno-15-74" href="#__codelineno-15-74"></a>
|
|
|
|
|
<a id="__codelineno-15-75" name="__codelineno-15-75" href="#__codelineno-15-75"></a><span class="w"> </span><span class="cm">/* 队首出队操作 */</span>
|
|
|
|
|
<a id="__codelineno-15-76" name="__codelineno-15-76" href="#__codelineno-15-76"></a><span class="w"> </span><span class="nx">pollFirst</span><span class="p">()</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-15-76" name="__codelineno-15-76" href="#__codelineno-15-76"></a><span class="w"> </span><span class="nx">popFirst</span><span class="p">()</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-15-77" name="__codelineno-15-77" href="#__codelineno-15-77"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">queSize</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-15-78" name="__codelineno-15-78" href="#__codelineno-15-78"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-15-79" name="__codelineno-15-79" href="#__codelineno-15-79"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
@ -2844,7 +2844,7 @@
|
|
|
|
|
<a id="__codelineno-18-65" name="__codelineno-18-65" href="#__codelineno-18-65"></a> <span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-18-66" name="__codelineno-18-66" href="#__codelineno-18-66"></a>
|
|
|
|
|
<a id="__codelineno-18-67" name="__codelineno-18-67" href="#__codelineno-18-67"></a> <span class="cm">/* 出队操作 */</span>
|
|
|
|
|
<a id="__codelineno-18-68" name="__codelineno-18-68" href="#__codelineno-18-68"></a> <span class="kd">private</span> <span class="kd">func</span> <span class="nf">poll</span><span class="p">(</span><span class="n">isFront</span><span class="p">:</span> <span class="nb">Bool</span><span class="p">)</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-18-68" name="__codelineno-18-68" href="#__codelineno-18-68"></a> <span class="kd">private</span> <span class="kd">func</span> <span class="nf">pop</span><span class="p">(</span><span class="n">isFront</span><span class="p">:</span> <span class="nb">Bool</span><span class="p">)</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-18-69" name="__codelineno-18-69" href="#__codelineno-18-69"></a> <span class="k">if</span> <span class="bp">isEmpty</span><span class="p">()</span> <span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-18-70" name="__codelineno-18-70" href="#__codelineno-18-70"></a> <span class="bp">fatalError</span><span class="p">(</span><span class="s">"双向队列为空"</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-18-71" name="__codelineno-18-71" href="#__codelineno-18-71"></a> <span class="p">}</span>
|
|
|
|
@ -2876,13 +2876,13 @@
|
|
|
|
|
<a id="__codelineno-18-97" name="__codelineno-18-97" href="#__codelineno-18-97"></a> <span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-18-98" name="__codelineno-18-98" href="#__codelineno-18-98"></a>
|
|
|
|
|
<a id="__codelineno-18-99" name="__codelineno-18-99" href="#__codelineno-18-99"></a> <span class="cm">/* 队首出队 */</span>
|
|
|
|
|
<a id="__codelineno-18-100" name="__codelineno-18-100" href="#__codelineno-18-100"></a> <span class="kd">func</span> <span class="nf">pollFirst</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-18-101" name="__codelineno-18-101" href="#__codelineno-18-101"></a> <span class="n">poll</span><span class="p">(</span><span class="n">isFront</span><span class="p">:</span> <span class="kc">true</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-18-100" name="__codelineno-18-100" href="#__codelineno-18-100"></a> <span class="kd">func</span> <span class="nf">popFirst</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-18-101" name="__codelineno-18-101" href="#__codelineno-18-101"></a> <span class="n">pop</span><span class="p">(</span><span class="n">isFront</span><span class="p">:</span> <span class="kc">true</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-18-102" name="__codelineno-18-102" href="#__codelineno-18-102"></a> <span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-18-103" name="__codelineno-18-103" href="#__codelineno-18-103"></a>
|
|
|
|
|
<a id="__codelineno-18-104" name="__codelineno-18-104" href="#__codelineno-18-104"></a> <span class="cm">/* 队尾出队 */</span>
|
|
|
|
|
<a id="__codelineno-18-105" name="__codelineno-18-105" href="#__codelineno-18-105"></a> <span class="kd">func</span> <span class="nf">pollLast</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-18-106" name="__codelineno-18-106" href="#__codelineno-18-106"></a> <span class="n">poll</span><span class="p">(</span><span class="n">isFront</span><span class="p">:</span> <span class="kc">false</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-18-105" name="__codelineno-18-105" href="#__codelineno-18-105"></a> <span class="kd">func</span> <span class="nf">popLast</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-18-106" name="__codelineno-18-106" href="#__codelineno-18-106"></a> <span class="n">pop</span><span class="p">(</span><span class="n">isFront</span><span class="p">:</span> <span class="kc">false</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-18-107" name="__codelineno-18-107" href="#__codelineno-18-107"></a> <span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-18-108" name="__codelineno-18-108" href="#__codelineno-18-108"></a>
|
|
|
|
|
<a id="__codelineno-18-109" name="__codelineno-18-109" href="#__codelineno-18-109"></a> <span class="cm">/* 访问队首元素 */</span>
|
|
|
|
@ -3000,7 +3000,7 @@
|
|
|
|
|
<a id="__codelineno-19-89" name="__codelineno-19-89" href="#__codelineno-19-89"></a><span class="w"> </span><span class="p">}</span><span class="w"> </span>
|
|
|
|
|
<a id="__codelineno-19-90" name="__codelineno-19-90" href="#__codelineno-19-90"></a>
|
|
|
|
|
<a id="__codelineno-19-91" name="__codelineno-19-91" href="#__codelineno-19-91"></a><span class="w"> </span><span class="c1">// 出队操作</span>
|
|
|
|
|
<a id="__codelineno-19-92" name="__codelineno-19-92" href="#__codelineno-19-92"></a><span class="w"> </span><span class="kr">pub</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">poll</span><span class="p">(</span><span class="n">self</span><span class="o">:</span><span class="w"> </span><span class="o">*</span><span class="n">Self</span><span class="p">,</span><span class="w"> </span><span class="n">isFront</span><span class="o">:</span><span class="w"> </span><span class="kt">bool</span><span class="p">)</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-19-92" name="__codelineno-19-92" href="#__codelineno-19-92"></a><span class="w"> </span><span class="kr">pub</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">pop</span><span class="p">(</span><span class="n">self</span><span class="o">:</span><span class="w"> </span><span class="o">*</span><span class="n">Self</span><span class="p">,</span><span class="w"> </span><span class="n">isFront</span><span class="o">:</span><span class="w"> </span><span class="kt">bool</span><span class="p">)</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-19-93" name="__codelineno-19-93" href="#__codelineno-19-93"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">isEmpty</span><span class="p">())</span><span class="w"> </span><span class="nb">@panic</span><span class="p">(</span><span class="s">"双向队列为空"</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-19-94" name="__codelineno-19-94" href="#__codelineno-19-94"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">val</span><span class="o">:</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">undefined</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-19-95" name="__codelineno-19-95" href="#__codelineno-19-95"></a><span class="w"> </span><span class="c1">// 队首出队操作</span>
|
|
|
|
@ -3029,13 +3029,13 @@
|
|
|
|
|
<a id="__codelineno-19-118" name="__codelineno-19-118" href="#__codelineno-19-118"></a><span class="w"> </span><span class="p">}</span><span class="w"> </span>
|
|
|
|
|
<a id="__codelineno-19-119" name="__codelineno-19-119" href="#__codelineno-19-119"></a>
|
|
|
|
|
<a id="__codelineno-19-120" name="__codelineno-19-120" href="#__codelineno-19-120"></a><span class="w"> </span><span class="c1">// 队首出队</span>
|
|
|
|
|
<a id="__codelineno-19-121" name="__codelineno-19-121" href="#__codelineno-19-121"></a><span class="w"> </span><span class="kr">pub</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">pollFirst</span><span class="p">(</span><span class="n">self</span><span class="o">:</span><span class="w"> </span><span class="o">*</span><span class="n">Self</span><span class="p">)</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-19-122" name="__codelineno-19-122" href="#__codelineno-19-122"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">self</span><span class="p">.</span><span class="n">poll</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-19-121" name="__codelineno-19-121" href="#__codelineno-19-121"></a><span class="w"> </span><span class="kr">pub</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">popFirst</span><span class="p">(</span><span class="n">self</span><span class="o">:</span><span class="w"> </span><span class="o">*</span><span class="n">Self</span><span class="p">)</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-19-122" name="__codelineno-19-122" href="#__codelineno-19-122"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">self</span><span class="p">.</span><span class="n">pop</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-19-123" name="__codelineno-19-123" href="#__codelineno-19-123"></a><span class="w"> </span><span class="p">}</span><span class="w"> </span>
|
|
|
|
|
<a id="__codelineno-19-124" name="__codelineno-19-124" href="#__codelineno-19-124"></a>
|
|
|
|
|
<a id="__codelineno-19-125" name="__codelineno-19-125" href="#__codelineno-19-125"></a><span class="w"> </span><span class="c1">// 队尾出队</span>
|
|
|
|
|
<a id="__codelineno-19-126" name="__codelineno-19-126" href="#__codelineno-19-126"></a><span class="w"> </span><span class="kr">pub</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">pollLast</span><span class="p">(</span><span class="n">self</span><span class="o">:</span><span class="w"> </span><span class="o">*</span><span class="n">Self</span><span class="p">)</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-19-127" name="__codelineno-19-127" href="#__codelineno-19-127"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">self</span><span class="p">.</span><span class="n">poll</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-19-126" name="__codelineno-19-126" href="#__codelineno-19-126"></a><span class="w"> </span><span class="kr">pub</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">popLast</span><span class="p">(</span><span class="n">self</span><span class="o">:</span><span class="w"> </span><span class="o">*</span><span class="n">Self</span><span class="p">)</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-19-127" name="__codelineno-19-127" href="#__codelineno-19-127"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">self</span><span class="p">.</span><span class="n">pop</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-19-128" name="__codelineno-19-128" href="#__codelineno-19-128"></a><span class="w"> </span><span class="p">}</span><span class="w"> </span>
|
|
|
|
|
<a id="__codelineno-19-129" name="__codelineno-19-129" href="#__codelineno-19-129"></a>
|
|
|
|
|
<a id="__codelineno-19-130" name="__codelineno-19-130" href="#__codelineno-19-130"></a><span class="w"> </span><span class="c1">// 访问队首元素</span>
|
|
|
|
@ -3083,7 +3083,7 @@
|
|
|
|
|
</div>
|
|
|
|
|
<h3 id="_2">基于数组的实现<a class="headerlink" href="#_2" title="Permanent link">¶</a></h3>
|
|
|
|
|
<p>与基于数组实现队列类似,我们也可以使用环形数组来实现双向队列。在实现队列的基础上,增加实现“队首入队”和“队尾出队”方法即可。</p>
|
|
|
|
|
<div class="tabbed-set tabbed-alternate" data-tabs="4:5"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><input id="__tabbed_4_3" name="__tabbed_4" type="radio" /><input id="__tabbed_4_4" name="__tabbed_4" type="radio" /><input id="__tabbed_4_5" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1">ArrayDeque</label><label for="__tabbed_4_2">pushLast()</label><label for="__tabbed_4_3">pushFirst()</label><label for="__tabbed_4_4">pollLast()</label><label for="__tabbed_4_5">pollFirst()</label></div>
|
|
|
|
|
<div class="tabbed-set tabbed-alternate" data-tabs="4:5"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><input id="__tabbed_4_3" name="__tabbed_4" type="radio" /><input id="__tabbed_4_4" name="__tabbed_4" type="radio" /><input id="__tabbed_4_5" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1">ArrayDeque</label><label for="__tabbed_4_2">pushLast()</label><label for="__tabbed_4_3">pushFirst()</label><label for="__tabbed_4_4">popLast()</label><label for="__tabbed_4_5">popFirst()</label></div>
|
|
|
|
|
<div class="tabbed-content">
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="基于数组实现双向队列的入队出队操作" src="../deque.assets/array_deque.png" /></p>
|
|
|
|
@ -3095,10 +3095,10 @@
|
|
|
|
|
<p><img alt="array_deque_push_first" src="../deque.assets/array_deque_push_first.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="array_deque_poll_last" src="../deque.assets/array_deque_poll_last.png" /></p>
|
|
|
|
|
<p><img alt="array_deque_pop_last" src="../deque.assets/array_deque_pop_last.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="array_deque_poll_first" src="../deque.assets/array_deque_poll_first.png" /></p>
|
|
|
|
|
<p><img alt="array_deque_pop_first" src="../deque.assets/array_deque_pop_first.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
@ -3169,7 +3169,7 @@
|
|
|
|
|
<a id="__codelineno-20-61" name="__codelineno-20-61" href="#__codelineno-20-61"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-20-62" name="__codelineno-20-62" href="#__codelineno-20-62"></a>
|
|
|
|
|
<a id="__codelineno-20-63" name="__codelineno-20-63" href="#__codelineno-20-63"></a><span class="w"> </span><span class="cm">/* 队首出队 */</span>
|
|
|
|
|
<a id="__codelineno-20-64" name="__codelineno-20-64" href="#__codelineno-20-64"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">pollFirst</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-20-64" name="__codelineno-20-64" href="#__codelineno-20-64"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">popFirst</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-20-65" name="__codelineno-20-65" href="#__codelineno-20-65"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">peekFirst</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-20-66" name="__codelineno-20-66" href="#__codelineno-20-66"></a><span class="w"> </span><span class="c1">// 队首指针向后移动一位</span>
|
|
|
|
|
<a id="__codelineno-20-67" name="__codelineno-20-67" href="#__codelineno-20-67"></a><span class="w"> </span><span class="n">front</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">index</span><span class="p">(</span><span class="n">front</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
|
|
|
@ -3178,7 +3178,7 @@
|
|
|
|
|
<a id="__codelineno-20-70" name="__codelineno-20-70" href="#__codelineno-20-70"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-20-71" name="__codelineno-20-71" href="#__codelineno-20-71"></a>
|
|
|
|
|
<a id="__codelineno-20-72" name="__codelineno-20-72" href="#__codelineno-20-72"></a><span class="w"> </span><span class="cm">/* 队尾出队 */</span>
|
|
|
|
|
<a id="__codelineno-20-73" name="__codelineno-20-73" href="#__codelineno-20-73"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">pollLast</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-20-73" name="__codelineno-20-73" href="#__codelineno-20-73"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">popLast</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-20-74" name="__codelineno-20-74" href="#__codelineno-20-74"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">peekLast</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-20-75" name="__codelineno-20-75" href="#__codelineno-20-75"></a><span class="w"> </span><span class="n">queSize</span><span class="o">--</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-20-76" name="__codelineno-20-76" href="#__codelineno-20-76"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">num</span><span class="p">;</span>
|
|
|
|
@ -3278,7 +3278,7 @@
|
|
|
|
|
<a id="__codelineno-21-63" name="__codelineno-21-63" href="#__codelineno-21-63"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-21-64" name="__codelineno-21-64" href="#__codelineno-21-64"></a>
|
|
|
|
|
<a id="__codelineno-21-65" name="__codelineno-21-65" href="#__codelineno-21-65"></a><span class="w"> </span><span class="cm">/* 队首出队 */</span>
|
|
|
|
|
<a id="__codelineno-21-66" name="__codelineno-21-66" href="#__codelineno-21-66"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">pollFirst</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-21-66" name="__codelineno-21-66" href="#__codelineno-21-66"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">popFirst</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-21-67" name="__codelineno-21-67" href="#__codelineno-21-67"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">peekFirst</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-21-68" name="__codelineno-21-68" href="#__codelineno-21-68"></a><span class="w"> </span><span class="c1">// 队首指针向后移动一位</span>
|
|
|
|
|
<a id="__codelineno-21-69" name="__codelineno-21-69" href="#__codelineno-21-69"></a><span class="w"> </span><span class="n">front</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">index</span><span class="p">(</span><span class="n">front</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
|
|
|
@ -3287,7 +3287,7 @@
|
|
|
|
|
<a id="__codelineno-21-72" name="__codelineno-21-72" href="#__codelineno-21-72"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-21-73" name="__codelineno-21-73" href="#__codelineno-21-73"></a>
|
|
|
|
|
<a id="__codelineno-21-74" name="__codelineno-21-74" href="#__codelineno-21-74"></a><span class="w"> </span><span class="cm">/* 队尾出队 */</span>
|
|
|
|
|
<a id="__codelineno-21-75" name="__codelineno-21-75" href="#__codelineno-21-75"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">pollLast</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-21-75" name="__codelineno-21-75" href="#__codelineno-21-75"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">popLast</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-21-76" name="__codelineno-21-76" href="#__codelineno-21-76"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">peekLast</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-21-77" name="__codelineno-21-77" href="#__codelineno-21-77"></a><span class="w"> </span><span class="n">queSize</span><span class="o">--</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-21-78" name="__codelineno-21-78" href="#__codelineno-21-78"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">num</span><span class="p">;</span>
|
|
|
|
@ -3373,7 +3373,7 @@
|
|
|
|
|
<a id="__codelineno-22-49" name="__codelineno-22-49" href="#__codelineno-22-49"></a> <span class="bp">self</span><span class="o">.</span><span class="n">__nums</span><span class="p">[</span><span class="n">rear</span><span class="p">]</span> <span class="o">=</span> <span class="n">num</span>
|
|
|
|
|
<a id="__codelineno-22-50" name="__codelineno-22-50" href="#__codelineno-22-50"></a> <span class="bp">self</span><span class="o">.</span><span class="n">__size</span> <span class="o">+=</span> <span class="mi">1</span>
|
|
|
|
|
<a id="__codelineno-22-51" name="__codelineno-22-51" href="#__codelineno-22-51"></a>
|
|
|
|
|
<a id="__codelineno-22-52" name="__codelineno-22-52" href="#__codelineno-22-52"></a> <span class="k">def</span> <span class="nf">poll_first</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-22-52" name="__codelineno-22-52" href="#__codelineno-22-52"></a> <span class="k">def</span> <span class="nf">pop_first</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-22-53" name="__codelineno-22-53" href="#__codelineno-22-53"></a><span class="w"> </span><span class="sd">""" 队首出队 """</span>
|
|
|
|
|
<a id="__codelineno-22-54" name="__codelineno-22-54" href="#__codelineno-22-54"></a> <span class="n">num</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">peek_first</span><span class="p">()</span>
|
|
|
|
|
<a id="__codelineno-22-55" name="__codelineno-22-55" href="#__codelineno-22-55"></a> <span class="c1"># 队首指针向后移动一位</span>
|
|
|
|
@ -3381,7 +3381,7 @@
|
|
|
|
|
<a id="__codelineno-22-57" name="__codelineno-22-57" href="#__codelineno-22-57"></a> <span class="bp">self</span><span class="o">.</span><span class="n">__size</span> <span class="o">-=</span> <span class="mi">1</span>
|
|
|
|
|
<a id="__codelineno-22-58" name="__codelineno-22-58" href="#__codelineno-22-58"></a> <span class="k">return</span> <span class="n">num</span>
|
|
|
|
|
<a id="__codelineno-22-59" name="__codelineno-22-59" href="#__codelineno-22-59"></a>
|
|
|
|
|
<a id="__codelineno-22-60" name="__codelineno-22-60" href="#__codelineno-22-60"></a> <span class="k">def</span> <span class="nf">poll_last</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-22-60" name="__codelineno-22-60" href="#__codelineno-22-60"></a> <span class="k">def</span> <span class="nf">pop_last</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-22-61" name="__codelineno-22-61" href="#__codelineno-22-61"></a><span class="w"> </span><span class="sd">""" 队尾出队 """</span>
|
|
|
|
|
<a id="__codelineno-22-62" name="__codelineno-22-62" href="#__codelineno-22-62"></a> <span class="n">num</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">peek_last</span><span class="p">()</span>
|
|
|
|
|
<a id="__codelineno-22-63" name="__codelineno-22-63" href="#__codelineno-22-63"></a> <span class="bp">self</span><span class="o">.</span><span class="n">__size</span> <span class="o">-=</span> <span class="mi">1</span>
|
|
|
|
@ -3477,7 +3477,7 @@
|
|
|
|
|
<a id="__codelineno-24-62" name="__codelineno-24-62" href="#__codelineno-24-62"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-24-63" name="__codelineno-24-63" href="#__codelineno-24-63"></a>
|
|
|
|
|
<a id="__codelineno-24-64" name="__codelineno-24-64" href="#__codelineno-24-64"></a><span class="w"> </span><span class="cm">/* 队首出队 */</span>
|
|
|
|
|
<a id="__codelineno-24-65" name="__codelineno-24-65" href="#__codelineno-24-65"></a><span class="w"> </span><span class="nx">pollFirst</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-24-65" name="__codelineno-24-65" href="#__codelineno-24-65"></a><span class="w"> </span><span class="nx">popFirst</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-24-66" name="__codelineno-24-66" href="#__codelineno-24-66"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">peekFirst</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-24-67" name="__codelineno-24-67" href="#__codelineno-24-67"></a><span class="w"> </span><span class="c1">// 队首指针向后移动一位</span>
|
|
|
|
|
<a id="__codelineno-24-68" name="__codelineno-24-68" href="#__codelineno-24-68"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="err">#</span><span class="nx">front</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">index</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="err">#</span><span class="nx">front</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
|
|
|
|
@ -3486,7 +3486,7 @@
|
|
|
|
|
<a id="__codelineno-24-71" name="__codelineno-24-71" href="#__codelineno-24-71"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-24-72" name="__codelineno-24-72" href="#__codelineno-24-72"></a>
|
|
|
|
|
<a id="__codelineno-24-73" name="__codelineno-24-73" href="#__codelineno-24-73"></a><span class="w"> </span><span class="cm">/* 队尾出队 */</span>
|
|
|
|
|
<a id="__codelineno-24-74" name="__codelineno-24-74" href="#__codelineno-24-74"></a><span class="w"> </span><span class="nx">pollLast</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-24-74" name="__codelineno-24-74" href="#__codelineno-24-74"></a><span class="w"> </span><span class="nx">popLast</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-24-75" name="__codelineno-24-75" href="#__codelineno-24-75"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">peekLast</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-24-76" name="__codelineno-24-76" href="#__codelineno-24-76"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="err">#</span><span class="nx">queSize</span><span class="o">--</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-24-77" name="__codelineno-24-77" href="#__codelineno-24-77"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">num</span><span class="p">;</span>
|
|
|
|
@ -3585,7 +3585,7 @@
|
|
|
|
|
<a id="__codelineno-25-62" name="__codelineno-25-62" href="#__codelineno-25-62"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-25-63" name="__codelineno-25-63" href="#__codelineno-25-63"></a>
|
|
|
|
|
<a id="__codelineno-25-64" name="__codelineno-25-64" href="#__codelineno-25-64"></a><span class="w"> </span><span class="cm">/* 队首出队 */</span>
|
|
|
|
|
<a id="__codelineno-25-65" name="__codelineno-25-65" href="#__codelineno-25-65"></a><span class="w"> </span><span class="nx">pollFirst</span><span class="p">()</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-25-65" name="__codelineno-25-65" href="#__codelineno-25-65"></a><span class="w"> </span><span class="nx">popFirst</span><span class="p">()</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-25-66" name="__codelineno-25-66" href="#__codelineno-25-66"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">num</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">peekFirst</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-25-67" name="__codelineno-25-67" href="#__codelineno-25-67"></a><span class="w"> </span><span class="c1">// 队首指针向后移动一位</span>
|
|
|
|
|
<a id="__codelineno-25-68" name="__codelineno-25-68" href="#__codelineno-25-68"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">front</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">index</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">front</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
|
|
|
|
@ -3594,7 +3594,7 @@
|
|
|
|
|
<a id="__codelineno-25-71" name="__codelineno-25-71" href="#__codelineno-25-71"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-25-72" name="__codelineno-25-72" href="#__codelineno-25-72"></a>
|
|
|
|
|
<a id="__codelineno-25-73" name="__codelineno-25-73" href="#__codelineno-25-73"></a><span class="w"> </span><span class="cm">/* 队尾出队 */</span>
|
|
|
|
|
<a id="__codelineno-25-74" name="__codelineno-25-74" href="#__codelineno-25-74"></a><span class="w"> </span><span class="nx">pollLast</span><span class="p">()</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-25-74" name="__codelineno-25-74" href="#__codelineno-25-74"></a><span class="w"> </span><span class="nx">popLast</span><span class="p">()</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-25-75" name="__codelineno-25-75" href="#__codelineno-25-75"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">num</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">peekLast</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-25-76" name="__codelineno-25-76" href="#__codelineno-25-76"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">queSize</span><span class="o">--</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-25-77" name="__codelineno-25-77" href="#__codelineno-25-77"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">num</span><span class="p">;</span>
|
|
|
|
@ -3701,7 +3701,7 @@
|
|
|
|
|
<a id="__codelineno-28-62" name="__codelineno-28-62" href="#__codelineno-28-62"></a> <span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-28-63" name="__codelineno-28-63" href="#__codelineno-28-63"></a>
|
|
|
|
|
<a id="__codelineno-28-64" name="__codelineno-28-64" href="#__codelineno-28-64"></a> <span class="cm">/* 队首出队 */</span>
|
|
|
|
|
<a id="__codelineno-28-65" name="__codelineno-28-65" href="#__codelineno-28-65"></a> <span class="kd">func</span> <span class="nf">pollFirst</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-28-65" name="__codelineno-28-65" href="#__codelineno-28-65"></a> <span class="kd">func</span> <span class="nf">popFirst</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-28-66" name="__codelineno-28-66" href="#__codelineno-28-66"></a> <span class="kd">let</span> <span class="nv">num</span> <span class="p">=</span> <span class="n">peekFirst</span><span class="p">()</span>
|
|
|
|
|
<a id="__codelineno-28-67" name="__codelineno-28-67" href="#__codelineno-28-67"></a> <span class="c1">// 队首指针向后移动一位</span>
|
|
|
|
|
<a id="__codelineno-28-68" name="__codelineno-28-68" href="#__codelineno-28-68"></a> <span class="n">front</span> <span class="p">=</span> <span class="n">index</span><span class="p">(</span><span class="n">i</span><span class="p">:</span> <span class="n">front</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
|
|
|
|
@ -3710,7 +3710,7 @@
|
|
|
|
|
<a id="__codelineno-28-71" name="__codelineno-28-71" href="#__codelineno-28-71"></a> <span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-28-72" name="__codelineno-28-72" href="#__codelineno-28-72"></a>
|
|
|
|
|
<a id="__codelineno-28-73" name="__codelineno-28-73" href="#__codelineno-28-73"></a> <span class="cm">/* 队尾出队 */</span>
|
|
|
|
|
<a id="__codelineno-28-74" name="__codelineno-28-74" href="#__codelineno-28-74"></a> <span class="kd">func</span> <span class="nf">pollLast</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-28-74" name="__codelineno-28-74" href="#__codelineno-28-74"></a> <span class="kd">func</span> <span class="nf">popLast</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-28-75" name="__codelineno-28-75" href="#__codelineno-28-75"></a> <span class="kd">let</span> <span class="nv">num</span> <span class="p">=</span> <span class="n">peekLast</span><span class="p">()</span>
|
|
|
|
|
<a id="__codelineno-28-76" name="__codelineno-28-76" href="#__codelineno-28-76"></a> <span class="n">queSize</span> <span class="o">-=</span> <span class="mi">1</span>
|
|
|
|
|
<a id="__codelineno-28-77" name="__codelineno-28-77" href="#__codelineno-28-77"></a> <span class="k">return</span> <span class="n">num</span>
|
|
|
|
|