mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-13 15:56:05 +00:00
deploy
This commit is contained in:
@@ -1620,9 +1620,9 @@
|
||||
|
||||
<!-- Page content -->
|
||||
<h1 id="51-stack">5.1 Stack<a class="headerlink" href="#51-stack" title="Permanent link">¶</a></h1>
|
||||
<p>"Stack" is a linear data structure that follows the principle of Last-In-First-Out (LIFO).</p>
|
||||
<p>We can compare a stack to a pile of plates on a table. To access the bottom plate, one must remove the plates on top. If we replace the plates with various types of elements (such as integers, characters, objects, etc.), we obtain the data structure known as a stack.</p>
|
||||
<p>As shown in the following figure, we refer to the top of the pile of elements as the "top of the stack" and the bottom as the "bottom of the stack." The operation of adding elements to the top of the stack is called "push," and the operation of removing the top element is called "pop."</p>
|
||||
<p>A "Stack" is a linear data structure that follows the principle of Last-In-First-Out (LIFO).</p>
|
||||
<p>We can compare a stack to a pile of plates on a table. To access the bottom plate, one must first remove the plates on top. By replacing the plates with various types of elements (such as integers, characters, objects, etc.), we obtain the data structure known as a stack.</p>
|
||||
<p>As shown in the Figure 5-1 , we refer to the top of the pile of elements as the "top of the stack" and the bottom as the "bottom of the stack." The operation of adding elements to the top of the stack is called "push," and the operation of removing the top element is called "pop."</p>
|
||||
<p><a class="glightbox" href="../stack.assets/stack_operations.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Stack's Last-In-First-Out Rule" class="animation-figure" src="../stack.assets/stack_operations.png" /></a></p>
|
||||
<p align="center"> Figure 5-1 Stack's Last-In-First-Out Rule </p>
|
||||
|
||||
@@ -1925,8 +1925,8 @@
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%A0%88%0A%20%20%20%20%23%20Python%20%E6%B2%A1%E6%9C%89%E5%86%85%E7%BD%AE%E7%9A%84%E6%A0%88%E7%B1%BB%EF%BC%8C%E5%8F%AF%E4%BB%A5%E6%8A%8A%20list%20%E5%BD%93%E4%BD%9C%E6%A0%88%E6%9D%A5%E4%BD%BF%E7%94%A8%0A%20%20%20%20stack%20%3D%20%5B%5D%0A%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%85%A5%E6%A0%88%0A%20%20%20%20stack.append%281%29%0A%20%20%20%20stack.append%283%29%0A%20%20%20%20stack.append%282%29%0A%20%20%20%20stack.append%285%29%0A%20%20%20%20stack.append%284%29%0A%20%20%20%20print%28%22%E6%A0%88%20stack%20%3D%22,%20stack%29%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E6%A0%88%E9%A1%B6%E5%85%83%E7%B4%A0%0A%20%20%20%20peek%20%3D%20stack%5B-1%5D%0A%20%20%20%20print%28%22%E6%A0%88%E9%A1%B6%E5%85%83%E7%B4%A0%20peek%20%3D%22,%20peek%29%0A%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%87%BA%E6%A0%88%0A%20%20%20%20pop%20%3D%20stack.pop%28%29%0A%20%20%20%20print%28%22%E5%87%BA%E6%A0%88%E5%85%83%E7%B4%A0%20pop%20%3D%22,%20pop%29%0A%20%20%20%20print%28%22%E5%87%BA%E6%A0%88%E5%90%8E%20stack%20%3D%22,%20stack%29%0A%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E6%A0%88%E7%9A%84%E9%95%BF%E5%BA%A6%0A%20%20%20%20size%20%3D%20len%28stack%29%0A%20%20%20%20print%28%22%E6%A0%88%E7%9A%84%E9%95%BF%E5%BA%A6%20size%20%3D%22,%20size%29%0A%0A%20%20%20%20%23%20%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%0A%20%20%20%20is_empty%20%3D%20len%28stack%29%20%3D%3D%200%0A%20%20%20%20print%28%22%E6%A0%88%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%20%3D%22,%20is_empty%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=2&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen ></a></div></p>
|
||||
</details>
|
||||
<h2 id="512-implementing-a-stack">5.1.2 Implementing a Stack<a class="headerlink" href="#512-implementing-a-stack" title="Permanent link">¶</a></h2>
|
||||
<p>To understand the mechanics of a stack more deeply, let's try implementing a stack class ourselves.</p>
|
||||
<p>A stack follows the principle of Last-In-First-Out, which means we can only add or remove elements at the top of the stack. However, both arrays and linked lists allow adding and removing elements at any position, <strong>therefore a stack can be seen as a restricted array or linked list</strong>. In other words, we can "mask" some unrelated operations of arrays or linked lists to make their logic conform to the characteristics of a stack.</p>
|
||||
<p>To gain a deeper understanding of how a stack operates, let's try implementing a stack class ourselves.</p>
|
||||
<p>A stack follows the principle of Last-In-First-Out, which means we can only add or remove elements at the top of the stack. However, both arrays and linked lists allow adding and removing elements at any position, <strong>therefore a stack can be seen as a restricted array or linked list</strong>. In other words, we can "shield" certain irrelevant operations of an array or linked list, aligning their external behavior with the characteristics of a stack.</p>
|
||||
<h3 id="1-implementation-based-on-linked-list">1. Implementation Based on Linked List<a class="headerlink" href="#1-implementation-based-on-linked-list" title="Permanent link">¶</a></h3>
|
||||
<p>When implementing a stack using a linked list, we can consider the head node of the list as the top of the stack and the tail node as the bottom of the stack.</p>
|
||||
<p>As shown in the Figure 5-2 , for the push operation, we simply insert elements at the head of the linked list. This method of node insertion is known as "head insertion." For the pop operation, we just need to remove the head node from the list.</p>
|
||||
@@ -3269,7 +3269,7 @@
|
||||
<p><strong>Supported Operations</strong></p>
|
||||
<p>Both implementations support all the operations defined in a stack. The array implementation additionally supports random access, but this is beyond the scope of a stack definition and is generally not used.</p>
|
||||
<p><strong>Time Efficiency</strong></p>
|
||||
<p>In the array-based implementation, both push and pop operations occur in pre-allocated continuous memory, which has good cache locality and therefore higher efficiency. However, if the push operation exceeds the array capacity, it triggers a resizing mechanism, making the time complexity of that push operation <span class="arithmatex">\(O(n)\)</span>.</p>
|
||||
<p>In the array-based implementation, both push and pop operations occur in pre-allocated contiguous memory, which has good cache locality and therefore higher efficiency. However, if the push operation exceeds the array capacity, it triggers a resizing mechanism, making the time complexity of that push operation <span class="arithmatex">\(O(n)\)</span>.</p>
|
||||
<p>In the linked list implementation, list expansion is very flexible, and there is no efficiency decrease issue as in array expansion. However, the push operation requires initializing a node object and modifying pointers, so its efficiency is relatively lower. If the elements being pushed are already node objects, then the initialization step can be skipped, improving efficiency.</p>
|
||||
<p>Thus, when the elements for push and pop operations are basic data types like <code>int</code> or <code>double</code>, we can draw the following conclusions:</p>
|
||||
<ul>
|
||||
|
||||
@@ -1538,21 +1538,21 @@
|
||||
<h1 id="54-summary">5.4 Summary<a class="headerlink" href="#54-summary" title="Permanent link">¶</a></h1>
|
||||
<h3 id="1-key-review">1. Key Review<a class="headerlink" href="#1-key-review" title="Permanent link">¶</a></h3>
|
||||
<ul>
|
||||
<li>A stack is a data structure that follows the Last-In-First-Out (LIFO) principle and can be implemented using either arrays or linked lists.</li>
|
||||
<li>In terms of time efficiency, the array implementation of a stack has higher average efficiency, but during expansion, the time complexity for a single push operation can degrade to <span class="arithmatex">\(O(n)\)</span>. In contrast, the linked list implementation of a stack offers more stable efficiency.</li>
|
||||
<li>Regarding space efficiency, the array implementation of a stack may lead to some level of space wastage. However, it's important to note that the memory space occupied by nodes in a linked list is generally larger than that for elements in an array.</li>
|
||||
<li>A queue is a data structure that follows the First-In-First-Out (FIFO) principle, and it can also be implemented using either arrays or linked lists. The conclusions regarding time and space efficiency for queues are similar to those for stacks.</li>
|
||||
<li>A double-ended queue is a more flexible type of queue that allows adding and removing elements from both ends.</li>
|
||||
<li>Stack is a data structure that follows the Last-In-First-Out (LIFO) principle and can be implemented using arrays or linked lists.</li>
|
||||
<li>In terms of time efficiency, the array implementation of the stack has a higher average efficiency. However, during expansion, the time complexity for a single push operation can degrade to <span class="arithmatex">\(O(n)\)</span>. In contrast, the linked list implementation of a stack offers more stable efficiency.</li>
|
||||
<li>Regarding space efficiency, the array implementation of the stack may lead to a certain degree of space wastage. However, it's important to note that the memory space occupied by nodes in a linked list is generally larger than that for elements in an array.</li>
|
||||
<li>A queue is a data structure that follows the First-In-First-Out (FIFO) principle, and it can also be implemented using arrays or linked lists. The conclusions regarding time and space efficiency for queues are similar to those for stacks.</li>
|
||||
<li>A double-ended queue (deque) is a more flexible type of queue that allows adding and removing elements at both ends.</li>
|
||||
</ul>
|
||||
<h3 id="2-q-a">2. Q & A<a class="headerlink" href="#2-q-a" title="Permanent link">¶</a></h3>
|
||||
<p><strong>Q</strong>: Is the browser's forward and backward functionality implemented with a doubly linked list?</p>
|
||||
<p>The forward and backward functionality of a browser fundamentally represents the "stack" concept. When a user visits a new page, it is added to the top of the stack; when they click the back button, the page is popped from the top. A double-ended queue can conveniently implement some additional operations, as mentioned in the "Double-Ended Queue" section.</p>
|
||||
<p>A browser's forward and backward navigation is essentially a manifestation of the "stack" concept. When a user visits a new page, the page is added to the top of the stack; when they click the back button, the page is popped from the top of the stack. A double-ended queue (deque) can conveniently implement some additional operations, as mentioned in the "Double-Ended Queue" section.</p>
|
||||
<p><strong>Q</strong>: After popping from a stack, is it necessary to free the memory of the popped node?</p>
|
||||
<p>If the popped node will still be used later, it's not necessary to free its memory. In languages like Java and Python that have automatic garbage collection, manual memory release isn't required; in C and C++, manual memory release is necessary if the node will no longer be used.</p>
|
||||
<p>If the popped node will still be used later, it's not necessary to free its memory. In languages like Java and Python that have automatic garbage collection, manual memory release is not necessary; in C and C++, manual memory release is required.</p>
|
||||
<p><strong>Q</strong>: A double-ended queue seems like two stacks joined together. What are its uses?</p>
|
||||
<p>A double-ended queue is essentially a combination of a stack and a queue, or like two stacks joined together. It exhibits both stack and queue logic, therefore enabling the implementation of all applications of stacks and queues with added flexibility.</p>
|
||||
<p>A double-ended queue, which is a combination of a stack and a queue or two stacks joined together, exhibits both stack and queue logic. Thus, it can implement all applications of stacks and queues while offering more flexibility.</p>
|
||||
<p><strong>Q</strong>: How exactly are undo and redo implemented?</p>
|
||||
<p>Undo and redo are implemented using two stacks: Stack A for undo and Stack B for redo.</p>
|
||||
<p>Undo and redo operations are implemented using two stacks: Stack A for undo and Stack B for redo.</p>
|
||||
<ol>
|
||||
<li>Each time a user performs an operation, it is pushed onto Stack A, and Stack B is cleared.</li>
|
||||
<li>When the user executes an "undo", the most recent operation is popped from Stack A and pushed onto Stack B.</li>
|
||||
|
||||
Reference in New Issue
Block a user