mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-09 22:16:06 +00:00
build
This commit is contained in:
@@ -199,7 +199,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "C"
|
||||
|
||||
```c title="array.c"
|
||||
[class]{}-[func]{randomAccess}
|
||||
/* 随机返回一个数组元素 */
|
||||
int randomAccess(int *nums, int size) {
|
||||
// 在区间 [0, size) 中随机抽取一个数字
|
||||
int randomIndex = rand() % size;
|
||||
// 获取并返回随机元素
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -350,7 +357,21 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "C"
|
||||
|
||||
```c title="array.c"
|
||||
[class]{}-[func]{extend}
|
||||
/* 扩展数组长度 */
|
||||
int *extend(int *nums, int size, int enlarge) {
|
||||
// 初始化一个扩展长度后的数组
|
||||
int *res = (int *)malloc(sizeof(int) * (size + enlarge));
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
for (int i = 0; i < size; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// 初始化扩展后的空间
|
||||
for (int i = size; i < size + enlarge; i++) {
|
||||
res[i] = 0;
|
||||
}
|
||||
// 返回扩展后的新数组
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -493,7 +514,15 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "C"
|
||||
|
||||
```c title="array.c"
|
||||
[class]{}-[func]{insert}
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
void insert(int *nums, int size, int num, int index) {
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
nums[index] = num;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -605,7 +634,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "C"
|
||||
|
||||
```c title="array.c"
|
||||
[class]{}-[func]{removeItem}
|
||||
/* 删除索引 index 处元素 */
|
||||
// 注意:stdio.h 占用了 remove 关键词
|
||||
void removeItem(int *nums, int size, int index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (int i = index; i < size - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -757,7 +793,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "C"
|
||||
|
||||
```c title="array.c"
|
||||
[class]{}-[func]{traverse}
|
||||
/* 遍历数组 */
|
||||
void traverse(int *nums, int size) {
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (int i = 0; i < size; i++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -900,7 +943,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "C"
|
||||
|
||||
```c title="array.c"
|
||||
[class]{}-[func]{find}
|
||||
/* 在数组中查找指定元素 */
|
||||
int find(int *nums, int size, int target) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
@@ -101,10 +101,9 @@ comments: true
|
||||
struct ListNode *next; // 指向下一节点的指针(引用)
|
||||
};
|
||||
|
||||
// typedef 作用是为一种数据类型定义一个新名字
|
||||
typedef struct ListNode ListNode;
|
||||
|
||||
/* 构造函数,初始化一个新节点 */
|
||||
/* 构造函数 */
|
||||
ListNode *newListNode(int val) {
|
||||
ListNode *node, *next;
|
||||
node = (ListNode *) malloc(sizeof(ListNode));
|
||||
@@ -416,7 +415,12 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="linked_list.c"
|
||||
[class]{}-[func]{insertNode}
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
void insert(ListNode *n0, ListNode *P) {
|
||||
ListNode *n1 = n0->next;
|
||||
P->next = n1;
|
||||
n0->next = P;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -548,7 +552,18 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="linked_list.c"
|
||||
[class]{}-[func]{removeNode}
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
// 注意:stdio.h 占用了 remove 关键词
|
||||
void removeNode(ListNode *n0) {
|
||||
if (!n0->next)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode *P = n0->next;
|
||||
ListNode *n1 = P->next;
|
||||
n0->next = n1;
|
||||
// 释放内存
|
||||
free(P);
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -687,7 +702,14 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="linked_list.c"
|
||||
[class]{}-[func]{access}
|
||||
/* 访问链表中索引为 index 的节点 */
|
||||
ListNode *access(ListNode *head, int index) {
|
||||
while (head && head->next && index) {
|
||||
head = head->next;
|
||||
index--;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -843,7 +865,17 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="linked_list.c"
|
||||
[class]{}-[func]{findNode}
|
||||
/* 在链表中查找值为 target 的首个节点 */
|
||||
int find(ListNode *head, int target) {
|
||||
int index = 0;
|
||||
while (head) {
|
||||
if (head->val == target)
|
||||
return index;
|
||||
head = head->next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -996,7 +1028,24 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
/* 双向链表节点结构体 */
|
||||
struct ListNode {
|
||||
int val; // 节点值
|
||||
struct ListNode *next; // 指向后继节点的指针(引用)
|
||||
struct ListNode *prev; // 指向前驱节点的指针(引用)
|
||||
};
|
||||
|
||||
typedef struct ListNode ListNode;
|
||||
|
||||
/* 构造函数 */
|
||||
ListNode *newListNode(int val) {
|
||||
ListNode *node, *next;
|
||||
node = (ListNode *) malloc(sizeof(ListNode));
|
||||
node->val = val;
|
||||
node->next = NULL;
|
||||
node->prev = NULL;
|
||||
return node;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
@@ -77,7 +77,7 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="list.c"
|
||||
|
||||
// C 未提供内置动态数组
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -175,7 +175,7 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="list.c"
|
||||
|
||||
// C 未提供内置动态数组
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -333,7 +333,7 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="list.c"
|
||||
|
||||
// C 未提供内置动态数组
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -495,7 +495,7 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="list.c"
|
||||
|
||||
// C 未提供内置动态数组
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -603,7 +603,7 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="list.c"
|
||||
|
||||
// C 未提供内置动态数组
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -679,7 +679,7 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="list.c"
|
||||
|
||||
// C 未提供内置动态数组
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -1320,7 +1320,106 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="my_list.c"
|
||||
[class]{myList}-[func]{}
|
||||
/* 列表类简易实现 */
|
||||
struct myList {
|
||||
int *nums; // 数组(存储列表元素)
|
||||
int capacity; // 列表容量
|
||||
int size; // 列表大小
|
||||
int extendRatio; // 列表每次扩容的倍数
|
||||
};
|
||||
|
||||
/* 构造函数 */
|
||||
myList *newMyList() {
|
||||
myList *list = malloc(sizeof(myList));
|
||||
list->capacity = 10;
|
||||
list->nums = malloc(sizeof(int) * list->capacity);
|
||||
list->size = 0;
|
||||
list->extendRatio = 2;
|
||||
return list;
|
||||
}
|
||||
|
||||
/* 析构函数 */
|
||||
void delMyList(myList *list) {
|
||||
free(list->nums);
|
||||
free(list);
|
||||
}
|
||||
|
||||
/* 获取列表长度 */
|
||||
int size(myList *list) {
|
||||
return list->size;
|
||||
}
|
||||
|
||||
/* 获取列表容量 */
|
||||
int capacity(myList *list) {
|
||||
return list->capacity;
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
int get(myList *list, int index) {
|
||||
assert(index >= 0 && index < list->size);
|
||||
return list->nums[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
void set(myList *list, int index, int num) {
|
||||
assert(index >= 0 && index < list->size);
|
||||
list->nums[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
void add(myList *list, int num) {
|
||||
if (size(list) == capacity(list)) {
|
||||
extendCapacity(list); // 扩容
|
||||
}
|
||||
list->nums[size(list)] = num;
|
||||
list->size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
void insert(myList *list, int index, int num) {
|
||||
assert(index >= 0 && index < size(list));
|
||||
for (int i = size(list); i > index; --i) {
|
||||
list->nums[i] = list->nums[i - 1];
|
||||
}
|
||||
list->nums[index] = num;
|
||||
list->size++;
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
// 注意:stdio.h 占用了 remove 关键词
|
||||
int removeNum(myList *list, int index) {
|
||||
assert(index >= 0 && index < size(list));
|
||||
int num = list->nums[index];
|
||||
for (int i = index; i < size(list) - 1; i++) {
|
||||
list->nums[i] = list->nums[i + 1];
|
||||
}
|
||||
list->size--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
void extendCapacity(myList *list) {
|
||||
// 先分配空间
|
||||
int newCapacity = capacity(list) * list->extendRatio;
|
||||
int *extend = (int *)malloc(sizeof(int) * newCapacity);
|
||||
int *temp = list->nums;
|
||||
|
||||
// 拷贝旧数据到新数据
|
||||
for (int i = 0; i < size(list); i++)
|
||||
extend[i] = list->nums[i];
|
||||
|
||||
// 释放旧数据
|
||||
free(temp);
|
||||
|
||||
// 更新新数据
|
||||
list->nums = extend;
|
||||
list->capacity = newCapacity;
|
||||
}
|
||||
|
||||
/* 将列表转换为 Array 用于打印 */
|
||||
int *toArray(myList *list) {
|
||||
return list->nums;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
Reference in New Issue
Block a user