This commit is contained in:
krahets 2023-04-09 05:12:22 +08:00
parent 01d05cc1f0
commit 37f11aff68
27 changed files with 265 additions and 248 deletions

View File

@ -4,11 +4,11 @@ comments: true
# 4.2.   链表
内存空间是所有程序的公共资源,排除已被占用的内存空间,空闲内存空间通常散落在内存各处。在上一节中,我们提到存储数组的内存空间必须是连续的,而当我们需要申请一个非常大的数组时,空闲内存中可能没有这么大的连续空间。
内存空间是所有程序的公共资源,排除已被占用的内存空间,空闲内存空间通常散落在内存各处。在上一节中,我们提到存储数组的内存空间必须是连续的,而当我们需要申请一个非常大的数组时,空闲内存中可能没有这么大的连续空间。与数组相比,链表更具灵活性,它可以被存储在非连续的内存空间中。
与数组相比,链表更具灵活性,因为它可以存储在非连续的内存空间。「链表 Linked List」是一种线性数据结构其每个元素都是一个节点对象各个节点之间通过指针连接从当前节点通过指针可以访问到下一个节点。由于指针记录了下个节点的内存地址因此无需保证内存地址的连续性从而可以将各个节点分散存储在内存各处。
「链表 Linked List」是一种线性数据结构其每个元素都是一个节点对象各个节点之间通过指针连接从当前节点通过指针可以访问到下一个节点。**由于指针记录了下个节点的内存地址,因此无需保证内存地址的连续性**,从而可以将各个节点分散存储在内存各处。
链表「节点 Node」包含两项数据一是节点「值 Value」二是指向下一节点的「指针 Pointer」或称指向下一节点的「引用 Reference」。
链表「节点 Node」包含两项数据一是节点「值 Value」二是指向下一节点的「指针 Pointer」或称「引用 Reference」。
![链表定义与存储方式](linked_list.assets/linkedlist_definition.png)

View File

@ -935,6 +935,7 @@ comments: true
```python title="my_list.py"
class MyList:
"""列表类简易实现"""
def __init__(self):
"""构造方法"""
self.__capacity: int = 10 # 列表容量

View File

@ -1000,7 +1000,8 @@ $$
def linear_recur(n: int) -> None:
"""线性阶(递归实现)"""
print("递归 n =", n)
if n == 1: return
if n == 1:
return
linear_recur(n - 1)
```
@ -1278,7 +1279,8 @@ $$
```python title="space_complexity.py"
def quadratic_recur(n: int) -> int:
"""平方阶(递归实现)"""
if n <= 0: return 0
if n <= 0:
return 0
# 数组 nums 长度为 n, n-1, ..., 2, 1
nums: list[int] = [0] * n
return quadratic_recur(n - 1)
@ -1408,7 +1410,8 @@ $$
```python title="space_complexity.py"
def build_tree(n: int) -> TreeNode | None:
"""指数阶(建立满二叉树)"""
if n == 0: return None
if n == 0:
return None
root = TreeNode(0)
root.left = build_tree(n - 1)
root.right = build_tree(n - 1)

View File

@ -1769,7 +1769,8 @@ $$
```python title="time_complexity.py"
def exp_recur(n: int) -> int:
"""指数阶(递归实现)"""
if n == 1: return 1
if n == 1:
return 1
return exp_recur(n - 1) + exp_recur(n - 1) + 1
```
@ -2018,7 +2019,8 @@ $$
```python title="time_complexity.py"
def log_recur(n: float) -> int:
"""对数阶(递归实现)"""
if n <= 1: return 0
if n <= 1:
return 0
return log_recur(n / 2) + 1
```
@ -2134,9 +2136,9 @@ $$
```python title="time_complexity.py"
def linear_log_recur(n: float) -> int:
"""线性对数阶"""
if n <= 1: return 1
count: int = linear_log_recur(n // 2) + \
linear_log_recur(n // 2)
if n <= 1:
return 1
count: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)
for _ in range(n):
count += 1
return count
@ -2291,7 +2293,8 @@ $$
```python title="time_complexity.py"
def factorial_recur(n: int) -> int:
"""阶乘阶(递归实现)"""
if n == 0: return 1
if n == 0:
return 1
count: int = 0
# 从 1 个分裂出 n 个
for _ in range(n):

View File

@ -215,6 +215,7 @@ comments: true
```python title="graph_adjacency_matrix.py"
class GraphAdjMat:
"""基于邻接矩阵实现的无向图类"""
# 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
vertices: list[int] = []
# 邻接矩阵,行列索引对应“顶点索引”
@ -967,6 +968,7 @@ comments: true
```python title="graph_adjacency_list.py"
class GraphAdjList:
"""基于邻接表实现的无向图类"""
def __init__(self, edges: list[list[Vertex]]) -> None:
"""构造方法"""
# 邻接表key: 顶点value该顶点的所有邻接顶点

View File

@ -608,12 +608,14 @@ $$
```python title="array_hash_map.py"
class Entry:
"""键值对 int->String"""
def __init__(self, key: int, val: str):
self.key = key
self.val = val
class ArrayHashMap:
"""基于数组简易实现的哈希表"""
def __init__(self):
"""构造方法"""
# 初始化数组,包含 100 个桶

View File

@ -163,11 +163,8 @@ comments: true
=== "Python"
```python title="hashing_search.py"
def hashing_search_linkedlist(mapp: dict[int, ListNode], target: int) -> ListNode | None:
""" 哈希查找(链表) """
# 哈希表的 key: 目标元素value: 节点对象
# 若哈希表中无此 key ,返回 None
return mapp.get(target, None)
def hashing_search_linkedlist(
mapp: dict[int, ListNode], target: int
```
=== "Go"

View File

@ -87,6 +87,7 @@ comments: true
```python title="bucket_sort.py"
def bucket_sort(nums: list[float]) -> None:
"""桶排序"""
# 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
k = len(nums) // 2
buckets = [[] for _ in range(k)]

View File

@ -592,6 +592,7 @@ comments: true
```python title="linkedlist_deque.py"
class ListNode:
"""双向链表节点"""
def __init__(self, val: int) -> None:
"""构造方法"""
self.val: int = val
@ -600,6 +601,7 @@ comments: true
class LinkedListDeque:
"""基于双向链表实现的双向队列"""
def __init__(self) -> None:
"""构造方法"""
self.front: ListNode | None = None # 头节点 front
@ -1584,6 +1586,7 @@ comments: true
```python title="array_deque.py"
class ArrayDeque:
"""基于环形数组实现的双向队列"""
def __init__(self, capacity: int) -> None:
"""构造方法"""
self.__nums: list[int] = [0] * capacity

View File

@ -429,6 +429,7 @@ comments: true
```python title="linkedlist_queue.py"
class LinkedListQueue:
"""基于链表实现的队列"""
def __init__(self):
"""构造方法"""
self.__front: ListNode | None = None # 头节点 front
@ -1104,6 +1105,7 @@ comments: true
```python title="array_queue.py"
class ArrayQueue:
"""基于环形数组实现的队列"""
def __init__(self, size: int) -> None:
"""构造方法"""
self.__nums: list[int] = [0] * size # 用于存储队列元素的数组

View File

@ -410,6 +410,7 @@ comments: true
```python title="linkedlist_stack.py"
class LinkedListStack:
"""基于链表实现的栈"""
def __init__(self):
"""构造方法"""
self.__peek: ListNode | None = None
@ -440,7 +441,8 @@ comments: true
def peek(self) -> int:
"""访问栈顶元素"""
# 判空处理
if not self.__peek: return None
if not self.__peek:
return None
return self.__peek.val
def to_list(self) -> list[int]:
@ -952,6 +954,7 @@ comments: true
```python title="array_stack.py"
class ArrayStack:
"""基于数组实现的栈"""
def __init__(self) -> None:
"""构造方法"""
self.__stack: list[int] = []