Release reviewed gh-pages candidate

This commit is contained in:
krahets
2026-11-18 16:34:49 +08:00
parent 8966617032
commit 451a6b692c
822 changed files with 3585 additions and 3541 deletions

View File

@@ -1995,8 +1995,8 @@
<p>A <u>stack</u> is a linear data structure that follows a last-in, first-out rule.</p>
<p>Think of a stack of plates on a table. If we can move only one plate at a time, reaching the bottom plate requires removing all the plates above it first. Replace the plates with elements such as integers, characters, or objects, and we have a stack.</p>
<p>As shown below, the upper end is the “top” of the stack and the lower end is the “bottom.” Adding an element to the top is called “pushing,” and removing the top element is called “popping.”</p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_stack_and_queue/stack.assets/stack_operations.png" data-dark-src="/assets/figure-svg/figures/39bb0c5f80c1125d9bad7118.json" data-src="/assets/figure-svg/figures/c206500eda3aac91f969b02e.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/stack.assets/stack_operations.svg" data-theme="light" tabindex="-1" type="image/svg+xml">The last-in, first-out rule of a stack</object><object data="/en/chapter_stack_and_queue/stack.assets/stack_operations.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">The last-in, first-out rule of a stack</object></ha-svg-figure></p>
<p align="center"> Figure 5-1   The last-in, first-out rule of a stack </p>
<p><ha-svg-figure aria-label="Push and pop operations on a stack" class="animation-figure" data-book-key="en/chapter_stack_and_queue/stack.assets/stack_operations.png" data-dark-src="/assets/figure-svg/figures/39bb0c5f80c1125d9bad7118.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/c206500eda3aac91f969b02e.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/stack_operations.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">The last-in, first-out rule of a stack</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/stack_operations.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">The last-in, first-out rule of a stack</object></ha-svg-figure></p>
<p align="center"> Figure 5-1   The last-in, first-out rule of a stack </p><div class="ha-sr-only figure-description"><h2>Description of Figure 5-1: Push and pop operations on a stack</h2><p>Pushing and popping at the same top end demonstrates first-in, last-out (that is, last-in, first-out) order.</p><p>All sequences run from bottom to top. The push operation adds an element at the top, and pop removes and returns the top element.</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 open upper end is the top and the closed lower end is the bottom; push arrows enter from above, and pop arrows exit from above.</p><p>Dashed boxes only indicate elements' previous positions before an operation and are not counted again in the current stack; a solid box moved outside the container represents the return value of the current pop.</p><table><caption>Stack contents after each operation</caption><thead><tr><th scope="col">Operation</th><th scope="col">Active sequence (bottom to top)</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>[1, 3, 2, 5]</td><td>4</td></tr><tr><th scope="row">Second pop()</th><td>[1, 3, 2]</td><td>5</td></tr></tbody></table><p>5 is pushed first, and 4 is then pushed on top of 5. Because only the top element can be popped each time, 4 is returned first and 5 is returned next, demonstrating first-in, last-out—that is, last-in, first-out—order.</p></div>
<h2 id="511-common-stack-operations">5.1.1   Common stack operations<a class="headerlink" href="#511-common-stack-operations" title="Permanent link"></a></h2>
<p>Table 5-1 lists common operations. Actual method names depend on the programming language; here we use the familiar names <code>push()</code>, <code>pop()</code>, and <code>peek()</code>.</p>
<p align="center"> Table 5-1   Efficiency of stack operations </p>
@@ -2345,20 +2345,20 @@
<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>When implementing a stack with a linked list, treat the head as the top and the tail as the bottom.</p>
<p>As shown below, pushing simply inserts a node at the head, an operation called “head insertion.” Popping removes the head node.</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">&lt;1&gt;</label><label for="__tabbed_2_2">&lt;2&gt;</label><label for="__tabbed_2_3">&lt;3&gt;</label></div>
<div aria-label="Push and pop in a linked-list stack" class="tabbed-set tabbed-alternate" data-figure-sequence="" data-tabs="2:3" role="group"><input aria-label="Step 1: Initial stack: head node 5 is the top" checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio"/><input aria-label="Step 2: Push 4 at the head of the linked list" id="__tabbed_2_2" name="__tabbed_2" type="radio"/><input aria-label="Step 3: Pop 4 from the head of the linked list" id="__tabbed_2_3" name="__tabbed_2" type="radio"/><div class="tabbed-labels"><label for="__tabbed_2_1">&lt;1&gt;</label><label for="__tabbed_2_2">&lt;2&gt;</label><label for="__tabbed_2_3">&lt;3&gt;</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/stack.assets/linkedlist_stack_step1.png" data-dark-src="/assets/figure-svg/figures/20d2543cee957838bfd40c9c.json" data-src="/assets/figure-svg/figures/57b5e166884734495fc3440b.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step1.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Push and pop in a linked-list stack</object><object data="/en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step1.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Push and pop in a linked-list stack</object></ha-svg-figure></p>
<p><ha-svg-figure aria-label="Push and pop in a linked-list stack, step 1 of 3: Initial stack: head node 5 is the top" class="animation-figure" data-a11y-forward="The initial stack has 4 elements, with linked-list head node 5 at the top and tail node 1 at the bottom." data-a11y-state="Initial stack (bottom to top) [1, 3, 2, 5]; linked list (head to tail) 5 → 2 → 3 → 1, with head node 5 as the top." data-book-key="en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step1.png" data-dark-src="/assets/figure-svg/figures/20d2543cee957838bfd40c9c.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/57b5e166884734495fc3440b.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step1.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Push and pop in a linked-list stack</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step1.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Push and pop in a linked-list stack</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/stack.assets/linkedlist_stack_step2_push.png" data-dark-src="/assets/figure-svg/figures/477d9e7fff2c9fe4fede0d11.json" data-src="/assets/figure-svg/figures/72cf1e7995239daf41e02d5f.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step2_push.svg" data-theme="light" tabindex="-1" type="image/svg+xml">linkedlist_stack_push</object><object data="/en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step2_push.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">linkedlist_stack_push</object></ha-svg-figure></p>
<p><ha-svg-figure aria-label="Push and pop in a linked-list stack, step 2 of 3: Push 4 at the head of the linked list" class="animation-figure" data-a11y-forward="Call push(4) to insert node 4 at the head of the linked list; 4 becomes the new head node and stack top." data-a11y-state="After push(4): stack (bottom to top) [1, 3, 2, 5, 4]; linked list (head to tail) 4 → 5 → 2 → 3 → 1, with new head node 4 as the top." data-book-key="en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step2_push.png" data-dark-src="/assets/figure-svg/figures/477d9e7fff2c9fe4fede0d11.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/72cf1e7995239daf41e02d5f.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step2_push.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">linkedlist_stack_push</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step2_push.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">linkedlist_stack_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/stack.assets/linkedlist_stack_step3_pop.png" data-dark-src="/assets/figure-svg/figures/78062321f229a18c552808ad.json" data-src="/assets/figure-svg/figures/f14bc0b8ed8e687c8d2082dd.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step3_pop.svg" data-theme="light" tabindex="-1" type="image/svg+xml">linkedlist_stack_pop</object><object data="/en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step3_pop.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">linkedlist_stack_pop</object></ha-svg-figure></p>
<p><ha-svg-figure aria-label="Push and pop in a linked-list stack, step 3 of 3: Pop 4 from the head of the linked list" class="animation-figure" data-a11y-forward="Call pop() to remove and return linked-list head node 4, restoring the head node and stack top to 5." data-a11y-state="After pop() removes and returns head node 4: stack (bottom to top) [1, 3, 2, 5]; linked list (head to tail) 5 → 2 → 3 → 1, with stack top restored to 5." data-book-key="en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step3_pop.png" data-dark-src="/assets/figure-svg/figures/78062321f229a18c552808ad.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/f14bc0b8ed8e687c8d2082dd.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step3_pop.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">linkedlist_stack_pop</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/linkedlist_stack_step3_pop.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">linkedlist_stack_pop</object></ha-svg-figure></p>
</div>
</div>
</div>
<p align="center"> Figure 5-2   Push and pop in a linked-list stack </p>
<p align="center"> Figure 5-2   Push and pop in a linked-list stack </p><div class="ha-sr-only figure-description"><h4>Description of Figure 5-2: Push and pop in a linked-list stack</h4><p>Treat the head node of a singly linked list as the top of the stack so both push and pop operate at the head.</p><p>On the left is the external stack view, with the top above and the bottom below; on the right is a vertical singly linked list with the head node at the top, the tail node at the bottom, and next arrows pointing downward. A leftward arrow between them is labeled "Implemented with a linked list. Exposes stack operations".</p><p>The stack top corresponds to the linked list's head node, and the stack bottom corresponds to the tail node. Consequently, the stack order from bottom to top is the reverse of the linked-list order from head to tail. Beside the open top of the stack on the left, the push arrow enters downward and the pop arrow exits upward.</p><ol><li><p><strong>Initial stack: head node 5 is the top</strong></p><p>From bottom to top, the initial stack is [1, 3, 2, 5] with 4 elements; from head to tail, the linked list is 5 → 2 → 3 → 1. Head node 5 corresponds to the stack top, and tail node 1 corresponds to the stack bottom.</p></li><li><p><strong>Push 4 at the head of the linked list</strong></p><p>Call push(4), labeled on the right "Add node 4 to the head of the linked list". A cyan new node 4 is inserted above the previous head node 5, with its next pointer pointing to 5, and becomes the new head node. From head to tail, the linked list is now 4 → 5 → 2 → 3 → 1, and from bottom to top, the stack is [1, 3, 2, 5, 4] with 5 elements; the head node and stack top are 4, while the tail node and stack bottom remain 1.</p></li><li><p><strong>Pop 4 from the head of the linked list</strong></p><p>Call pop(), labeled on the right "Delete the head node". Remove and return the previous head node 4, making its successor 5 the new head node. From head to tail, the linked list returns to 5 → 2 → 3 → 1, and from bottom to top, the stack returns to [1, 3, 2, 5] with 4 elements; the head node and stack top are 5, while the tail node and stack bottom remain 1.</p></li></ol><p>In a linked-list stack, both pushing and popping add or remove nodes solely at the head of the linked list, each taking 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">
@@ -3116,20 +3116,20 @@
</details>
<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>For an array-based stack, use the end of the array as the top. As shown below, pushing and popping correspond to adding and removing the last element, both taking <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">&lt;1&gt;</label><label for="__tabbed_4_2">&lt;2&gt;</label><label for="__tabbed_4_3">&lt;3&gt;</label></div>
<div aria-label="Push and pop in an array-based stack" class="tabbed-set tabbed-alternate" data-figure-sequence="" data-tabs="4:3" role="group"><input aria-label="Step 1: Initial stack: last array element 5 is the top" checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio"/><input aria-label="Step 2: Push 4 at the end of the array" id="__tabbed_4_2" name="__tabbed_4" type="radio"/><input aria-label="Step 3: Pop 4 from the end of the array" id="__tabbed_4_3" name="__tabbed_4" type="radio"/><div class="tabbed-labels"><label for="__tabbed_4_1">&lt;1&gt;</label><label for="__tabbed_4_2">&lt;2&gt;</label><label for="__tabbed_4_3">&lt;3&gt;</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/stack.assets/array_stack_step1.png" data-dark-src="/assets/figure-svg/figures/e3275dc35a28e6e7e81d95f2.json" data-src="/assets/figure-svg/figures/ba15be652f75fd6726de0cc9.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/stack.assets/array_stack_step1.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Push and pop in an array-based stack</object><object data="/en/chapter_stack_and_queue/stack.assets/array_stack_step1.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Push and pop in an array-based stack</object></ha-svg-figure></p>
<p><ha-svg-figure aria-label="Push and pop in an array-based stack, step 1 of 3: Initial stack: last array element 5 is the top" class="animation-figure" data-a11y-forward="The initial array holds 4 active elements [1, 3, 2, 5], with last element 5 as the stack top." data-a11y-state="Initial occupied array (first to last) [1, 3, 2, 5]; first element 1 is the bottom, last element 5 is the top, with unused slots above." data-book-key="en/chapter_stack_and_queue/stack.assets/array_stack_step1.png" data-dark-src="/assets/figure-svg/figures/e3275dc35a28e6e7e81d95f2.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/ba15be652f75fd6726de0cc9.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/array_stack_step1.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Push and pop in an array-based stack</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/array_stack_step1.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Push and pop in an array-based stack</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/stack.assets/array_stack_step2_push.png" data-dark-src="/assets/figure-svg/figures/be1a43acf7a7039efbf4f5b4.json" data-src="/assets/figure-svg/figures/ae2317d399294ba86246a2a0.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/stack.assets/array_stack_step2_push.svg" data-theme="light" tabindex="-1" type="image/svg+xml">array_stack_push</object><object data="/en/chapter_stack_and_queue/stack.assets/array_stack_step2_push.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">array_stack_push</object></ha-svg-figure></p>
<p><ha-svg-figure aria-label="Push and pop in an array-based stack, step 2 of 3: Push 4 at the end of the array" class="animation-figure" data-a11y-forward="Call push(4) to write 4 into the unused slot at the end of the array; the last element and stack top become 4." data-a11y-state="After push(4): occupied array (first to last) [1, 3, 2, 5, 4]; last element and stack top updated to 4." data-book-key="en/chapter_stack_and_queue/stack.assets/array_stack_step2_push.png" data-dark-src="/assets/figure-svg/figures/be1a43acf7a7039efbf4f5b4.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/ae2317d399294ba86246a2a0.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/array_stack_step2_push.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">array_stack_push</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/array_stack_step2_push.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">array_stack_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/stack.assets/array_stack_step3_pop.png" data-dark-src="/assets/figure-svg/figures/4ea0d63b8b230aa269d2c909.json" data-src="/assets/figure-svg/figures/a9081c735e6751cded6cf8ba.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_stack_and_queue/stack.assets/array_stack_step3_pop.svg" data-theme="light" tabindex="-1" type="image/svg+xml">array_stack_pop</object><object data="/en/chapter_stack_and_queue/stack.assets/array_stack_step3_pop.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">array_stack_pop</object></ha-svg-figure></p>
<p><ha-svg-figure aria-label="Push and pop in an array-based stack, step 3 of 3: Pop 4 from the end of the array" class="animation-figure" data-a11y-forward="Call pop() to remove and return last array element 4, restoring the last element and stack top to 5." data-a11y-state="After pop() removes and returns last element 4: occupied array (first to last) [1, 3, 2, 5]; last element and stack top restored to 5." data-book-key="en/chapter_stack_and_queue/stack.assets/array_stack_step3_pop.png" data-dark-src="/assets/figure-svg/figures/4ea0d63b8b230aa269d2c909.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/a9081c735e6751cded6cf8ba.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/array_stack_step3_pop.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">array_stack_pop</object><object aria-hidden="true" data="/en/chapter_stack_and_queue/stack.assets/array_stack_step3_pop.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">array_stack_pop</object></ha-svg-figure></p>
</div>
</div>
</div>
<p align="center"> Figure 5-3   Push and pop in an array-based stack </p>
<p align="center"> Figure 5-3   Push and pop in an array-based stack </p><div class="ha-sr-only figure-description"><h4>Description of Figure 5-3: Push and pop in an array-based stack</h4><p>Treat the end of the array as the top of the stack so both push and pop operate at the end.</p><p>On the left is the external stack view, with the top above and the bottom below; on the right is a vertical array with the first element at the bottom and the last element at the top. A leftward arrow between them is labeled "Implemented with an array. Exposes stack operations".</p><p>The array order from first to last element matches the stack order from bottom to top, both read upward in the figure. Gray dashed cells and an ellipsis above the last element are labeled "Unused array slots".</p><ol><li><p><strong>Initial stack: last array element 5 is the top</strong></p><p>From first to last element, the initial occupied array is [1, 3, 2, 5], matching the stack from bottom to top with 4 elements in total. First element 1 corresponds to the bottom, and last element 5 corresponds to the top; above 5 are two gray dashed unused slots and an ellipsis.</p></li><li><p><strong>Push 4 at the end of the array</strong></p><p>Call push(4), labeled on the right "Add element 4 to the end of the array". A cyan 4 is written into the first unused slot above the previous last element 5, and the "Last element" label moves up to 4. From first to last element, the occupied array is now [1, 3, 2, 5, 4], matching the stack from bottom to top with 5 elements; the first element and stack bottom remain 1, the last element and stack top are 4, and unused slots remain above.</p></li><li><p><strong>Pop 4 from the end of the array</strong></p><p>Call pop(), labeled on the right "Delete the last array element". Remove and return last element 4; its former cell reverts to a gray dashed unused slot, and the "Last element" label moves down to 5. From first to last element, the occupied array returns to [1, 3, 2, 5], matching the stack from bottom to top with 4 elements; the first element and stack bottom are 1, and the last element and stack top are 5.</p></li></ol><p>In an array-based stack, pushing and popping correspond to appending and deleting at the end of the array; when no resizing is triggered, both take O(1) time without shifting existing elements.</p></div>
<p>Since elements may keep arriving, use a dynamic array so that resizing is handled for us. An implementation follows:</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">
@@ -3875,5 +3875,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>