mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 02:26:28 +08:00
style(codes/c): Update my_list.c code format
This commit is contained in:
parent
23f2bf8c67
commit
ed91372594
1 changed files with 42 additions and 42 deletions
|
@ -17,92 +17,92 @@ struct mylist {
|
||||||
|
|
||||||
typedef struct mylist MyList;
|
typedef struct mylist MyList;
|
||||||
/* 前置声明 */
|
/* 前置声明 */
|
||||||
void extendCapacity(MyList* l);
|
void extendCapacity(MyList *list);
|
||||||
|
|
||||||
/* 构造函数 */
|
/* 构造函数 */
|
||||||
void newMyList(MyList* l) {
|
void newMyList(MyList *list) {
|
||||||
l->capacity = 10;
|
list->capacity = 10;
|
||||||
l->nums = malloc(sizeof(int) * l->capacity);
|
list->nums = malloc(sizeof(int) * list->capacity);
|
||||||
l->size = 0;
|
list->size = 0;
|
||||||
l->extendRatio = 2;
|
list->extendRatio = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取列表长度 */
|
/* 获取列表长度 */
|
||||||
int size(MyList* l) {
|
int size(MyList *list) {
|
||||||
return l->size;
|
return list->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取列表容量 */
|
/* 获取列表容量 */
|
||||||
int capacity(MyList* l) {
|
int capacity(MyList *list) {
|
||||||
return l->capacity;
|
return list->capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 访问元素 */
|
/* 访问元素 */
|
||||||
int get(MyList* l, int index) {
|
int get(MyList *list, int index) {
|
||||||
assert(index < l->size);
|
assert(index < list->size);
|
||||||
return l->nums[index];
|
return list->nums[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 更新元素 */
|
/* 更新元素 */
|
||||||
void set(MyList* l, int index, int num) {
|
void set(MyList *list, int index, int num) {
|
||||||
assert(index < l->size);
|
assert(index < list->size);
|
||||||
l->nums[index] = num;
|
list->nums[index] = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 尾部添加元素 */
|
/* 尾部添加元素 */
|
||||||
void add(MyList* l, int num) {
|
void add(MyList *list, int num) {
|
||||||
if (size(l) == capacity(l)) {
|
if (size(list) == capacity(list)) {
|
||||||
extendCapacity(l); // 扩容
|
extendCapacity(list); // 扩容
|
||||||
}
|
}
|
||||||
l->nums[size(l)] = num;
|
list->nums[size(list)] = num;
|
||||||
l->size++;
|
list->size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 中间插入元素 */
|
/* 中间插入元素 */
|
||||||
void insert(MyList* l, int index, int num) {
|
void insert(MyList *list, int index, int num) {
|
||||||
assert(index < size(l));
|
assert(index < size(list));
|
||||||
for (int i=size(l); i>index; --i) {
|
for (int i = size(list); i > index; --i) {
|
||||||
l->nums[i] = l->nums[i-1];
|
list->nums[i] = list->nums[i-1];
|
||||||
}
|
}
|
||||||
l->nums[index] = num;
|
list->nums[index] = num;
|
||||||
l->size++;
|
list->size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除元素 */
|
/* 删除元素 */
|
||||||
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
|
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
|
||||||
// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888
|
// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888
|
||||||
int removeNum(MyList* l, int index) {
|
int removeNum(MyList *list, int index) {
|
||||||
assert(index < size(l));
|
assert(index < size(list));
|
||||||
int num = l->nums[index];
|
int num = list->nums[index];
|
||||||
for (int i=index; i<size(l)-1; i++) {
|
for (int i = index; i < size(list) - 1; i++) {
|
||||||
l->nums[i] = l->nums[i+1];
|
list->nums[i] = list->nums[i+1];
|
||||||
}
|
}
|
||||||
l->size--;
|
list->size--;
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 列表扩容 */
|
/* 列表扩容 */
|
||||||
void extendCapacity(MyList* l) {
|
void extendCapacity(MyList *list) {
|
||||||
// 先分配空间
|
// 先分配空间
|
||||||
size_t newCapacity = capacity(l) * l->extendRatio;
|
int newCapacity = capacity(list) * list->extendRatio;
|
||||||
int *extend = (int *)malloc(sizeof(int) * newCapacity);
|
int *extend = (int *)malloc(sizeof(int) * newCapacity);
|
||||||
int *temp = l->nums;
|
int *temp = list->nums;
|
||||||
|
|
||||||
// 拷贝旧数据到新数据
|
// 拷贝旧数据到新数据
|
||||||
for(size_t i=0; i<size(l); i++)
|
for(int i = 0; i < size(list); i++)
|
||||||
extend[i] = l->nums[i];
|
extend[i] = list->nums[i];
|
||||||
|
|
||||||
// 释放旧数据
|
// 释放旧数据
|
||||||
free(temp);
|
free(temp);
|
||||||
|
|
||||||
// 更新新数据
|
// 更新新数据
|
||||||
l->nums = extend;
|
list->nums = extend;
|
||||||
l->capacity = newCapacity;
|
list->capacity = newCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 将列表转换为 Array 用于打印 */
|
/* 将列表转换为 Array 用于打印 */
|
||||||
int* toArray(MyList* l) {
|
int* toArray(MyList *list) {
|
||||||
return l->nums;
|
return list->nums;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
Loading…
Reference in a new issue