This commit is contained in:
krahets
2024-05-02 01:46:20 +08:00
parent 5e90519796
commit 23353e7960
324 changed files with 420 additions and 419 deletions
@@ -3747,7 +3747,7 @@
<!-- Page content -->
<h1 id="41-array">4.1 &nbsp; Array<a class="headerlink" href="#41-array" title="Permanent link">&para;</a></h1>
<p>An "array" is a linear data structure that operates as a lineup of similar items, stored together in a computer's memory in contiguous spaces. It's like a sequence that maintains organized storage. Each item in this lineup has its unique 'spot' known as an "index". Please refer to Figure 4-1 to observe how arrays work and grasp these key terms.</p>
<p>An <u>array</u> is a linear data structure that operates as a lineup of similar items, stored together in a computer's memory in contiguous spaces. It's like a sequence that maintains organized storage. Each item in this lineup has its unique 'spot' known as an <u>index</u>. Please refer to Figure 4-1 to observe how arrays work and grasp these key terms.</p>
<p><a class="glightbox" href="../array.assets/array_definition.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Array definition and storage method" class="animation-figure" src="../array.assets/array_definition.png" /></a></p>
<p align="center"> Figure 4-1 &nbsp; Array definition and storage method </p>
@@ -5183,7 +5183,7 @@ aria-label="Footer"
<div class="md-copyright">
<div class="md-copyright__highlight">
Copyright &copy; 2022-2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
Copyright &copy; 2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
</div>
+1 -1
View File
@@ -3725,7 +3725,7 @@ aria-label="Footer"
<div class="md-copyright">
<div class="md-copyright__highlight">
Copyright &copy; 2022-2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
Copyright &copy; 2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
</div>
@@ -3730,12 +3730,12 @@
<!-- Page content -->
<h1 id="42-linked-list">4.2 &nbsp; Linked list<a class="headerlink" href="#42-linked-list" title="Permanent link">&para;</a></h1>
<p>Memory space is a shared resource among all programs. In a complex system environment, available memory can be dispersed throughout the memory space. We understand that the memory allocated for an array must be continuous. However, for very large arrays, finding a sufficiently large contiguous memory space might be challenging. This is where the flexible advantage of linked lists becomes evident.</p>
<p>A "linked list" is a linear data structure in which each element is a node object, and the nodes are interconnected through "references". These references hold the memory addresses of subsequent nodes, enabling navigation from one node to the next.</p>
<p>A <u>linked list</u> is a linear data structure in which each element is a node object, and the nodes are interconnected through "references". These references hold the memory addresses of subsequent nodes, enabling navigation from one node to the next.</p>
<p>The design of linked lists allows for their nodes to be distributed across memory locations without requiring contiguous memory addresses.</p>
<p><a class="glightbox" href="../linked_list.assets/linkedlist_definition.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Linked list definition and storage method" class="animation-figure" src="../linked_list.assets/linkedlist_definition.png" /></a></p>
<p align="center"> Figure 4-5 &nbsp; Linked list definition and storage method </p>
<p>As shown in the figure, we see that the basic building block of a linked list is the "node" object. Each node comprises two key components: the node's "value" and a "reference" to the next node.</p>
<p>As shown in the figure, we see that the basic building block of a linked list is the <u>node</u> object. Each node comprises two key components: the node's "value" and a "reference" to the next node.</p>
<ul>
<li>The first node in a linked list is the "head node", and the final one is the "tail node".</li>
<li>The tail node points to "null", designated as <code>null</code> in Java, <code>nullptr</code> in C++, and <code>None</code> in Python.</li>
@@ -5248,7 +5248,7 @@ aria-label="Footer"
<div class="md-copyright">
<div class="md-copyright__highlight">
Copyright &copy; 2022-2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
Copyright &copy; 2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
</div>
@@ -3711,13 +3711,13 @@
<!-- Page content -->
<h1 id="43-list">4.3 &nbsp; List<a class="headerlink" href="#43-list" title="Permanent link">&para;</a></h1>
<p>A "list" is an abstract data structure concept that represents an ordered collection of elements, supporting operations such as element access, modification, addition, deletion, and traversal, without requiring users to consider capacity limitations. Lists can be implemented based on linked lists or arrays.</p>
<p>A <u>list</u> is an abstract data structure concept that represents an ordered collection of elements, supporting operations such as element access, modification, addition, deletion, and traversal, without requiring users to consider capacity limitations. Lists can be implemented based on linked lists or arrays.</p>
<ul>
<li>A linked list inherently serves as a list, supporting operations for adding, deleting, searching, and modifying elements, with the flexibility to dynamically adjust its size.</li>
<li>Arrays also support these operations, but due to their immutable length, they can be considered as a list with a length limit.</li>
</ul>
<p>When implementing lists using arrays, <strong>the immutability of length reduces the practicality of the list</strong>. This is because predicting the amount of data to be stored in advance is often challenging, making it difficult to choose an appropriate list length. If the length is too small, it may not meet the requirements; if too large, it may waste memory space.</p>
<p>To solve this problem, we can implement lists using a "dynamic array." It inherits the advantages of arrays and can dynamically expand during program execution.</p>
<p>To solve this problem, we can implement lists using a <u>dynamic array</u>. It inherits the advantages of arrays and can dynamically expand during program execution.</p>
<p>In fact, <strong>many programming languages' standard libraries implement lists using dynamic arrays</strong>, such as Python's <code>list</code>, Java's <code>ArrayList</code>, C++'s <code>vector</code>, and C#'s <code>List</code>. In the following discussion, we will consider "list" and "dynamic array" as synonymous concepts.</p>
<h2 id="431-common-list-operations">4.3.1 &nbsp; Common list operations<a class="headerlink" href="#431-common-list-operations" title="Permanent link">&para;</a></h2>
<h3 id="1-initializing-a-list">1. &nbsp; Initializing a list<a class="headerlink" href="#1-initializing-a-list" title="Permanent link">&para;</a></h3>
@@ -6081,7 +6081,7 @@ aria-label="Footer"
<div class="md-copyright">
<div class="md-copyright__highlight">
Copyright &copy; 2022-2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
Copyright &copy; 2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
</div>
@@ -3612,7 +3612,7 @@
<p>In the first two sections of this chapter, we explored arrays and linked lists, two fundamental and important data structures, representing "continuous storage" and "dispersed storage" respectively.</p>
<p>In fact, <strong>the physical structure largely determines the efficiency of a program's use of memory and cache</strong>, which in turn affects the overall performance of the algorithm.</p>
<h2 id="441-computer-storage-devices">4.4.1 &nbsp; Computer storage devices<a class="headerlink" href="#441-computer-storage-devices" title="Permanent link">&para;</a></h2>
<p>There are three types of storage devices in computers: "hard disk," "random-access memory (RAM)," and "cache memory." The following table shows their different roles and performance characteristics in computer systems.</p>
<p>There are three types of storage devices in computers: <u>hard disk</u>, <u>random-access memory (RAM)</u>, and <u>cache memory</u>. The following table shows their different roles and performance characteristics in computer systems.</p>
<p align="center"> Table 4-2 &nbsp; Computer storage devices </p>
<div class="center-table">
@@ -3681,8 +3681,8 @@
<p>On one hand, <strong>memory is limited and cannot be shared by multiple programs</strong>, so we hope that data structures can use space as efficiently as possible. The elements of an array are tightly packed without extra space for storing references (pointers) between linked list nodes, making them more space-efficient. However, arrays require allocating sufficient continuous memory space at once, which may lead to memory waste, and array expansion also requires additional time and space costs. In contrast, linked lists allocate and reclaim memory dynamically on a per-node basis, providing greater flexibility.</p>
<p>On the other hand, during program execution, <strong>as memory is repeatedly allocated and released, the degree of fragmentation of free memory becomes higher</strong>, leading to reduced memory utilization efficiency. Arrays, due to their continuous storage method, are relatively less likely to cause memory fragmentation. In contrast, the elements of a linked list are dispersedly stored, and frequent insertion and deletion operations make memory fragmentation more likely.</p>
<h2 id="443-cache-efficiency-of-data-structures">4.4.3 &nbsp; Cache efficiency of data structures<a class="headerlink" href="#443-cache-efficiency-of-data-structures" title="Permanent link">&para;</a></h2>
<p>Although caches are much smaller in space capacity than memory, they are much faster and play a crucial role in program execution speed. Since the cache's capacity is limited and can only store a small part of frequently accessed data, when the CPU tries to access data not in the cache, a "cache miss" occurs, forcing the CPU to load the needed data from slower memory.</p>
<p>Clearly, <strong>the fewer the cache misses, the higher the CPU's data read-write efficiency</strong>, and the better the program performance. The proportion of successful data retrieval from the cache by the CPU is called the "cache hit rate," a metric often used to measure cache efficiency.</p>
<p>Although caches are much smaller in space capacity than memory, they are much faster and play a crucial role in program execution speed. Since the cache's capacity is limited and can only store a small part of frequently accessed data, when the CPU tries to access data not in the cache, a <u>cache miss</u> occurs, forcing the CPU to load the needed data from slower memory.</p>
<p>Clearly, <strong>the fewer the cache misses, the higher the CPU's data read-write efficiency</strong>, and the better the program performance. The proportion of successful data retrieval from the cache by the CPU is called the <u>cache hit rate</u>, a metric often used to measure cache efficiency.</p>
<p>To achieve higher efficiency, caches adopt the following data loading mechanisms.</p>
<ul>
<li><strong>Cache lines</strong>: Caches don't store and load data byte by byte but in units of cache lines. Compared to byte-by-byte transfer, the transmission of cache lines is more efficient.</li>
@@ -3890,7 +3890,7 @@ aria-label="Footer"
<div class="md-copyright">
<div class="md-copyright__highlight">
Copyright &copy; 2022-2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
Copyright &copy; 2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
</div>
@@ -3832,7 +3832,7 @@ aria-label="Footer"
<div class="md-copyright">
<div class="md-copyright__highlight">
Copyright &copy; 2022-2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
Copyright &copy; 2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
</div>