mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-28 04:42:48 +08:00
style(codes/c): update comment format
This commit is contained in:
@ -5,18 +5,16 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "../include/include.h"
|
#include "../include/include.h"
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
typedef int ElemType;
|
|
||||||
|
|
||||||
#define MAX_ELEM_SIZE 10
|
#define MAX_ELEM_SIZE 10
|
||||||
#define CAPACITY_RACTOR 2
|
#define CAPACITY_RACTOR 2
|
||||||
|
|
||||||
// 用数组实现 list
|
// 用数组实现 list
|
||||||
struct list {
|
struct list {
|
||||||
ElemType* nums;
|
int* nums;
|
||||||
size_t numsCapacity;
|
size_t cap;
|
||||||
size_t numsSize;
|
size_t size;
|
||||||
bool isInit;
|
bool isInit;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -24,42 +22,46 @@ typedef struct list List;
|
|||||||
|
|
||||||
void listInit(List* l) {
|
void listInit(List* l) {
|
||||||
if (l->isInit != true) {
|
if (l->isInit != true) {
|
||||||
l->numsCapacity = MAX_ELEM_SIZE;
|
l->cap = MAX_ELEM_SIZE;
|
||||||
l->nums = malloc(sizeof(ElemType) * l->numsCapacity);
|
l->nums = malloc(sizeof(int) * l->cap);
|
||||||
l->numsSize = 0;
|
l->size = 0;
|
||||||
l->isInit = true;
|
l->isInit = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void listInitWithCapacity(List* l, size_t newCapacity) {
|
void listInitWithCapacity(List* l, size_t newCapacity) {
|
||||||
if (l->isInit != true) {
|
if (l->isInit != true) {
|
||||||
l->numsCapacity = newCapacity;
|
l->cap = newCapacity;
|
||||||
l->nums = malloc(sizeof(ElemType) * l->numsCapacity);
|
l->nums = malloc(sizeof(int) * l->cap);
|
||||||
l->numsSize = 0;
|
l->size = 0;
|
||||||
l->isInit = true;
|
l->isInit = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t listSize(List* l) {
|
size_t listSize(List* l) {
|
||||||
return l->numsSize;
|
return l->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t listCapacity(List* l) {
|
size_t listCapacity(List* l) {
|
||||||
return l->numsCapacity;
|
return l->cap;
|
||||||
}
|
}
|
||||||
|
|
||||||
ElemType listGet(List* l, int idx) {
|
int listGet(List* l, int idx) {
|
||||||
|
assert(l->isInit);
|
||||||
if (l->isInit) {
|
if (l->isInit) {
|
||||||
if (idx < l->numsSize) {
|
if (idx < l->size) {
|
||||||
return l->nums[idx];
|
return l->nums[idx];
|
||||||
|
} else {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert("out_of_range");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void listSet(List* l, int idx, ElemType elem) {
|
void listSet(List* l, int idx, int elem) {
|
||||||
|
assert(l->isInit);
|
||||||
if (l->isInit) {
|
if (l->isInit) {
|
||||||
if (idx < l->numsSize) {
|
if (idx < l->size) {
|
||||||
l->nums[idx] = elem;
|
l->nums[idx] = elem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,8 +71,8 @@ void listSet(List* l, int idx, ElemType elem) {
|
|||||||
void listExtendCapacity(List* l) {
|
void listExtendCapacity(List* l) {
|
||||||
// 先分配空间
|
// 先分配空间
|
||||||
size_t newCapacity = listCapacity(l) * CAPACITY_RACTOR;
|
size_t newCapacity = listCapacity(l) * CAPACITY_RACTOR;
|
||||||
ElemType *newData = (ElemType *)malloc(sizeof(ElemType) * newCapacity);
|
int *newData = (int *)malloc(sizeof(int) * newCapacity);
|
||||||
ElemType *oldData = l->nums;
|
int *oldData = l->nums;
|
||||||
|
|
||||||
// 拷贝旧数据到新数据
|
// 拷贝旧数据到新数据
|
||||||
for(size_t i=0; i<listSize(l); i++)
|
for(size_t i=0; i<listSize(l); i++)
|
||||||
@ -81,25 +83,25 @@ void listExtendCapacity(List* l) {
|
|||||||
|
|
||||||
// 更新新数据
|
// 更新新数据
|
||||||
l->nums = newData;
|
l->nums = newData;
|
||||||
l->numsCapacity = newCapacity;
|
l->cap = newCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
void listAdd(List* l, ElemType elem) {
|
void listAdd(List* l, int elem) {
|
||||||
if (listSize(l) == listCapacity(l)) {
|
if (listSize(l) == listCapacity(l)) {
|
||||||
listExtendCapacity(l); // 扩容
|
listExtendCapacity(l); // 扩容
|
||||||
}
|
}
|
||||||
l->nums[listSize(l)] = elem;
|
l->nums[listSize(l)] = elem;
|
||||||
l->numsSize ++;
|
l->size ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void listInsert(List* l, size_t idx, ElemType elem) {
|
void listInsert(List* l, size_t idx, int elem) {
|
||||||
if (l->isInit) {
|
if (l->isInit) {
|
||||||
if (idx < listSize(l)) {
|
if (idx < listSize(l)) {
|
||||||
for (size_t i=listSize(l); i>idx; --i) {
|
for (size_t i=listSize(l); i>idx; --i) {
|
||||||
l->nums[i] = l->nums[i-1];
|
l->nums[i] = l->nums[i-1];
|
||||||
}
|
}
|
||||||
l->nums[idx] = elem;
|
l->nums[idx] = elem;
|
||||||
l->numsSize++;
|
l->size++;
|
||||||
} else {
|
} else {
|
||||||
// 越界 -- 抛出异常
|
// 越界 -- 抛出异常
|
||||||
}
|
}
|
||||||
@ -108,7 +110,7 @@ void listInsert(List* l, size_t idx, ElemType elem) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ElemType listRemove(List* l, int idx) {
|
int listRemove(List* l, int idx) {
|
||||||
if (l->isInit) {
|
if (l->isInit) {
|
||||||
if (idx < listSize(l)) {
|
if (idx < listSize(l)) {
|
||||||
size_t i = idx;
|
size_t i = idx;
|
||||||
@ -117,7 +119,7 @@ ElemType listRemove(List* l, int idx) {
|
|||||||
l->nums[i] = l->nums[i+1];
|
l->nums[i] = l->nums[i+1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
l->numsSize--;
|
l->size--;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// 数组越界
|
// 数组越界
|
||||||
@ -128,15 +130,15 @@ ElemType listRemove(List* l, int idx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void listClear(List* l) {
|
void listClear(List* l) {
|
||||||
l->numsSize = 0;
|
l->size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printList(List* l) {
|
void printList(List* l) {
|
||||||
size_t i=0;
|
size_t i=0;
|
||||||
|
|
||||||
printf("[");
|
printf("[");
|
||||||
if (l->numsSize) {
|
if (l->size) {
|
||||||
for (; i<l->numsSize-1; i++) {
|
for (; i<l->size-1; i++) {
|
||||||
printf("%d, ", l->nums[i]);
|
printf("%d, ", l->nums[i]);
|
||||||
}
|
}
|
||||||
printf("%d", l->nums[i]);
|
printf("%d", l->nums[i]);
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
#include "list_node.h"
|
#include "list_node.h"
|
||||||
#include "tree_node.h"
|
#include "tree_node.h"
|
||||||
|
Reference in New Issue
Block a user