mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-23 04:02:44 +08:00
Release reviewed gh-pages candidate
This commit is contained in:
@@ -1976,8 +1976,8 @@
|
||||
<h1 id="52-queues">5.2 Queues<a class="headerlink" href="#52-queues" title="Permanent link">¶</a></h1>
|
||||
<p>A <u>queue</u> is a linear data structure that follows a first-in, first-out rule. It models a line of people: newcomers join at the back, while those at the front leave one by one.</p>
|
||||
<p>As shown below, the two ends are called the “front” and the “rear.” Adding an element at the rear is called “enqueueing,” and removing an element from the front is called “dequeueing.”</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/queue.assets/queue_operations.png" data-dark-src="/assets/figure-svg/figures/09f48f1479ff9dacd5d5d306.json" data-src="/assets/figure-svg/figures/af7b39ddc06c51a055a4276b.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/queue.assets/queue_operations.svg" data-theme="light" tabindex="-1" type="image/svg+xml">The first-in, first-out rule of a queue</object><object data="/en/chapter_stack_and_queue/queue.assets/queue_operations.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">The first-in, first-out rule of a queue</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 5-4 The first-in, first-out rule of a queue </p>
|
||||
<p><ha-svg-figure aria-label="Enqueue and dequeue operations on a queue" class="animation-figure" data-book-key="en/chapter_stack_and_queue/queue.assets/queue_operations.png" data-dark-src="/assets/figure-svg/figures/09f48f1479ff9dacd5d5d306.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/af7b39ddc06c51a055a4276b.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/queue_operations.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">The first-in, first-out rule of a queue</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/queue_operations.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">The first-in, first-out rule of a queue</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 5-4 The first-in, first-out rule of a queue </p><div class="ha-sr-only figure-description"><h2>Description of Figure 5-4: Enqueue and dequeue operations on a queue</h2><p>Elements join at the rear and leave from the front, following the first-in, first-out rule.</p><p>All sequences are written from front to rear. In the figure, push enqueues an element at the rear, and pop dequeues and returns the element at the front.</p><p>Five vertical panels linked from left to right by arrows correspond in order to the five rows of the table below. In each panel, the upper end is the front and the lower end is the rear; enqueue arrows enter from below, and dequeue arrows exit from above.</p><p>Dashed boxes only indicate elements' previous positions before an operation and are not counted again in the current queue; a solid box moved outside the container represents the return value of the current pop.</p><table><caption>Queue contents after each operation</caption><thead><tr><th scope="col">Operation</th><th scope="col">Active sequence (front to rear)</th><th scope="col">Returned value</th></tr></thead><tbody><tr><th scope="row">Initial</th><td>[1, 3, 2]</td><td>None</td></tr><tr><th scope="row">push(5)</th><td>[1, 3, 2, 5]</td><td>None</td></tr><tr><th scope="row">push(4)</th><td>[1, 3, 2, 5, 4]</td><td>None</td></tr><tr><th scope="row">First pop()</th><td>[3, 2, 5, 4]</td><td>1</td></tr><tr><th scope="row">Second pop()</th><td>[2, 5, 4]</td><td>3</td></tr></tbody></table><p>New elements can only join at the rear, leaving existing elements in their original order. Thus, after 5 and 4 are added, consecutive dequeues still remove the original front element 1 first and 3 next, demonstrating first-in, first-out order.</p></div>
|
||||
<h2 id="521-common-queue-operations">5.2.1 Common queue operations<a class="headerlink" href="#521-common-queue-operations" title="Permanent link">¶</a></h2>
|
||||
<p>Table 5-2 lists common operations. Method names vary between programming languages; here we use the same names as for stacks.</p>
|
||||
<p align="center"> Table 5-2 Efficiency of queue operations </p>
|
||||
@@ -2333,20 +2333,20 @@
|
||||
<p>A queue needs a data structure that allows insertion at one end and deletion at the other. Both linked lists and arrays can meet this requirement.</p>
|
||||
<h3 id="1-implementation-with-a-linked-list">1. Implementation with a linked list<a class="headerlink" href="#1-implementation-with-a-linked-list" title="Permanent link">¶</a></h3>
|
||||
<p>As shown below, treat the linked list's head and tail as the queue's front and rear. Nodes may be added only at the rear and removed only from the front.</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-figure-sequence="" data-tabs="2:3"><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"/><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></div>
|
||||
<div aria-label="Enqueue and dequeue in a linked-list queue" class="tabbed-set tabbed-alternate" data-figure-sequence="" data-tabs="2:3" role="group"><input aria-label="Step 1: Initial queue: remove at the head, add at the tail" checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio"/><input aria-label="Step 2: Enqueue 4 at the tail of the list" id="__tabbed_2_2" name="__tabbed_2" type="radio"/><input aria-label="Step 3: Dequeue 1 from the head of the list" id="__tabbed_2_3" 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></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step1.png" data-dark-src="/assets/figure-svg/figures/604b279371c057d551bff176.json" data-src="/assets/figure-svg/figures/ba21cb8f70ec2bf0e2cf65e4.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step1.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Enqueue and dequeue in a linked-list queue</object><object data="/en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step1.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Enqueue and dequeue in a linked-list queue</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Enqueue and dequeue in a linked-list queue, step 1 of 3: Initial queue: remove at the head, add at the tail" class="animation-figure" data-a11y-forward="The initial queue has 4 elements, [1, 3, 2, 5]. The head, 1, is at the front and the tail, 5, is at the rear." data-a11y-state="Initial queue from front to rear: [1, 3, 2, 5]. List from head to tail: 1 → 3 → 2 → 5. Head 1 is the front and tail 5 is the rear." data-book-key="en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step1.png" data-dark-src="/assets/figure-svg/figures/604b279371c057d551bff176.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/ba21cb8f70ec2bf0e2cf65e4.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step1.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Enqueue and dequeue in a linked-list queue</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step1.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Enqueue and dequeue in a linked-list queue</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/queue.assets/linkedlist_queue_step2_push.png" data-dark-src="/assets/figure-svg/figures/dc6fea6e278dbd6b9a66d308.json" data-src="/assets/figure-svg/figures/c5931f85fac7fef7a10e71a2.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step2_push.svg" data-theme="light" tabindex="-1" type="image/svg+xml">linkedlist_queue_push</object><object data="/en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step2_push.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">linkedlist_queue_push</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Enqueue and dequeue in a linked-list queue, step 2 of 3: Enqueue 4 at the tail of the list" class="animation-figure" data-a11y-forward="Call push(4) to attach node 4 at the tail. Both the tail and the rear now refer to 4." data-a11y-state="After push(4), the queue is [1, 3, 2, 5, 4] and the list is 1 → 3 → 2 → 5 → 4. The front remains 1 and the new tail, 4, is the rear." data-book-key="en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step2_push.png" data-dark-src="/assets/figure-svg/figures/dc6fea6e278dbd6b9a66d308.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/c5931f85fac7fef7a10e71a2.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step2_push.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">linkedlist_queue_push</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step2_push.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">linkedlist_queue_push</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/queue.assets/linkedlist_queue_step3_pop.png" data-dark-src="/assets/figure-svg/figures/0a4aba013643640a36e6af3a.json" data-src="/assets/figure-svg/figures/a77e6f6ba27d7f56111906c6.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step3_pop.svg" data-theme="light" tabindex="-1" type="image/svg+xml">linkedlist_queue_pop</object><object data="/en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step3_pop.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">linkedlist_queue_pop</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Enqueue and dequeue in a linked-list queue, step 3 of 3: Dequeue 1 from the head of the list" class="animation-figure" data-a11y-forward="Call pop() to remove head 1. Node 3 becomes the new head and front, leaving queue [3, 2, 5, 4]." data-a11y-state="After pop() removes head 1, the queue is [3, 2, 5, 4] and the list is 3 → 2 → 5 → 4. The new front is head 3, and the tail remains 4." data-book-key="en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step3_pop.png" data-dark-src="/assets/figure-svg/figures/0a4aba013643640a36e6af3a.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/a77e6f6ba27d7f56111906c6.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step3_pop.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">linkedlist_queue_pop</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/linkedlist_queue_step3_pop.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">linkedlist_queue_pop</object></ha-svg-figure></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p align="center"> Figure 5-5 Enqueue and dequeue in a linked-list queue </p>
|
||||
<p align="center"> Figure 5-5 Enqueue and dequeue in a linked-list queue </p><div class="ha-sr-only figure-description"><h4>Description of Figure 5-5: Enqueue and dequeue in a linked-list queue</h4><p>In a singly linked list, the head serves as the front and the tail as the rear. New elements enter at the tail and leave at the head.</p><p>The queue on the left and the vertical singly linked list on the right are linked by the label “Implemented with a linked list. Exposes queue operations”. Both run from front to rear, top to bottom, with next arrows pointing down. The head is the front and the tail is the rear.</p><p>Nodes are added only at the tail and removed only from the head. In the initial frame, the arrow at the front points up and out of the queue. The arrow at the rear points up into it from below.</p><ol><li><p><strong>Initial queue: remove at the head, add at the tail</strong></p><p>The initial list is 1 → 3 → 2 → 5, corresponding to the 4-element queue [1, 3, 2, 5]. Node 1 is the head and front. Node 5 is the tail and rear.</p></li><li><p><strong>Enqueue 4 at the tail of the list</strong></p><p>Call push(4). The note on the right reads “Add node 4 to the tail of the linked list”. Attach the new cyan node 4 below the former tail 5 and move the tail marker down to 4. The list now has 5 elements, 1 → 3 → 2 → 5 → 4, in queue order from front to rear. The head and front are still 1. The tail and rear are now 4.</p></li><li><p><strong>Dequeue 1 from the head of the list</strong></p><p>Call pop(). The note on the right reads “Delete the head node”. Remove and return 1, then move the head marker down to its successor, 3. The 4-element list is now 3 → 2 → 5 → 4, in queue order from front to rear. The head and front are 3. The cyan 4 is still the tail and rear.</p></li></ol><p>Keeping references to the head and tail means that adding at the tail and removing from the head update only nearby links. Both operations take O(1) time.</p></div>
|
||||
<p>Here is an implementation using a linked list:</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">
|
||||
@@ -3259,20 +3259,20 @@
|
||||
<li>Dequeue: increase <code>front</code> by 1 and decrease <code>size</code> by 1.</li>
|
||||
</ul>
|
||||
<p>Both operations require only a constant amount of work and take <span class="arithmatex">\(O(1)\)</span> time.</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-figure-sequence="" data-tabs="4:3"><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"/><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></div>
|
||||
<div aria-label="Enqueue and dequeue in an array-based queue" class="tabbed-set tabbed-alternate" data-figure-sequence="" data-tabs="4:3" role="group"><input aria-label="Step 1: Initial queue and its active range" checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio"/><input aria-label="Step 2: Enqueue 4 at rear" id="__tabbed_4_2" name="__tabbed_4" type="radio"/><input aria-label="Step 3: Advance front and dequeue 1" id="__tabbed_4_3" 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></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/queue.assets/array_queue_step1.png" data-dark-src="/assets/figure-svg/figures/91259b3f99a79a6d59fe468c.json" data-src="/assets/figure-svg/figures/b9820711812dd66d22db5b20.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/queue.assets/array_queue_step1.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Enqueue and dequeue in an array-based queue</object><object data="/en/chapter_stack_and_queue/queue.assets/array_queue_step1.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Enqueue and dequeue in an array-based queue</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Enqueue and dequeue in an array-based queue, step 1 of 3: Initial queue and its active range" class="animation-figure" data-a11y-forward="The array-based queue starts with [1, 3, 2, 5], size = 4. The index front points to first element 1 and rear to the free slot after last element 5." data-a11y-state="Initial queue [1, 3, 2, 5], size = 4. The index front points to 1, and rear = front + size points to the free slot below 5." data-book-key="en/chapter_stack_and_queue/queue.assets/array_queue_step1.png" data-dark-src="/assets/figure-svg/figures/91259b3f99a79a6d59fe468c.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/b9820711812dd66d22db5b20.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/array_queue_step1.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Enqueue and dequeue in an array-based queue</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/array_queue_step1.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Enqueue and dequeue in an array-based queue</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/queue.assets/array_queue_step2_push.png" data-dark-src="/assets/figure-svg/figures/c1992f191af980f1ec9bae5c.json" data-src="/assets/figure-svg/figures/422c9fc90bb03309c42fffd3.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/queue.assets/array_queue_step2_push.svg" data-theme="light" tabindex="-1" type="image/svg+xml">array_queue_push</object><object data="/en/chapter_stack_and_queue/queue.assets/array_queue_step2_push.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">array_queue_push</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Enqueue and dequeue in an array-based queue, step 2 of 3: Enqueue 4 at rear" class="animation-figure" data-a11y-forward="Call push(4) to write 4 at rear and increase size to 5. The queue is now [1, 3, 2, 5, 4]." data-a11y-state="After push(4), the queue is [1, 3, 2, 5, 4], size = 5, and front points to 1. The rear marker still points to the 4 just written." data-book-key="en/chapter_stack_and_queue/queue.assets/array_queue_step2_push.png" data-dark-src="/assets/figure-svg/figures/c1992f191af980f1ec9bae5c.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/422c9fc90bb03309c42fffd3.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/array_queue_step2_push.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">array_queue_push</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/array_queue_step2_push.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">array_queue_push</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/queue.assets/array_queue_step3_pop.png" data-dark-src="/assets/figure-svg/figures/930be9778902aa7679d19060.json" data-src="/assets/figure-svg/figures/d9034e031cfaeb01c6156253.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/queue.assets/array_queue_step3_pop.svg" data-theme="light" tabindex="-1" type="image/svg+xml">array_queue_pop</object><object data="/en/chapter_stack_and_queue/queue.assets/array_queue_step3_pop.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">array_queue_pop</object></ha-svg-figure></p>
|
||||
<p><ha-svg-figure aria-label="Enqueue and dequeue in an array-based queue, step 3 of 3: Advance front and dequeue 1" class="animation-figure" data-a11y-forward="Call pop(). The index front moves down to 3 and size decreases to 4. Element 1 leaves the queue, and [3, 2, 5, 4] stay in place." data-a11y-state="Calling pop() returns 1. The index front moves down one slot to 3, size = 4, and the queue is [3, 2, 5, 4]. The former slot of 1 is outside the active range and has not been cleared. This frame has no rear marker." data-book-key="en/chapter_stack_and_queue/queue.assets/array_queue_step3_pop.png" data-dark-src="/assets/figure-svg/figures/930be9778902aa7679d19060.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/d9034e031cfaeb01c6156253.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/array_queue_step3_pop.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">array_queue_pop</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/queue.assets/array_queue_step3_pop.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">array_queue_pop</object></ha-svg-figure></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p align="center"> Figure 5-6 Enqueue and dequeue in an array-based queue </p>
|
||||
<p align="center"> Figure 5-6 Enqueue and dequeue in an array-based queue </p><div class="ha-sr-only figure-description"><h4>Description of Figure 5-6: Enqueue and dequeue in an array-based queue</h4><p>The index front identifies the first element, and size counts the elements. Enqueue writes just past the rear. Dequeue advances the front index. All other elements stay in place.</p><p>The queue on the left and vertical array on the right are linked by the label “Implemented with an array. Exposes queue operations”. Both show the front at the top and the rear at the bottom. Moving down increases the index, equivalent to moving right in the chapter’s horizontal array.</p><p>These three frames never wrap past the end of the array. The next free slot after the rear is rear = front + size, and the active interval is [front, rear - 1]. Dashed gray slots and ellipses at each end represent the remaining space. Leaving the active interval does not clear a slot's value to zero.</p><ol><li><p><strong>Initial queue and its active range</strong></p><p>Initially the queue is [1, 3, 2, 5], with size = 4. The orange front points to 1. The cyan rear, calculated as front + 4, points to the first free slot below 5. A note explains that front marks the first element and rear is 1 past the last.</p></li><li><p><strong>Enqueue 4 at rear</strong></p><p>Call push(4). The callout lists three steps: compute rear, add the element at rear, and increase size by 1. Using the old size = 4 locates the free slot below 5. Write the cyan 4 there, then increase size to 5. The queue is now [1, 3, 2, 5, 4], and front still points to 1. The rear arrow remains at the newly written 4. Recomputing rear with the new size would move it to the empty slot below 4.</p></li><li><p><strong>Advance front and dequeue 1</strong></p><p>Call pop() to remove and return the former front element, 1. The callout says to increase front by 1 and decrease size by 1. The index front moves one slot down to 3, and size drops from 5 to 4. The remaining elements [3, 2, 5, 4] stay in their original slots. A dashed outline marks the former slot of 1 as outside the active range. There is no rear marker in this frame.</p></li></ol><p>Enqueue writes at rear and increases size by 1. Dequeue increases front by 1 and decreases size by 1. Neither operation shifts the remaining elements, so both take O(1) time.</p></div>
|
||||
<p>There is a problem: repeated enqueueing and dequeueing move both <code>front</code> and <code>rear</code> to the right, <strong>and they cannot continue once they reach the end of the array</strong>. To solve this, treat the array as circular, with its end connected to its beginning.</p>
|
||||
<p>When <code>front</code> or <code>rear</code> passes the array's end, wrap it back to the beginning. The remainder operation implements this cyclic behavior:</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>
|
||||
@@ -4328,5 +4328,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