mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-24 18:43:59 +08:00
build
This commit is contained in:
@@ -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