diff --git a/codes/c/chapter_array_and_linkedlist/array.c b/codes/c/chapter_array_and_linkedlist/array.c index d251ffdb..39bfd1be 100644 --- a/codes/c/chapter_array_and_linkedlist/array.c +++ b/codes/c/chapter_array_and_linkedlist/array.c @@ -42,6 +42,7 @@ void insert(int *nums, int size, int num, int index) { } /* 删除索引 index 处元素 */ +// 注意:stdio.h 占用了 remove 关键词 void removeItem(int *nums, int size, int index) { // 把索引 index 之后的所有元素向前移动一位 for (int i = index; i < size - 1; i++) { diff --git a/codes/c/chapter_array_and_linkedlist/linked_list.c b/codes/c/chapter_array_and_linkedlist/linked_list.c index 6f449179..af2ad758 100644 --- a/codes/c/chapter_array_and_linkedlist/linked_list.c +++ b/codes/c/chapter_array_and_linkedlist/linked_list.c @@ -14,8 +14,7 @@ void insert(ListNode *n0, ListNode *P) { } /* 删除链表的节点 n0 之后的首个节点 */ -// 由于引入了 stdio.h ,此处无法使用 remove 关键词 -// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888 +// 注意:stdio.h 占用了 remove 关键词 void removeNode(ListNode *n0) { if (!n0->next) return; diff --git a/codes/c/chapter_array_and_linkedlist/my_list.c b/codes/c/chapter_array_and_linkedlist/my_list.c index 33ec2bf3..fce46517 100644 --- a/codes/c/chapter_array_and_linkedlist/my_list.c +++ b/codes/c/chapter_array_and_linkedlist/my_list.c @@ -6,7 +6,7 @@ #include "../include/include.h" -// 用数组实现 list +/* 列表类简易实现 */ struct myList { int *nums; // 数组(存储列表元素) int capacity; // 列表容量 @@ -16,10 +16,9 @@ struct myList { typedef struct myList myList; -/* 前置声明 */ void extendCapacity(myList *list); -/* 构造方法 */ +/* 构造函数 */ myList *newMyList() { myList *list = malloc(sizeof(myList)); list->capacity = 10; @@ -29,7 +28,7 @@ myList *newMyList() { return list; } -/* 析构方法 */ +/* 析构函数 */ void delMyList(myList *list) { free(list->nums); free(list); @@ -77,8 +76,7 @@ void insert(myList *list, int index, int num) { } /* 删除元素 */ -// 由于引入了 stdio.h ,此处无法使用 remove 关键词 -// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888 +// 注意:stdio.h 占用了 remove 关键词 int removeNum(myList *list, int index) { assert(index >= 0 && index < size(list)); int num = list->nums[index]; diff --git a/codes/c/chapter_computational_complexity/space_complexity.c b/codes/c/chapter_computational_complexity/space_complexity.c index 0eec264f..0d059b5b 100644 --- a/codes/c/chapter_computational_complexity/space_complexity.c +++ b/codes/c/chapter_computational_complexity/space_complexity.c @@ -29,10 +29,12 @@ void constant(int n) { } } -typedef struct { +/* 哈希表 */ +typedef struct hashTable { int key; - char val[10]; - UT_hash_handle hh; // 借助 uthash 实现的哈希表 + int val; + // 借助 LetCode 上常用的哈希表 + UT_hash_handle hh; } hashTable; /* 线性阶 */ @@ -58,7 +60,7 @@ void linear(int n) { for (int i = 0; i < n; i++) { hashTable *tmp = malloc(sizeof(hashTable)); tmp->key = i; - sprintf(tmp->val, "%d", i); + tmp->val = i; HASH_ADD_INT(h, key, tmp); } diff --git a/codes/c/chapter_heap/my_heap.c b/codes/c/chapter_heap/my_heap.c index d59f3c3d..2b6cedf1 100644 --- a/codes/c/chapter_heap/my_heap.c +++ b/codes/c/chapter_heap/my_heap.c @@ -9,18 +9,20 @@ #define MAX_SIZE 5000 /* 大顶堆 */ -typedef struct maxHeap { +struct maxHeap { // size 代表的是实际元素的个数 int size; // 使用预先分配内存的数组,避免扩容 int data[MAX_SIZE]; -} maxHeap; +}; + +typedef struct maxHeap maxHeap; void siftDown(maxHeap *h, int i); void siftUp(maxHeap *h, int i); -/* 构造方法,根据切片建堆 */ +/* 构造函数,根据切片建堆 */ maxHeap *newMaxHeap(int nums[], int size) { // 所有元素入堆 maxHeap *h = (maxHeap *)malloc(sizeof(maxHeap)); diff --git a/codes/c/chapter_sorting/quick_sort.c b/codes/c/chapter_sorting/quick_sort.c index 440ee032..66ded35e 100644 --- a/codes/c/chapter_sorting/quick_sort.c +++ b/codes/c/chapter_sorting/quick_sort.c @@ -15,7 +15,7 @@ void swap(int nums[], int i, int j) { /* 快速排序类 */ // 快速排序类-哨兵划分 -int quickSortPartition(int nums[], int left, int right) { +int partition(int nums[], int left, int right) { // 以 nums[left] 作为基准数 int i = left, j = right; while (i < j) { @@ -43,7 +43,7 @@ void quickSort(int nums[], int left, int right) { return; } // 哨兵划分 - int pivot = quickSortPartition(nums, left, right); + int pivot = partition(nums, left, right); // 递归左子数组、右子数组 quickSort(nums, left, pivot - 1); quickSort(nums, pivot + 1, right); @@ -63,7 +63,7 @@ int medianThree(int nums[], int left, int mid, int right) { } // 哨兵划分(三数取中值) -int quickSortMedianPartition(int nums[], int left, int right) { +int partitionMedian(int nums[], int left, int right) { // 选取三个候选元素的中位数 int med = medianThree(nums, left, (left + right) / 2, right); // 将中位数交换至数组最左端 @@ -87,34 +87,19 @@ void quickSortMedian(int nums[], int left, int right) { if (left >= right) return; // 哨兵划分 - int pivot = quickSortMedianPartition(nums, left, right); + int pivot = partitionMedian(nums, left, right); // 递归左子数组、右子数组 quickSortMedian(nums, left, pivot - 1); quickSortMedian(nums, pivot + 1, right); } /* 快速排序类(尾递归优化) */ -/* 尾递归优化-哨兵划分 */ -int quickSortTailCallPartition(int nums[], int left, int right) { - // 以 nums[left] 作为基准数 - int i = left, j = right; - while (i < j) { - while (i < j && nums[j] >= nums[left]) - j--; // 从右向左找首个小于基准数的元素 - while (i < j && nums[i] <= nums[left]) - i++; // 从左向右找首个大于基准数的元素 - swap(nums, i, j); // 交换这两个元素 - } - swap(nums, i, left); // 将基准数交换至两子数组的分界线 - return i; // 返回基准数的索引 -} - // 快速排序(尾递归优化) void quickSortTailCall(int nums[], int left, int right) { // 子数组长度为 1 时终止 while (left < right) { // 哨兵划分操作 - int pivot = quickSortTailCallPartition(nums, left, right); + int pivot = partition(nums, left, right); // 对两个子数组中较短的那个执行快排 if (pivot - left < right - pivot) { quickSortTailCall(nums, left, pivot - 1); // 递归排序左子数组 diff --git a/codes/c/chapter_stack_and_queue/array_deque.c b/codes/c/chapter_stack_and_queue/array_deque.c index 017d2ad7..e7c4d896 100644 --- a/codes/c/chapter_stack_and_queue/array_deque.c +++ b/codes/c/chapter_stack_and_queue/array_deque.c @@ -6,19 +6,19 @@ #include "../include/include.h" -/* 基于环形数组形成的双向队列 */ -struct ArrayDeque { +/* 基于环形数组实现的双向队列 */ +struct arrayDeque { int *nums; // 用于存储队列元素的数组 int front; // 队首指针,指向队首元素 int queSize; // 尾指针,指向队尾 + 1 int queCapacity; // 队列容量 }; -typedef struct ArrayDeque ArrayDeque; +typedef struct arrayDeque arrayDeque; -/* 构造方法 */ -ArrayDeque *newArrayDeque(int capacity) { - ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque)); +/* 构造函数 */ +arrayDeque *newArrayDeque(int capacity) { + arrayDeque *deque = (arrayDeque *)malloc(sizeof(arrayDeque)); // 初始化数组 deque->queCapacity = capacity; deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity); @@ -26,28 +26,28 @@ ArrayDeque *newArrayDeque(int capacity) { return deque; } -/* 析构方法 */ -void delArrayDeque(ArrayDeque *deque) { +/* 析构函数 */ +void delArrayDeque(arrayDeque *deque) { free(deque->nums); deque->queCapacity = 0; } /* 获取双向队列的容量 */ -int capacity(ArrayDeque *deque) { +int capacity(arrayDeque *deque) { return deque->queCapacity; } /* 获取双向队列的长度 */ -int size(ArrayDeque *deque) { +int size(arrayDeque *deque) { return deque->queSize; } /* 判断双向队列是否为空 */ -bool empty(ArrayDeque *deque) { +bool empty(arrayDeque *deque) { return deque->queSize == 0; } -int dequeIndex(ArrayDeque *deque, int i) { +int dequeIndex(arrayDeque *deque, int i) { // 通过取余操作实现数组首尾相连 // 当 i 越过数组尾部时,回到头部 // 当 i 越过数组头部后,回到尾部 @@ -55,7 +55,7 @@ int dequeIndex(ArrayDeque *deque, int i) { } /* 队首入队 */ -void pushFirst(ArrayDeque *deque, int num) { +void pushFirst(arrayDeque *deque, int num) { if (deque->queSize == capacity(deque)) { printf("双向队列已满\r\n"); return; @@ -69,7 +69,7 @@ void pushFirst(ArrayDeque *deque, int num) { } /* 队尾入队 */ -void pushLast(ArrayDeque *deque, int num) { +void pushLast(arrayDeque *deque, int num) { if (deque->queSize == capacity(deque)) { printf("双向队列已满\r\n"); return; @@ -82,14 +82,14 @@ void pushLast(ArrayDeque *deque, int num) { } /* 访问队首元素 */ -int peekFirst(ArrayDeque *deque) { +int peekFirst(arrayDeque *deque) { // 访问异常:双向队列为空 assert(empty(deque) == 0); return deque->nums[deque->front]; } /* 访问队尾元素 */ -int peekLast(ArrayDeque *deque) { +int peekLast(arrayDeque *deque) { // 访问异常:双向队列为空 assert(empty(deque) == 0); int last = dequeIndex(deque, deque->front + deque->queSize - 1); @@ -97,7 +97,7 @@ int peekLast(ArrayDeque *deque) { } /* 队首出队 */ -int popFirst(ArrayDeque *deque) { +int popFirst(arrayDeque *deque) { int num = peekFirst(deque); // 队首指针向后移动一位 deque->front = dequeIndex(deque, deque->front + 1); @@ -106,14 +106,14 @@ int popFirst(ArrayDeque *deque) { } /* 队尾出队 */ -int popLast(ArrayDeque *deque) { +int popLast(arrayDeque *deque) { int num = peekLast(deque); deque->queSize--; return num; } -/* 打印基于环形数组形成的队列 */ -void printArrayDeque(ArrayDeque *deque) { +/* 打印队列 */ +void printArrayDeque(arrayDeque *deque) { int arr[deque->queSize]; // 拷贝 for (int i = 0, j = deque->front; i < deque->queSize; i++, j++) { @@ -126,7 +126,7 @@ void printArrayDeque(ArrayDeque *deque) { int main() { /* 初始化队列 */ int capacity = 10; - ArrayDeque *deque = newArrayDeque(capacity); + arrayDeque *deque = newArrayDeque(capacity); pushLast(deque, 3); pushLast(deque, 2); pushLast(deque, 5); diff --git a/codes/c/chapter_stack_and_queue/array_queue.c b/codes/c/chapter_stack_and_queue/array_queue.c index 0cc75f8e..6aaedf41 100644 --- a/codes/c/chapter_stack_and_queue/array_queue.c +++ b/codes/c/chapter_stack_and_queue/array_queue.c @@ -6,19 +6,19 @@ #include "../include/include.h" -/* 基于环形数组形成的队列 */ -struct ArrayQueue { +/* 基于环形数组实现的队列 */ +struct arrayQueue { int *nums; // 用于存储队列元素的数组 int front; // 队首指针,指向队首元素 int queSize; // 尾指针,指向队尾 + 1 int queCapacity; // 队列容量 }; -typedef struct ArrayQueue ArrayQueue; +typedef struct arrayQueue arrayQueue; -/* 构造方法 */ -ArrayQueue *newArrayQueue(int capacity) { - ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue)); +/* 构造函数 */ +arrayQueue *newArrayQueue(int capacity) { + arrayQueue *queue = (arrayQueue *)malloc(sizeof(arrayQueue)); // 初始化数组 queue->queCapacity = capacity; queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity); @@ -26,35 +26,35 @@ ArrayQueue *newArrayQueue(int capacity) { return queue; } -/* 析构方法 */ -void delArrayQueue(ArrayQueue *queue) { +/* 析构函数 */ +void delArrayQueue(arrayQueue *queue) { free(queue->nums); queue->queCapacity = 0; } /* 获取队列的容量 */ -int capacity(ArrayQueue *queue) { +int capacity(arrayQueue *queue) { return queue->queCapacity; } /* 获取队列的长度 */ -int size(ArrayQueue *queue) { +int size(arrayQueue *queue) { return queue->queSize; } /* 判断队列是否为空 */ -bool empty(ArrayQueue *queue) { +bool empty(arrayQueue *queue) { return queue->queSize == 0; } /* 访问队首元素 */ -int peek(ArrayQueue *queue) { +int peek(arrayQueue *queue) { assert(size(queue) != 0); return queue->nums[queue->front]; } /* 入队 */ -void push(ArrayQueue *queue, int num) { +void push(arrayQueue *queue, int num) { if (size(queue) == capacity(queue)) { printf("队列已满\r\n"); return; @@ -68,15 +68,15 @@ void push(ArrayQueue *queue, int num) { } /* 出队 */ -void pop(ArrayQueue *queue) { +void pop(arrayQueue *queue) { int num = peek(queue); // 队首指针向后移动一位,若越过尾部则返回到数组头部 queue->front = (queue->front + 1) % queue->queCapacity; queue->queSize--; } -/* 打印基于环形数组形成的队列 */ -void printArrayQueue(ArrayQueue *queue) { +/* 打印队列 */ +void printArrayQueue(arrayQueue *queue) { int arr[queue->queSize]; // 拷贝 for (int i = 0, j = queue->front; i < queue->queSize; i++, j++) { @@ -89,7 +89,7 @@ void printArrayQueue(ArrayQueue *queue) { int main() { /* 初始化队列 */ int capacity = 10; - ArrayQueue *queue = newArrayQueue(capacity); + arrayQueue *queue = newArrayQueue(capacity); /* 元素入队 */ push(queue, 1); diff --git a/codes/c/chapter_stack_and_queue/array_stack.c b/codes/c/chapter_stack_and_queue/array_stack.c index d8f48e18..c96c1ffb 100644 --- a/codes/c/chapter_stack_and_queue/array_stack.c +++ b/codes/c/chapter_stack_and_queue/array_stack.c @@ -16,6 +16,7 @@ struct arrayStack { typedef struct arrayStack arrayStack; +/* 构造函数 */ arrayStack *newArrayStack() { arrayStack *s = malloc(sizeof(arrayStack)); // 初始化一个大容量,避免扩容 diff --git a/codes/c/chapter_stack_and_queue/linkedlist_deque.c b/codes/c/chapter_stack_and_queue/linkedlist_deque.c index 9888849a..fff6d671 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_deque.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_deque.c @@ -7,50 +7,50 @@ #include "../include/include.h" /* 双向链表节点 */ -struct DoublyListNode { +struct doublyListNode { int val; // 节点值 - struct DoublyListNode *next; // 后继节点 - struct DoublyListNode *prev; // 前驱节点 + struct doublyListNode *next; // 后继节点 + struct doublyListNode *prev; // 前驱节点 }; -typedef struct DoublyListNode DoublyListNode; +typedef struct doublyListNode doublyListNode; -/* 双向链表节点构造方法 */ -DoublyListNode *newDoublyListNode(int num) { - DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode)); +/* 构造函数 */ +doublyListNode *newDoublyListNode(int num) { + doublyListNode *new = (doublyListNode *)malloc(sizeof(doublyListNode)); new->val = num; new->next = NULL; new->prev = NULL; return new; } -/* 双向链表节点析构方法 */ -void delDoublyListNode(DoublyListNode *node) { +/* 析构函数 */ +void delDoublyListNode(doublyListNode *node) { free(node); } /* 基于双向链表实现的双向队列 */ -struct LinkedListDeque { - DoublyListNode *front, *rear; // 头节点 front ,尾节点 rear +struct linkedListDeque { + doublyListNode *front, *rear; // 头节点 front ,尾节点 rear int queSize; // 双向队列的长度 }; -typedef struct LinkedListDeque LinkedListDeque; +typedef struct linkedListDeque linkedListDeque; -/* 构造方法 */ -LinkedListDeque *newLinkedListDeque() { - LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque)); +/* 构造j */ +linkedListDeque *newLinkedListDeque() { + linkedListDeque *deque = (linkedListDeque *)malloc(sizeof(linkedListDeque)); deque->front = NULL; deque->rear = NULL; deque->queSize = 0; return deque; } -/* 析构方法 */ -void delLinkedListdeque(LinkedListDeque *deque) { +/* 析构函数 */ +void delLinkedListdeque(linkedListDeque *deque) { // 释放所有节点 for (int i = 0; i < deque->queSize && deque->front != NULL; i++) { - DoublyListNode *tmp = deque->front; + doublyListNode *tmp = deque->front; deque->front = deque->front->next; free(tmp); } @@ -59,18 +59,18 @@ void delLinkedListdeque(LinkedListDeque *deque) { } /* 获取队列的长度 */ -int size(LinkedListDeque *deque) { +int size(linkedListDeque *deque) { return deque->queSize; } /* 判断队列是否为空 */ -bool empty(LinkedListDeque *deque) { +bool empty(linkedListDeque *deque) { return (size(deque) == 0); } /* 入队 */ -void push(LinkedListDeque *deque, int num, bool isFront) { - DoublyListNode *node = newDoublyListNode(num); +void push(linkedListDeque *deque, int num, bool isFront) { + doublyListNode *node = newDoublyListNode(num); // 若链表为空,则令 front, rear 都指向node if (empty(deque)) { deque->front = deque->rear = node; @@ -93,36 +93,36 @@ void push(LinkedListDeque *deque, int num, bool isFront) { } /* 队首入队 */ -void pushFirst(LinkedListDeque *deque, int num) { +void pushFirst(linkedListDeque *deque, int num) { push(deque, num, true); } /* 队尾入队 */ -void pushLast(LinkedListDeque *deque, int num) { +void pushLast(linkedListDeque *deque, int num) { push(deque, num, false); } /* 访问队首元素 */ -int peekFirst(LinkedListDeque *deque) { +int peekFirst(linkedListDeque *deque) { assert(size(deque) && deque->front); return deque->front->val; } /* 访问队尾元素 */ -int peekLast(LinkedListDeque *deque) { +int peekLast(linkedListDeque *deque) { assert(size(deque) && deque->rear); return deque->rear->val; } /* 出队 */ -int pop(LinkedListDeque *deque, bool isFront) { +int pop(linkedListDeque *deque, bool isFront) { if (empty(deque)) return -1; int val; // 队首出队操作 if (isFront) { val = peekFirst(deque); // 暂存头节点值 - DoublyListNode *fNext = deque->front->next; + doublyListNode *fNext = deque->front->next; if (fNext) { fNext->prev = NULL; deque->front->next = NULL; @@ -133,7 +133,7 @@ int pop(LinkedListDeque *deque, bool isFront) { // 队尾出队操作 else { val = peekLast(deque); // 暂存尾节点值 - DoublyListNode *rPrev = deque->rear->prev; + doublyListNode *rPrev = deque->rear->prev; if (rPrev) { rPrev->next = NULL; deque->rear->prev = NULL; @@ -146,21 +146,21 @@ int pop(LinkedListDeque *deque, bool isFront) { } /* 队首出队 */ -int popFirst(LinkedListDeque *deque) { +int popFirst(linkedListDeque *deque) { return pop(deque, true); } /* 队尾出队 */ -int popLast(LinkedListDeque *deque) { +int popLast(linkedListDeque *deque) { return pop(deque, false); } /* 打印队列 */ -void printLinkedListDeque(LinkedListDeque *deque) { +void printLinkedListDeque(linkedListDeque *deque) { int arr[deque->queSize]; // 拷贝链表中的数据到数组 int i; - DoublyListNode *node; + doublyListNode *node; for (i = 0, node = deque->front; i < deque->queSize; i++) { arr[i] = node->val; node = node->next; @@ -171,7 +171,7 @@ void printLinkedListDeque(LinkedListDeque *deque) { /* Driver Code */ int main() { /* 初始化双向队列 */ - LinkedListDeque *deque = newLinkedListDeque(); + linkedListDeque *deque = newLinkedListDeque(); pushLast(deque, 3); pushLast(deque, 2); pushLast(deque, 5); diff --git a/codes/c/chapter_stack_and_queue/linkedlist_queue.c b/codes/c/chapter_stack_and_queue/linkedlist_queue.c index ef5b466d..b5a208f4 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_queue.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_queue.c @@ -7,24 +7,24 @@ #include "../include/include.h" /* 基于链表实现的队列 */ -struct LinkedListQueue { +struct linkedListQueue { ListNode *front, *rear; int queSize; }; -typedef struct LinkedListQueue LinkedListQueue; +typedef struct linkedListQueue linkedListQueue; -/* 构造方法 */ -LinkedListQueue *newLinkedListQueue() { - LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue)); +/* 构造函数 */ +linkedListQueue *newLinkedListQueue() { + linkedListQueue *queue = (linkedListQueue *)malloc(sizeof(linkedListQueue)); queue->front = NULL; queue->rear = NULL; queue->queSize = 0; return queue; } -/* 析构方法 */ -void delLinkedListQueue(LinkedListQueue *queue) { +/* 析构函数 */ +void delLinkedListQueue(linkedListQueue *queue) { // 释放所有节点 for (int i = 0; i < queue->queSize && queue->front != NULL; i++) { ListNode *tmp = queue->front; @@ -36,17 +36,17 @@ void delLinkedListQueue(LinkedListQueue *queue) { } /* 获取队列的长度 */ -int size(LinkedListQueue *queue) { +int size(linkedListQueue *queue) { return queue->queSize; } /* 判断队列是否为空 */ -bool empty(LinkedListQueue *queue) { +bool empty(linkedListQueue *queue) { return (size(queue) == 0); } /* 入队 */ -void push(LinkedListQueue *queue, int num) { +void push(linkedListQueue *queue, int num) { // 尾节点处添加 node ListNode *node = newListNode(num); // 如果队列为空,则令头、尾节点都指向该节点 @@ -63,13 +63,13 @@ void push(LinkedListQueue *queue, int num) { } /* 访问队首元素 */ -int peek(LinkedListQueue *queue) { +int peek(linkedListQueue *queue) { assert(size(queue) && queue->front); return queue->front->val; } /* 出队 */ -void pop(LinkedListQueue *queue) { +void pop(linkedListQueue *queue) { int num = peek(queue); ListNode *tmp = queue->front; queue->front = queue->front->next; @@ -78,7 +78,7 @@ void pop(LinkedListQueue *queue) { } /* 打印队列 */ -void printLinkedListQueue(LinkedListQueue *queue) { +void printLinkedListQueue(linkedListQueue *queue) { int arr[queue->queSize]; // 拷贝链表中的数据到数组 int i; @@ -93,7 +93,7 @@ void printLinkedListQueue(LinkedListQueue *queue) { /* Driver Code */ int main() { /* 初始化队列 */ - LinkedListQueue *queue = newLinkedListQueue(); + linkedListQueue *queue = newLinkedListQueue(); /* 元素入队 */ push(queue, 1); diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c index 34b1d87b..92872b55 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_stack.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -14,7 +14,7 @@ struct linkedListStack { typedef struct linkedListStack linkedListStack; -/* 构造方法 */ +/* 构造函数 */ linkedListStack *newLinkedListStack() { linkedListStack *s = malloc(sizeof(linkedListStack)); s->top = NULL; @@ -22,7 +22,7 @@ linkedListStack *newLinkedListStack() { return s; } -/* 析构方法 */ +/* 析构函数 */ void delLinkedListStack(linkedListStack *s) { while (s->top) { ListNode *n = s->top->next; @@ -80,7 +80,6 @@ int pop(linkedListStack *s) { /* Driver Code */ int main() { /* 初始化栈 */ - // 构造方法 linkedListStack *stack = newLinkedListStack(); /* 元素入栈 */ diff --git a/codes/c/chapter_tree/avl_tree.c b/codes/c/chapter_tree/avl_tree.c index f9714322..4222482d 100644 --- a/codes/c/chapter_tree/avl_tree.c +++ b/codes/c/chapter_tree/avl_tree.c @@ -7,15 +7,15 @@ #include "../include/include.h" /* AVL Tree */ -struct avlTree { +struct aVLTree { TreeNode *root; }; -typedef struct avlTree avlTree; +typedef struct aVLTree aVLTree; /* 构建 AVL 树 */ -avlTree *newAVLTree() { - avlTree *tree = (avlTree *)malloc(sizeof(avlTree)); +aVLTree *newAVLTree() { + aVLTree *tree = (aVLTree *)malloc(sizeof(aVLTree)); tree->root = NULL; return tree; } @@ -134,7 +134,7 @@ TreeNode *insertHelper(TreeNode *node, int val) { } /* 插入节点 */ -void insert(avlTree *tree, int val) { +void insert(aVLTree *tree, int val) { tree->root = insertHelper(tree->root, val); } @@ -183,12 +183,12 @@ TreeNode *removeHelper(TreeNode *node, int val) { /* 删除节点 */ // 由于引入了 stdio.h ,此处无法使用 remove 关键词 -void removeNode(avlTree *tree, int val) { +void removeNode(aVLTree *tree, int val) { TreeNode *root = removeHelper(tree->root, val); } /* 查找节点 */ -TreeNode *search(avlTree *tree, int val) { +TreeNode *search(aVLTree *tree, int val) { TreeNode *cur = tree->root; // 循环查找,越过叶节点后跳出 while (cur != NULL) { @@ -207,13 +207,13 @@ TreeNode *search(avlTree *tree, int val) { return cur; } -void testInsert(avlTree *tree, int val) { +void testInsert(aVLTree *tree, int val) { insert(tree, val); printf("\n插入节点 %d 后,AVL 树为 \n", val); printTree(tree->root); } -void testRemove(avlTree *tree, int val) { +void testRemove(aVLTree *tree, int val) { removeNode(tree, val); printf("\n删除节点 %d 后,AVL 树为 \n", val); printTree(tree->root); @@ -222,7 +222,7 @@ void testRemove(avlTree *tree, int val) { /* Driver Code */ int main() { /* 初始化空 AVL 树 */ - avlTree *tree = (avlTree *)newAVLTree(); + aVLTree *tree = (aVLTree *)newAVLTree(); /* 插入节点 */ // 请关注插入节点后,AVL 树是如何保持平衡的 testInsert(tree, 1); diff --git a/codes/c/include/tree_node.h b/codes/c/include/tree_node.h index 5e0c1373..37816d3e 100644 --- a/codes/c/include/tree_node.h +++ b/codes/c/include/tree_node.h @@ -15,11 +15,12 @@ extern "C" { #define MAX_NODE_SIZE 5000 +/* 二叉树节点结构体 */ struct TreeNode { - int val; - int height; - struct TreeNode *left; - struct TreeNode *right; + int val; // 节点值 + int height; // 节点高度 + struct TreeNode *left; // 左子节点指针 + struct TreeNode *right; // 右子节点指针 }; typedef struct TreeNode TreeNode; @@ -60,7 +61,7 @@ TreeNode *arrToTree(const int *arr, size_t size) { node = queue[front++]; index++; if (index < size) { - // represent null with INT_MAX + // represent null with INT_MAX if (arr[index] != INT_MAX) { node->left = newTreeNode(arr[index]); queue[rear++] = node->left; diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index a85dccff..a777c13e 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -95,10 +95,9 @@ struct ListNode *next; // 指向下一节点的指针(引用) }; - // typedef 作用是为一种数据类型定义一个新名字 typedef struct ListNode ListNode; - /* 构造函数,初始化一个新节点 */ + /* 构造函数 */ ListNode *newListNode(int val) { ListNode *node, *next; node = (ListNode *) malloc(sizeof(ListNode)); @@ -379,7 +378,7 @@ === "C" ```c title="linked_list.c" - [class]{}-[func]{insertNode} + [class]{}-[func]{insert} ``` === "C#" @@ -573,7 +572,7 @@ === "C" ```c title="linked_list.c" - [class]{}-[func]{findNode} + [class]{}-[func]{find} ``` === "C#" @@ -692,7 +691,24 @@ === "C" ```c title="" + /* 双向链表节点结构体 */ + struct ListNode { + int val; // 节点值 + struct ListNode *next; // 指向后继节点的指针(引用) + struct ListNode *prev; // 指向前驱节点的指针(引用) + }; + typedef struct ListNode ListNode; + + /* 构造函数 */ + ListNode *newListNode(int val) { + ListNode *node, *next; + node = (ListNode *) malloc(sizeof(ListNode)); + node->val = val; + node->next = NULL; + node->prev = NULL; + return node; + } ``` === "C#" diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index dd49a38b..c919dc00 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -73,7 +73,7 @@ === "C" ```c title="list.c" - + // C 未提供内置动态数组 ``` === "C#" @@ -171,7 +171,7 @@ === "C" ```c title="list.c" - + // C 未提供内置动态数组 ``` === "C#" @@ -329,7 +329,7 @@ === "C" ```c title="list.c" - + // C 未提供内置动态数组 ``` === "C#" @@ -491,7 +491,7 @@ === "C" ```c title="list.c" - + // C 未提供内置动态数组 ``` === "C#" @@ -599,7 +599,7 @@ === "C" ```c title="list.c" - + // C 未提供内置动态数组 ``` === "C#" @@ -675,7 +675,7 @@ === "C" ```c title="list.c" - + // C 未提供内置动态数组 ``` === "C#" diff --git a/docs/chapter_binary_search/binary_search.md b/docs/chapter_binary_search/binary_search.md index 23d762a2..adbfa0cc 100755 --- a/docs/chapter_binary_search/binary_search.md +++ b/docs/chapter_binary_search/binary_search.md @@ -157,7 +157,10 @@ $$ === "C" ```c title="" - + // (i + j) 有可能超出 int 的取值范围 + int m = (i + j) / 2; + // 更换为此写法则不会越界 + int m = i + (j - i) / 2; ``` === "C#" diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index ad42dbef..9df8e3ee 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -182,7 +182,18 @@ === "C" ```c title="" + /* 函数 */ + int func() { + // do something... + return 0; + } + int algorithm(int n) { // 输入数据 + const int a = 0; // 暂存数据(常量) + int b = 0; // 暂存数据(变量) + int c = func(); // 栈帧空间(调用函数) + return a + b + c; // 输出数据 + } ``` === "C#" @@ -329,7 +340,12 @@ === "C" ```c title="" - + void algorithm(int n) { + int a = 0; // O(1) + int b[10000]; // O(1) + if (n > 10) + vector nums(n); // O(n) + } ``` === "C#" @@ -491,7 +507,21 @@ === "C" ```c title="" - + int func() { + // do something + return 0; + } + /* 循环 O(1) */ + void loop(int n) { + for (int i = 0; i < n; i++) { + func(); + } + } + /* 递归 O(n) */ + void recur(int n) { + if (n == 1) return; + return recur(n - 1); + } ``` === "C#" @@ -611,7 +641,7 @@ $$ === "C" ```c title="space_complexity.c" - [class]{}-[func]{spaceConstant} + [class]{}-[func]{constant} ``` === "C#" @@ -675,7 +705,7 @@ $$ === "C" ```c title="space_complexity.c" - [class]{}-[func]{spaceLinear} + [class]{}-[func]{linear} ``` === "C#" @@ -737,7 +767,7 @@ $$ === "C" ```c title="space_complexity.c" - [class]{}-[func]{spaceLinearRecur} + [class]{}-[func]{linearRecur} ``` === "C#" @@ -803,7 +833,7 @@ $$ === "C" ```c title="space_complexity.c" - [class]{}-[func]{spaceQuadratic} + [class]{}-[func]{quadratic} ``` === "C#" @@ -865,7 +895,7 @@ $$ === "C" ```c title="space_complexity.c" - [class]{}-[func]{spaceQuadraticRecur} + [class]{}-[func]{quadraticRecur} ``` === "C#" diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 35acbd22..df063edd 100755 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -168,7 +168,7 @@ === "C" ```c title="hash_map.c" - + // C 未提供内置哈希表 ``` === "C#" @@ -333,7 +333,7 @@ === "C" ```c title="hash_map.c" - + // C 未提供内置哈希表 ``` === "C#" diff --git a/docs/chapter_heap/heap.md b/docs/chapter_heap/heap.md index 0179c1c5..eedaa117 100644 --- a/docs/chapter_heap/heap.md +++ b/docs/chapter_heap/heap.md @@ -249,7 +249,7 @@ === "C" ```c title="heap.c" - + // C 未提供内置 Heap 类 ``` === "C#" diff --git a/docs/chapter_sorting/merge_sort.md b/docs/chapter_sorting/merge_sort.md index db9c07fd..a643ad8c 100755 --- a/docs/chapter_sorting/merge_sort.md +++ b/docs/chapter_sorting/merge_sort.md @@ -102,7 +102,9 @@ === "C" ```c title="merge_sort.c" + [class]{}-[func]{merge} + [class]{}-[func]{mergeSort} ``` === "C#" diff --git a/docs/chapter_sorting/quick_sort.md b/docs/chapter_sorting/quick_sort.md index d16dcab4..72bbd941 100755 --- a/docs/chapter_sorting/quick_sort.md +++ b/docs/chapter_sorting/quick_sort.md @@ -88,7 +88,7 @@ === "C" ```c title="quick_sort.c" - [class]{quickSort}-[func]{partition} + [class]{}-[func]{partition} ``` === "C#" @@ -162,7 +162,7 @@ === "C" ```c title="quick_sort.c" - [class]{quickSort}-[func]{quickSort} + [class]{}-[func]{quickSort} ``` === "C#" @@ -262,9 +262,9 @@ === "C" ```c title="quick_sort.c" - [class]{quickSortMedian}-[func]{medianThree} + [class]{}-[func]{medianThree} - [class]{quickSortMedian}-[func]{partition} + [class]{}-[func]{partitionMedian} ``` === "C#" @@ -336,7 +336,7 @@ === "C" ```c title="quick_sort.c" - [class]{quickSortTailCall}-[func]{quickSort} + [class]{}-[func]{quickSortTailCall} ``` === "C#" diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index d4eff463..8d2576c8 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -215,7 +215,7 @@ === "C" ```c title="deque.c" - + // C 未提供内置双向队列 ``` === "C#" @@ -361,9 +361,9 @@ === "C" ```c title="linkedlist_deque.c" - [class]{ListNode}-[func]{} + [class]{doublyListNode}-[func]{} - [class]{LinkedListDeque}-[func]{} + [class]{linkedListDeque}-[func]{} ``` === "C#" @@ -450,7 +450,7 @@ === "C" ```c title="array_deque.c" - [class]{ArrayDeque}-[func]{} + [class]{arrayDeque}-[func]{} ``` === "C#" diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index 1ab17f2e..28677e5e 100755 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -189,7 +189,7 @@ === "C" ```c title="queue.c" - + // C 未提供内置队列 ``` === "C#" diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 56a8eb8c..da852436 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -188,7 +188,7 @@ === "C" ```c title="stack.c" - + // C 未提供内置栈 ``` === "C#" diff --git a/docs/chapter_tree/array_representation_of_tree.md b/docs/chapter_tree/array_representation_of_tree.md index 80657f49..704e6144 100644 --- a/docs/chapter_tree/array_representation_of_tree.md +++ b/docs/chapter_tree/array_representation_of_tree.md @@ -34,9 +34,8 @@ ```cpp title="" /* 二叉树的数组表示 */ - // 为了符合数据类型为 int ,使用 int 最大值标记空位 - // 该方法的使用前提是没有节点的值 = INT_MAX - vector tree = { 1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15 }; + // 使用 int 最大值标记空位,因此要求节点值不能为 INT_MAX + vector tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15}; ``` === "Python" @@ -74,7 +73,9 @@ === "C" ```c title="" - + /* 二叉树的数组表示 */ + // 使用 int 最大值标记空位,因此要求节点值不能为 INT_MAX + int tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15}; ``` === "C#" diff --git a/docs/chapter_tree/avl_tree.md b/docs/chapter_tree/avl_tree.md index e8e7adf4..a8e5b091 100644 --- a/docs/chapter_tree/avl_tree.md +++ b/docs/chapter_tree/avl_tree.md @@ -62,7 +62,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit === "Go" ```go title="" - /* AVL 树节点类 */ + /* AVL 树节点结构体 */ type TreeNode struct { Val int // 节点值 Height int // 节点高度 @@ -74,6 +74,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit === "JavaScript" ```javascript title="" + /* AVL 树节点类 */ class TreeNode { val; // 节点值 height; //节点高度 @@ -91,6 +92,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit === "TypeScript" ```typescript title="" + /* AVL 树节点类 */ class TreeNode { val: number; // 节点值 height: number; // 节点高度 @@ -108,7 +110,27 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit === "C" ```c title="" + /* AVL 树节点结构体 */ + struct TreeNode { + int val; + int height; + struct TreeNode *left; + struct TreeNode *right; + }; + typedef struct TreeNode TreeNode; + + /* 构造函数 */ + TreeNode *newTreeNode(int val) { + TreeNode *node; + + node = (TreeNode *)malloc(sizeof(TreeNode)); + node->val = val; + node->height = 0; + node->left = NULL; + node->right = NULL; + return node; + } ``` === "C#" @@ -200,9 +222,9 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit === "C" ```c title="avl_tree.c" - [class]{aVLTree}-[func]{height} + [class]{}-[func]{height} - [class]{aVLTree}-[func]{updateHeight} + [class]{}-[func]{updateHeight} ``` === "C#" @@ -272,7 +294,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit === "C" ```c title="avl_tree.c" - [class]{aVLTree}-[func]{balanceFactor} + [class]{}-[func]{balanceFactor} ``` === "C#" @@ -364,7 +386,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉 === "C" ```c title="avl_tree.c" - [class]{aVLTree}-[func]{rightRotate} + [class]{}-[func]{rightRotate} ``` === "C#" @@ -436,7 +458,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉 === "C" ```c title="avl_tree.c" - [class]{aVLTree}-[func]{leftRotate} + [class]{}-[func]{leftRotate} ``` === "C#" @@ -529,7 +551,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉 === "C" ```c title="avl_tree.c" - [class]{aVLTree}-[func]{rotate} + [class]{}-[func]{rotate} ``` === "C#" @@ -609,7 +631,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉 ```c title="avl_tree.c" [class]{aVLTree}-[func]{insert} - [class]{aVLTree}-[func]{insertHelper} + [class]{}-[func]{insertHelper} ``` === "C#" @@ -691,9 +713,9 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉 === "C" ```c title="avl_tree.c" - [class]{aVLTree}-[func]{remove} + [class]{aVLTree}-[func]{removeNode} - [class]{aVLTree}-[func]{removeHelper} + [class]{}-[func]{removeHelper} ``` === "C#" diff --git a/docs/chapter_tree/binary_search_tree.md b/docs/chapter_tree/binary_search_tree.md index 731883ab..0b869fb8 100755 --- a/docs/chapter_tree/binary_search_tree.md +++ b/docs/chapter_tree/binary_search_tree.md @@ -237,7 +237,7 @@ === "C" ```c title="binary_search_tree.c" - [class]{binarySearchTree}-[func]{remove} + [class]{binarySearchTree}-[func]{removeNode} ``` === "C#" diff --git a/docs/chapter_tree/binary_tree.md b/docs/chapter_tree/binary_tree.md index 5c894105..eb861598 100644 --- a/docs/chapter_tree/binary_tree.md +++ b/docs/chapter_tree/binary_tree.md @@ -87,7 +87,27 @@ === "C" ```c title="" - + /* 二叉树节点结构体 */ + struct TreeNode { + int val; // 节点值 + int height; // 节点高度 + struct TreeNode *left; // 左子节点指针 + struct TreeNode *right; // 右子节点指针 + }; + + typedef struct TreeNode TreeNode; + + /* 构造函数 */ + TreeNode *newTreeNode(int val) { + TreeNode *node; + + node = (TreeNode *)malloc(sizeof(TreeNode)); + node->val = val; + node->height = 0; + node->left = NULL; + node->right = NULL; + return node; + } ``` === "C#" @@ -256,7 +276,18 @@ === "C" ```c title="binary_tree.c" - + /* 初始化二叉树 */ + // 初始化节点 + TreeNode *n1 = newTreeNode(1); + TreeNode *n2 = newTreeNode(2); + TreeNode *n3 = newTreeNode(3); + TreeNode *n4 = newTreeNode(4); + TreeNode *n5 = newTreeNode(5); + // 构建引用指向(即指针) + n1->left = n2; + n1->right = n3; + n2->left = n4; + n2->right = n5; ``` === "C#" @@ -376,7 +407,13 @@ === "C" ```c title="binary_tree.c" - + /* 插入与删除节点 */ + TreeNode *P = newTreeNode(0); + // 在 n1 -> n2 中间插入节点 P + n1->left = P; + P->left = n2; + // 删除节点 P + n1->left = n2; ``` === "C#"