mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
feat: Revised the book (#978)
* Sync recent changes to the revised Word. * Revised the preface chapter * Revised the introduction chapter * Revised the computation complexity chapter * Revised the chapter data structure * Revised the chapter array and linked list * Revised the chapter stack and queue * Revised the chapter hashing * Revised the chapter tree * Revised the chapter heap * Revised the chapter graph * Revised the chapter searching * Reivised the sorting chapter * Revised the divide and conquer chapter * Revised the chapter backtacking * Revised the DP chapter * Revised the greedy chapter * Revised the appendix chapter * Revised the preface chapter doubly * Revised the figures
This commit is contained in:
@ -37,11 +37,11 @@ void insert(int *nums, int size, int num, int index) {
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
// 将 num 赋给 index 处的元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
/* 删除索引 index 处的元素 */
|
||||
// 注意:stdio.h 占用了 remove 关键词
|
||||
void removeItem(int *nums, int size, int index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
|
||||
@ -56,7 +56,7 @@ int main() {
|
||||
ListNode *n2 = newListNode(2);
|
||||
ListNode *n3 = newListNode(5);
|
||||
ListNode *n4 = newListNode(4);
|
||||
// 构建引用指向
|
||||
// 构建节点之间的引用
|
||||
n0->next = n1;
|
||||
n1->next = n2;
|
||||
n2->next = n3;
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 列表类简易实现 */
|
||||
/* 列表类 */
|
||||
typedef struct {
|
||||
int *arr; // 数组(存储列表元素)
|
||||
int capacity; // 列表容量
|
||||
@ -54,7 +54,7 @@ void set(MyList *nums, int index, int num) {
|
||||
nums->arr[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
void add(MyList *nums, int num) {
|
||||
if (size(nums) == capacity(nums)) {
|
||||
extendCapacity(nums); // 扩容
|
||||
@ -63,7 +63,7 @@ void add(MyList *nums, int num) {
|
||||
nums->size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
void insert(MyList *nums, int index, int num) {
|
||||
assert(index >= 0 && index < size(nums));
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
@ -117,7 +117,7 @@ int *toArray(MyList *nums) {
|
||||
int main() {
|
||||
/* 初始化列表 */
|
||||
MyList *nums = newMyList();
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
add(nums, 1);
|
||||
add(nums, 3);
|
||||
add(nums, 2);
|
||||
@ -127,7 +127,7 @@ int main() {
|
||||
printArray(toArray(nums), size(nums));
|
||||
printf("容量 = %d ,长度 = %d\n", capacity(nums), size(nums));
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
insert(nums, 3, 6);
|
||||
printf("在索引 3 处插入数字 6 ,得到 nums = ");
|
||||
printArray(toArray(nums), size(nums));
|
||||
|
||||
Reference in New Issue
Block a user