From 3970e88be2b995190c498cf6e8a54bcaecb2d779 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Thu, 12 Jan 2023 20:27:18 +0800 Subject: [PATCH] style(codes/c): update comment format --- codes/c/chapter_array_and_linkedlist/list.c | 62 +++++++++++---------- codes/c/include/include.h | 1 + 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/codes/c/chapter_array_and_linkedlist/list.c b/codes/c/chapter_array_and_linkedlist/list.c index 4af0d93a..52deb1e1 100644 --- a/codes/c/chapter_array_and_linkedlist/list.c +++ b/codes/c/chapter_array_and_linkedlist/list.c @@ -5,18 +5,16 @@ */ #include "../include/include.h" -#include -typedef int ElemType; #define MAX_ELEM_SIZE 10 #define CAPACITY_RACTOR 2 // 用数组实现 list struct list { - ElemType* nums; - size_t numsCapacity; - size_t numsSize; + int* nums; + size_t cap; + size_t size; bool isInit; }; @@ -24,42 +22,46 @@ typedef struct list List; void listInit(List* l) { if (l->isInit != true) { - l->numsCapacity = MAX_ELEM_SIZE; - l->nums = malloc(sizeof(ElemType) * l->numsCapacity); - l->numsSize = 0; + l->cap = MAX_ELEM_SIZE; + l->nums = malloc(sizeof(int) * l->cap); + l->size = 0; l->isInit = true; } } void listInitWithCapacity(List* l, size_t newCapacity) { if (l->isInit != true) { - l->numsCapacity = newCapacity; - l->nums = malloc(sizeof(ElemType) * l->numsCapacity); - l->numsSize = 0; + l->cap = newCapacity; + l->nums = malloc(sizeof(int) * l->cap); + l->size = 0; l->isInit = true; } } size_t listSize(List* l) { - return l->numsSize; + return l->size; } 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 (idx < l->numsSize) { + if (idx < l->size) { 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 (idx < l->numsSize) { + if (idx < l->size) { l->nums[idx] = elem; } } @@ -69,8 +71,8 @@ void listSet(List* l, int idx, ElemType elem) { void listExtendCapacity(List* l) { // 先分配空间 size_t newCapacity = listCapacity(l) * CAPACITY_RACTOR; - ElemType *newData = (ElemType *)malloc(sizeof(ElemType) * newCapacity); - ElemType *oldData = l->nums; + int *newData = (int *)malloc(sizeof(int) * newCapacity); + int *oldData = l->nums; // 拷贝旧数据到新数据 for(size_t i=0; inums = newData; - l->numsCapacity = newCapacity; + l->cap = newCapacity; } -void listAdd(List* l, ElemType elem) { +void listAdd(List* l, int elem) { if (listSize(l) == listCapacity(l)) { listExtendCapacity(l); // 扩容 } 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 (idx < listSize(l)) { for (size_t i=listSize(l); i>idx; --i) { l->nums[i] = l->nums[i-1]; } l->nums[idx] = elem; - l->numsSize++; + l->size++; } 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 (idx < listSize(l)) { size_t i = idx; @@ -117,7 +119,7 @@ ElemType listRemove(List* l, int idx) { l->nums[i] = l->nums[i+1]; } } - l->numsSize--; + l->size--; } else { // 数组越界 @@ -128,15 +130,15 @@ ElemType listRemove(List* l, int idx) { } void listClear(List* l) { - l->numsSize = 0; + l->size = 0; } void printList(List* l) { size_t i=0; printf("["); - if (l->numsSize) { - for (; inumsSize-1; i++) { + if (l->size) { + for (; isize-1; i++) { printf("%d, ", l->nums[i]); } printf("%d", l->nums[i]); diff --git a/codes/c/include/include.h b/codes/c/include/include.h index 9f30b052..e3919a9f 100644 --- a/codes/c/include/include.h +++ b/codes/c/include/include.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "list_node.h" #include "tree_node.h"