mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-25 11:13:38 +08:00
build
This commit is contained in:
@ -1895,18 +1895,16 @@ comments: true
|
||||
|
||||
```c title="my_list.c"
|
||||
/* 列表类简易实现 */
|
||||
struct myList {
|
||||
typedef struct {
|
||||
int *arr; // 数组(存储列表元素)
|
||||
int capacity; // 列表容量
|
||||
int size; // 列表大小
|
||||
int extendRatio; // 列表每次扩容的倍数
|
||||
};
|
||||
|
||||
typedef struct myList myList;
|
||||
} MyList;
|
||||
|
||||
/* 构造函数 */
|
||||
myList *newMyList() {
|
||||
myList *nums = malloc(sizeof(myList));
|
||||
MyList *newMyList() {
|
||||
MyList *nums = malloc(sizeof(MyList));
|
||||
nums->capacity = 10;
|
||||
nums->arr = malloc(sizeof(int) * nums->capacity);
|
||||
nums->size = 0;
|
||||
@ -1915,35 +1913,35 @@ comments: true
|
||||
}
|
||||
|
||||
/* 析构函数 */
|
||||
void delMyList(myList *nums) {
|
||||
void delMyList(MyList *nums) {
|
||||
free(nums->arr);
|
||||
free(nums);
|
||||
}
|
||||
|
||||
/* 获取列表长度 */
|
||||
int size(myList *nums) {
|
||||
int size(MyList *nums) {
|
||||
return nums->size;
|
||||
}
|
||||
|
||||
/* 获取列表容量 */
|
||||
int capacity(myList *nums) {
|
||||
int capacity(MyList *nums) {
|
||||
return nums->capacity;
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
int get(myList *nums, int index) {
|
||||
int get(MyList *nums, int index) {
|
||||
assert(index >= 0 && index < nums->size);
|
||||
return nums->arr[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
void set(myList *nums, int index, int num) {
|
||||
void set(MyList *nums, int index, int num) {
|
||||
assert(index >= 0 && index < nums->size);
|
||||
nums->arr[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
void add(myList *nums, int num) {
|
||||
void add(MyList *nums, int num) {
|
||||
if (size(nums) == capacity(nums)) {
|
||||
extendCapacity(nums); // 扩容
|
||||
}
|
||||
@ -1952,7 +1950,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
void insert(myList *nums, int index, int num) {
|
||||
void insert(MyList *nums, int index, int num) {
|
||||
assert(index >= 0 && index < size(nums));
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size(nums) == capacity(nums)) {
|
||||
@ -1967,7 +1965,7 @@ comments: true
|
||||
|
||||
/* 删除元素 */
|
||||
// 注意:stdio.h 占用了 remove 关键词
|
||||
int removeNum(myList *nums, int index) {
|
||||
int removeNum(MyList *nums, int index) {
|
||||
assert(index >= 0 && index < size(nums));
|
||||
int num = nums->arr[index];
|
||||
for (int i = index; i < size(nums) - 1; i++) {
|
||||
@ -1978,7 +1976,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
void extendCapacity(myList *nums) {
|
||||
void extendCapacity(MyList *nums) {
|
||||
// 先分配空间
|
||||
int newCapacity = capacity(nums) * nums->extendRatio;
|
||||
int *extend = (int *)malloc(sizeof(int) * newCapacity);
|
||||
@ -1997,7 +1995,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 将列表转换为 Array 用于打印 */
|
||||
int *toArray(myList *nums) {
|
||||
int *toArray(MyList *nums) {
|
||||
return nums->arr;
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user