mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-29 15:54:44 +08:00
Release reviewed gh-pages candidate
This commit is contained in:
@@ -1975,8 +1975,8 @@
|
||||
<!-- Page content -->
|
||||
<h1 id="53-deques">5.3 Deques<a class="headerlink" href="#53-deques" title="Permanent link">¶</a></h1>
|
||||
<p>A queue allows removal only at the front and insertion only at the rear. As shown below, a <u>double-ended queue</u>, or deque, is more flexible: elements can be added or removed at either end.</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/deque.assets/deque_operations.png" data-dark-src="/assets/figure-svg/figures/4390a9e579efab08495d4606.json" data-src="/assets/figure-svg/figures/8cb6397e78b7184a32b49465.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/deque.assets/deque_operations.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Deque operations</object><object data="/en/chapter_stack_and_queue/deque.assets/deque_operations.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Deque operations</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 5-7 Deque operations </p>
|
||||
<p><ha-svg-figure aria-label="Operations at both ends of a deque" class="animation-figure" data-book-key="en/chapter_stack_and_queue/deque.assets/deque_operations.png" data-dark-src="/assets/figure-svg/figures/4390a9e579efab08495d4606.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/8cb6397e78b7184a32b49465.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/deque_operations.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Deque operations</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/deque_operations.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Deque operations</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 5-7 Deque operations </p><div class="ha-sr-only figure-description"><h2>Description of Figure 5-7: Operations at both ends of a deque</h2><p>Two examples show all four deque operations: inserting and removing an element at the rear and at the front.</p><p>Five vertical panels run from left to right. The front is at the top and the rear at the bottom. All sequences in the table below run from front to rear. The first three panels show initialization, insertion at the rear, and removal from the rear. The last two show insertion at the front and removal from the front.</p><p>Although arrows connect the panels from left to right, the front-insertion panel still contains 4. It does not continue from the preceding panel, which removed 4. The front example starts over from [3, 2, 5, 4]. The table has six rows because it includes this starting state, which has no panel of its own.</p><p>Dashed squares mark an element’s previous position and do not count as current elements. A solid square outside the deque represents the value returned by removal. In the first panel, arrows at each end show the directions of insertion and removal.</p><table><caption>Two independent examples</caption><thead><tr><th scope="col">Example and operation</th><th scope="col">Panel (left to right)</th><th scope="col">Deque after the operation</th><th scope="col">Return value</th></tr></thead><tbody><tr><th scope="row">Rear: initial state</th><td>1</td><td>[3,2,5]</td><td>None</td></tr><tr><th scope="row">Rear: push_last(4)</th><td>2</td><td>[3,2,5,4]</td><td>None</td></tr><tr><th scope="row">Rear: pop_last()</th><td>3</td><td>[3,2,5]</td><td>4</td></tr><tr><th scope="row">Front: independent starting state</th><td>Starting state supplied here; no separate panel</td><td>[3,2,5,4]</td><td>None</td></tr><tr><th scope="row">Front: push_first(1)</th><td>4</td><td>[1,3,2,5,4]</td><td>None</td></tr><tr><th scope="row">Front: pop_first()</th><td>5</td><td>[3,2,5,4]</td><td>1</td></tr></tbody></table><p>The rear example adds and removes 4 at the rear. The front example adds and removes 1 at the front. The remaining elements retain their relative order in both cases. A deque supports insertion and removal at either end.</p></div>
|
||||
<h2 id="531-common-deque-operations">5.3.1 Common deque operations<a class="headerlink" href="#531-common-deque-operations" title="Permanent link">¶</a></h2>
|
||||
<p>Table 5-3 lists common operations. Actual method names depend on the programming language.</p>
|
||||
<p align="center"> Table 5-3 Efficiency of deque operations </p>
|
||||
@@ -2375,26 +2375,26 @@
|
||||
<p>In the previous section, we used a singly linked list for a queue because it easily supports removing the head for dequeueing and adding after the tail for enqueueing.</p>
|
||||
<p>A deque supports both insertion and removal at both ends. It therefore needs the symmetric operations in the other direction as well. For this reason, we use a doubly linked list.</p>
|
||||
<p>As shown below, treat the list's head and tail as the deque's front and rear, and support adding and removing nodes at both ends.</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-figure-sequence="" 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"><1></label><label for="__tabbed_2_2"><2></label><label for="__tabbed_2_3"><3></label><label for="__tabbed_2_4"><4></label><label for="__tabbed_2_5"><5></label></div>
|
||||
<div aria-label="Implementing a deque with a doubly linked list" class="tabbed-set tabbed-alternate" data-figure-sequence="" data-tabs="2:5" role="group"><input aria-label="Step 1: Initial deque" checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio"/><input aria-label="Step 2: Add 4 at the rear" id="__tabbed_2_2" name="__tabbed_2" type="radio"/><input aria-label="Step 3: Add 1 at the front" id="__tabbed_2_3" name="__tabbed_2" type="radio"/><input aria-label="Step 4: Remove 4 from the rear" id="__tabbed_2_4" name="__tabbed_2" type="radio"/><input aria-label="Step 5: Remove 1 from the front" id="__tabbed_2_5" name="__tabbed_2" type="radio"/><div class="tabbed-labels"><label for="__tabbed_2_1"><1></label><label for="__tabbed_2_2"><2></label><label for="__tabbed_2_3"><3></label><label for="__tabbed_2_4"><4></label><label for="__tabbed_2_5"><5></label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step1.png" data-dark-src="/assets/figure-svg/figures/8213f597cf81b8848405ce44.json" data-src="/assets/figure-svg/figures/8cd3a3f5ed42086433c335c3.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step1.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Insertion and removal in a linked-list deque</object><object data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step1.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Insertion and removal in a linked-list deque</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Implementing a deque with a doubly linked list, step 1 of 5: Initial deque" class="animation-figure" data-a11y-forward="Initial list: 3 ⇄ 2 ⇄ 5, with head 3 and tail 5." data-a11y-state="Head 3, tail 5. Deque [3, 2, 5]." data-book-key="en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step1.png" data-dark-src="/assets/figure-svg/figures/8213f597cf81b8848405ce44.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/8cd3a3f5ed42086433c335c3.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step1.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Insertion and removal in a linked-list deque</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step1.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Insertion and removal in a linked-list deque</object></ha-svg-figure></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step2_push_last.png" data-dark-src="/assets/figure-svg/figures/77137a417263f5470c3a8778.json" data-src="/assets/figure-svg/figures/3bd8b54f3caa47448ec26dbf.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step2_push_last.svg" data-theme="light" tabindex="-1" type="image/svg+xml">linkedlist_deque_push_last</object><object data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step2_push_last.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">linkedlist_deque_push_last</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Implementing a deque with a doubly linked list, step 2 of 5: Add 4 at the rear" class="animation-figure" data-a11y-forward="Link 5 and 4 in both directions and update the tail reference to 4." data-a11y-state="Head 3, tail 4. Deque [3, 2, 5, 4]." data-book-key="en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step2_push_last.png" data-dark-src="/assets/figure-svg/figures/77137a417263f5470c3a8778.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/3bd8b54f3caa47448ec26dbf.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step2_push_last.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">linkedlist_deque_push_last</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step2_push_last.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">linkedlist_deque_push_last</object></ha-svg-figure></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step3_push_first.png" data-dark-src="/assets/figure-svg/figures/a8273690ec1d809e346695b5.json" data-src="/assets/figure-svg/figures/e3d761dd4f1f326321f5ce0f.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step3_push_first.svg" data-theme="light" tabindex="-1" type="image/svg+xml">linkedlist_deque_push_first</object><object data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step3_push_first.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">linkedlist_deque_push_first</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Implementing a deque with a doubly linked list, step 3 of 5: Add 1 at the front" class="animation-figure" data-a11y-forward="Link 1 and 3 in both directions and update the head reference to 1." data-a11y-state="Head 1, tail 4. Deque [1, 3, 2, 5, 4]." data-book-key="en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step3_push_first.png" data-dark-src="/assets/figure-svg/figures/a8273690ec1d809e346695b5.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/e3d761dd4f1f326321f5ce0f.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step3_push_first.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">linkedlist_deque_push_first</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step3_push_first.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">linkedlist_deque_push_first</object></ha-svg-figure></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step4_pop_last.png" data-dark-src="/assets/figure-svg/figures/6f50400255b34f6b08a48a4b.json" data-src="/assets/figure-svg/figures/2f716b79ba34c88e0e6f90cf.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step4_pop_last.svg" data-theme="light" tabindex="-1" type="image/svg+xml">linkedlist_deque_pop_last</object><object data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step4_pop_last.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">linkedlist_deque_pop_last</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Implementing a deque with a doubly linked list, step 4 of 5: Remove 4 from the rear" class="animation-figure" data-a11y-forward="Follow 4.prev to 5 and make it the tail. Detach node 4 and return 4." data-a11y-state="Head 1, tail 5. Deque [1, 3, 2, 5]. Returned 4." data-book-key="en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step4_pop_last.png" data-dark-src="/assets/figure-svg/figures/6f50400255b34f6b08a48a4b.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/2f716b79ba34c88e0e6f90cf.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step4_pop_last.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">linkedlist_deque_pop_last</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step4_pop_last.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">linkedlist_deque_pop_last</object></ha-svg-figure></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step5_pop_first.png" data-dark-src="/assets/figure-svg/figures/d3b9a69678d1624adec37db9.json" data-src="/assets/figure-svg/figures/1fe24c2b3927e031c6d25f93.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step5_pop_first.svg" data-theme="light" tabindex="-1" type="image/svg+xml">linkedlist_deque_pop_first</object><object data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step5_pop_first.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">linkedlist_deque_pop_first</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Implementing a deque with a doubly linked list, step 5 of 5: Remove 1 from the front" class="animation-figure" data-a11y-forward="Follow 1.next to 3 and make it the head. Detach node 1 and return 1." data-a11y-state="Head 3, tail 5. Deque [3, 2, 5]. Returned 1." data-book-key="en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step5_pop_first.png" data-dark-src="/assets/figure-svg/figures/d3b9a69678d1624adec37db9.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/1fe24c2b3927e031c6d25f93.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step5_pop_first.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">linkedlist_deque_pop_first</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/linkedlist_deque_step5_pop_first.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">linkedlist_deque_pop_first</object></ha-svg-figure></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p align="center"> Figure 5-8 Insertion and removal in a linked-list deque </p>
|
||||
<p align="center"> Figure 5-8 Insertion and removal in a linked-list deque </p><div class="ha-sr-only figure-description"><h4>Description of Figure 5-8: Implementing a deque with a doubly linked list</h4><p>References to the head and tail and links in both directions allow insertion and removal at either end without moving intermediate nodes.</p><p>The deque is on the left and its vertical doubly linked list on the right. Both run from front to rear, top to bottom. The head is the front and the tail is the rear. Between neighboring nodes, next points down and prev points up.</p><p>Five frames show the initial state, appending 4, prepending 1, removing 4 from the rear, and removing 1 from the front. The new 4 is cyan and 1 is orange. The original elements 3, 2, and 5 stay in place.</p><ol><li><p><strong>Initial deque</strong></p><p>The list starts as 3 ⇄ 2 ⇄ 5. The head is 3 and the tail is 5, giving the deque [3, 2, 5] on the left. There is no return value.</p></li><li><p><strong>Add 4 at the rear</strong></p><p>Call push_last(4). Attach new node 4 below 5. Set 5.next to 4 and 4.prev to 5. The list is now 3 ⇄ 2 ⇄ 5 ⇄ 4. The head is still 3 and the new tail is 4. No value is returned.</p></li><li><p><strong>Add 1 at the front</strong></p><p>Call push_first(1). Attach new node 1 above 3. Set 1.next to 3 and 3.prev to 1. The list is now 1 ⇄ 3 ⇄ 2 ⇄ 5 ⇄ 4. The new head is 1 and the tail is still 4. No value is returned.</p></li><li><p><strong>Remove 4 from the rear</strong></p><p>Call pop_last(). Follow 4.prev directly to 5 and set the tail reference to 5. Disconnect the links between 5 and 4 and return 4. The list is now 1 ⇄ 3 ⇄ 2 ⇄ 5, with head 1 and tail 5.</p></li><li><p><strong>Remove 1 from the front</strong></p><p>Call pop_first(). Follow 1.next to 3 and set the head reference to 3. Disconnect the links between 1 and 3 and return 1. The list is back to 3 ⇄ 2 ⇄ 5, with head 3 and tail 5.</p></li></ol><p>With references to both the head and tail, removing the last element reaches the new tail directly via the old tail's prev link. There is no need to traverse from the head. Insertion and removal at either end update only nearby references and take O(1) time.</p></div>
|
||||
<p>The implementation is as follows:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="3:13"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio"/><input id="__tabbed_3_2" name="__tabbed_3" type="radio"/><input id="__tabbed_3_3" name="__tabbed_3" type="radio"/><input id="__tabbed_3_4" name="__tabbed_3" type="radio"/><input id="__tabbed_3_5" name="__tabbed_3" type="radio"/><input id="__tabbed_3_6" name="__tabbed_3" type="radio"/><input id="__tabbed_3_7" name="__tabbed_3" type="radio"/><input id="__tabbed_3_8" name="__tabbed_3" type="radio"/><input id="__tabbed_3_9" name="__tabbed_3" type="radio"/><input id="__tabbed_3_10" name="__tabbed_3" type="radio"/><input id="__tabbed_3_11" name="__tabbed_3" type="radio"/><input id="__tabbed_3_12" name="__tabbed_3" type="radio"/><input id="__tabbed_3_13" name="__tabbed_3" type="radio"/><div class="tabbed-labels"><label for="__tabbed_3_1">Python</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Java</label><label for="__tabbed_3_4">C#</label><label for="__tabbed_3_5">Go</label><label for="__tabbed_3_6">Swift</label><label for="__tabbed_3_7">JS</label><label for="__tabbed_3_8">TS</label><label for="__tabbed_3_9">Dart</label><label for="__tabbed_3_10">Rust</label><label for="__tabbed_3_11">C</label><label for="__tabbed_3_12">Kotlin</label><label for="__tabbed_3_13">Ruby</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -4141,26 +4141,26 @@
|
||||
</div>
|
||||
<h3 id="2-implementation-with-an-array">2. Implementation with an array<a class="headerlink" href="#2-implementation-with-an-array" title="Permanent link">¶</a></h3>
|
||||
<p>As with an array-based queue, we can use a circular array to implement a deque, as shown below.</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-figure-sequence="" 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"><1></label><label for="__tabbed_4_2"><2></label><label for="__tabbed_4_3"><3></label><label for="__tabbed_4_4"><4></label><label for="__tabbed_4_5"><5></label></div>
|
||||
<div aria-label="Implementing a deque with a circular array" class="tabbed-set tabbed-alternate" data-figure-sequence="" data-tabs="4:5" role="group"><input aria-label="Step 1: Initial deque" checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio"/><input aria-label="Step 2: Add 4 at the rear" id="__tabbed_4_2" name="__tabbed_4" type="radio"/><input aria-label="Step 3: Add 1 at the front" id="__tabbed_4_3" name="__tabbed_4" type="radio"/><input aria-label="Step 4: Remove 4 from the rear" id="__tabbed_4_4" name="__tabbed_4" type="radio"/><input aria-label="Step 5: Remove 1 from the front" id="__tabbed_4_5" name="__tabbed_4" type="radio"/><div class="tabbed-labels"><label for="__tabbed_4_1"><1></label><label for="__tabbed_4_2"><2></label><label for="__tabbed_4_3"><3></label><label for="__tabbed_4_4"><4></label><label for="__tabbed_4_5"><5></label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/deque.assets/array_deque_step1.png" data-dark-src="/assets/figure-svg/figures/b64b4d3d08510b56dce014b3.json" data-src="/assets/figure-svg/figures/7dec4e1f000216645f079296.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/deque.assets/array_deque_step1.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Insertion and removal in an array-based deque</object><object data="/en/chapter_stack_and_queue/deque.assets/array_deque_step1.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Insertion and removal in an array-based deque</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Implementing a deque with a circular array, step 1 of 5: Initial deque" class="animation-figure" data-a11y-forward="Initially the deque is [3, 2, 5], front points to 3, size = 3, and rear points to the first free slot after 5." data-a11y-state="Deque: [3, 2, 5]. The index front points to 3, size = 3, and rear points to the empty slot below 5." data-book-key="en/chapter_stack_and_queue/deque.assets/array_deque_step1.png" data-dark-src="/assets/figure-svg/figures/b64b4d3d08510b56dce014b3.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/7dec4e1f000216645f079296.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/array_deque_step1.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Insertion and removal in an array-based deque</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/array_deque_step1.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Insertion and removal in an array-based deque</object></ha-svg-figure></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/deque.assets/array_deque_step2_push_last.png" data-dark-src="/assets/figure-svg/figures/89132f0fb4a25538b4414855.json" data-src="/assets/figure-svg/figures/e3c6d1b12680683f5297386c.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/deque.assets/array_deque_step2_push_last.svg" data-theme="light" tabindex="-1" type="image/svg+xml">array_deque_push_last</object><object data="/en/chapter_stack_and_queue/deque.assets/array_deque_step2_push_last.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">array_deque_push_last</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Implementing a deque with a circular array, step 2 of 5: Add 4 at the rear" class="animation-figure" data-a11y-forward="Write 4 to the rear slot calculated before insertion and increase size to 4. The rear arrow still marks the slot where 4 was written." data-a11y-state="The index front points to 3, size = 4, and the deque is [3, 2, 5, 4]. The rear marker shows where 4 was just written." data-book-key="en/chapter_stack_and_queue/deque.assets/array_deque_step2_push_last.png" data-dark-src="/assets/figure-svg/figures/89132f0fb4a25538b4414855.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/e3c6d1b12680683f5297386c.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/array_deque_step2_push_last.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">array_deque_push_last</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/array_deque_step2_push_last.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">array_deque_push_last</object></ha-svg-figure></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/deque.assets/array_deque_step3_push_first.png" data-dark-src="/assets/figure-svg/figures/7b18774a056ffea29ec222ae.json" data-src="/assets/figure-svg/figures/0eb08ddc76f061bdf9b57e27.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/deque.assets/array_deque_step3_push_first.svg" data-theme="light" tabindex="-1" type="image/svg+xml">array_deque_push_first</object><object data="/en/chapter_stack_and_queue/deque.assets/array_deque_step3_push_first.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">array_deque_push_first</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Implementing a deque with a circular array, step 3 of 5: Add 1 at the front" class="animation-figure" data-a11y-forward="Move front up one slot, write 1, and increase size to 5. The other elements stay in place." data-a11y-state="The index front points to 1, size = 5, and the deque is [1, 3, 2, 5, 4]." data-book-key="en/chapter_stack_and_queue/deque.assets/array_deque_step3_push_first.png" data-dark-src="/assets/figure-svg/figures/7b18774a056ffea29ec222ae.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/0eb08ddc76f061bdf9b57e27.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/array_deque_step3_push_first.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">array_deque_push_first</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/array_deque_step3_push_first.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">array_deque_push_first</object></ha-svg-figure></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/deque.assets/array_deque_step4_pop_last.png" data-dark-src="/assets/figure-svg/figures/ae438ca6722235f22f0d5ca3.json" data-src="/assets/figure-svg/figures/8de476dd3aaa1e678a2f22f1.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/deque.assets/array_deque_step4_pop_last.svg" data-theme="light" tabindex="-1" type="image/svg+xml">array_deque_pop_last</object><object data="/en/chapter_stack_and_queue/deque.assets/array_deque_step4_pop_last.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">array_deque_pop_last</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Implementing a deque with a circular array, step 4 of 5: Remove 4 from the rear" class="animation-figure" data-a11y-forward="Read and return the rear element, 4. Decrease size to 4. The former rear slot is outside the active range." data-a11y-state="The index front points to 1, size = 4, and the deque is [1, 3, 2, 5]. Returned 4." data-book-key="en/chapter_stack_and_queue/deque.assets/array_deque_step4_pop_last.png" data-dark-src="/assets/figure-svg/figures/ae438ca6722235f22f0d5ca3.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/8de476dd3aaa1e678a2f22f1.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/array_deque_step4_pop_last.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">array_deque_pop_last</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/array_deque_step4_pop_last.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">array_deque_pop_last</object></ha-svg-figure></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/deque.assets/array_deque_step5_pop_first.png" data-dark-src="/assets/figure-svg/figures/e4d462f20b4cfbf584350f8e.json" data-src="/assets/figure-svg/figures/a87677071e57ea825e813bbb.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/deque.assets/array_deque_step5_pop_first.svg" data-theme="light" tabindex="-1" type="image/svg+xml">array_deque_pop_first</object><object data="/en/chapter_stack_and_queue/deque.assets/array_deque_step5_pop_first.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">array_deque_pop_first</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Implementing a deque with a circular array, step 5 of 5: Remove 1 from the front" class="animation-figure" data-a11y-forward="Read and return the front element, 1. Move front down one slot to point to 3 again and decrease size to 3." data-a11y-state="The index front points to 3, size = 3, and the deque is [3, 2, 5]. Returned 1." data-book-key="en/chapter_stack_and_queue/deque.assets/array_deque_step5_pop_first.png" data-dark-src="/assets/figure-svg/figures/e4d462f20b4cfbf584350f8e.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/a87677071e57ea825e813bbb.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/array_deque_step5_pop_first.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">array_deque_pop_first</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/deque.assets/array_deque_step5_pop_first.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">array_deque_pop_first</object></ha-svg-figure></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p align="center"> Figure 5-9 Insertion and removal in an array-based deque </p>
|
||||
<p align="center"> Figure 5-9 Insertion and removal in an array-based deque </p><div class="ha-sr-only figure-description"><h4>Description of Figure 5-9: Implementing a deque with a circular array</h4><p>Moving the front index and tracking the length allow operations at either end without shifting the elements in between.</p><p>The deque on the left and its underlying circular array on the right both run from front to rear, top to bottom. The index front marks the first element, and size counts the active elements. Moving up one slot decreases the index by 1, equivalent to moving left in the code comments. Moving down one slot increases the index by 1.</p><p>The next insertion at the rear uses rear = (front + size) % capacity. The figure uses the shorter expression rear = front + size. It shows no wraparound and gives neither the capacity nor numeric indices.</p><p>Insertion requires a free slot, and removal requires a nonempty deque. Active elements occupy positions (front + i) % capacity for i from 0 to size - 1. Inserting at the front updates front to (front - 1 + capacity) % capacity.</p><p>The original elements 3, 2, and 5 stay in their slots throughout. A dashed gray cell lies outside the active range. Its dashed outline after removal does not mean the code clears the stored value to zero. The new rear element 4 is cyan, and the new front element 1 is orange.</p><ol><li><p><strong>Initial deque</strong></p><p>The deque initially contains [3, 2, 5]. The index front points to 3, and size = 3. Calculated as front + size, rear points to the first free slot below 5. Free slots and ellipses appear at both ends.</p></li><li><p><strong>Add 4 at the rear</strong></p><p>Call push_last(4). Before insertion, use front and size = 3 to compute rear. Write 4 into that free slot, then increase size to 4. The deque is now [3, 2, 5, 4], and front still points to 3. The arrow labeled rear stays at the newly written 4. Recomputing rear with the new size would put it one slot below 4.</p></li><li><p><strong>Add 1 at the front</strong></p><p>Call push_first(1). Move front up one slot to the free slot above 3, write 1, then increase size from 4 to 5. The deque is now [1, 3, 2, 5, 4]. The index front points to the new first element, 1. Other elements stay in their slots. This frame has no rear marker.</p></li><li><p><strong>Remove 4 from the rear</strong></p><p>Call pop_last() to read and return 4 from the rear. Decrease size from 5 to 4. The deque now contains [1, 3, 2, 5], with front still pointing to 1. The former slot for 4 has a dashed outline because it now lies outside the active range. This frame has no rear marker.</p></li><li><p><strong>Remove 1 from the front</strong></p><p>Call pop_first() to read and return the front element, 1. Move front one slot down to 3 and decrease size from 4 to 3. The deque is back to [3, 2, 5]. The slot that held 1 is outside the active range. This frame has no rear marker.</p></li></ol><p>Insertions and removals at either end change only an end slot, front, or size. They take O(1) time and do not move intermediate elements.</p></div>
|
||||
<p>Starting with the queue implementation, we need only add methods for insertion at the front and removal at the rear:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="5:13"><input checked="checked" id="__tabbed_5_1" name="__tabbed_5" type="radio"/><input id="__tabbed_5_2" name="__tabbed_5" type="radio"/><input id="__tabbed_5_3" name="__tabbed_5" type="radio"/><input id="__tabbed_5_4" name="__tabbed_5" type="radio"/><input id="__tabbed_5_5" name="__tabbed_5" type="radio"/><input id="__tabbed_5_6" name="__tabbed_5" type="radio"/><input id="__tabbed_5_7" name="__tabbed_5" type="radio"/><input id="__tabbed_5_8" name="__tabbed_5" type="radio"/><input id="__tabbed_5_9" name="__tabbed_5" type="radio"/><input id="__tabbed_5_10" name="__tabbed_5" type="radio"/><input id="__tabbed_5_11" name="__tabbed_5" type="radio"/><input id="__tabbed_5_12" name="__tabbed_5" type="radio"/><input id="__tabbed_5_13" name="__tabbed_5" type="radio"/><div class="tabbed-labels"><label for="__tabbed_5_1">Python</label><label for="__tabbed_5_2">C++</label><label for="__tabbed_5_3">Java</label><label for="__tabbed_5_4">C#</label><label for="__tabbed_5_5">Go</label><label for="__tabbed_5_6">Swift</label><label for="__tabbed_5_7">JS</label><label for="__tabbed_5_8">TS</label><label for="__tabbed_5_9">Dart</label><label for="__tabbed_5_10">Rust</label><label for="__tabbed_5_11">C</label><label for="__tabbed_5_12">Kotlin</label><label for="__tabbed_5_13">Ruby</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -5689,5 +5689,5 @@
|
||||
<script src="../../javascripts/mathjax.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js"></script>
|
||||
<script src="../../javascripts/animation_player.js"></script>
|
||||
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/bff17b8259339758cb1c4a06.js"></script></body>
|
||||
<script defer="" src="/assets/figure-svg/js/00174ff861e190f5c1f09e34.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/70ae82c6406c9bdbda8e8362.js"></script></body>
|
||||
</html>
|
||||
Binary file not shown.
Reference in New Issue
Block a user