Modify the code style of C (#299)

* style(my_list): modify c codes.

* style(stack): modify c codes.

* style(hash): modify c codes.
pull/306/head
Reanon 2 years ago committed by GitHub
parent 3a2a9a996c
commit da405b579d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,57 +6,59 @@
#include "../include/include.h"
// 用数组实现 list
struct mylist {
int* nums; // 数组(存储列表元素)
struct myList {
int *nums; // 数组(存储列表元素)
int capacity; // 列表容量
int size; // 列表大小
int extendRatio; // 列表每次扩容的倍数
};
typedef struct mylist MyList;
typedef struct myList myList;
/* 前置声明 */
void extendCapacity(MyList *list);
void extendCapacity(myList *list);
/* 构造函数 */
void newMyList(MyList *list) {
myList *newMyList() {
myList *list = malloc(sizeof(myList));
list->capacity = 10;
list->nums = malloc(sizeof(int) * list->capacity);
list->size = 0;
list->extendRatio = 2;
return list;
}
/* 析构函数 */
void delMyList(MyList *list) {
list->size = 0;
void delMyList(myList *list) {
free(list->nums);
free(list);
}
/* 获取列表长度 */
int size(MyList *list) {
int size(myList *list) {
return list->size;
}
/* 获取列表容量 */
int capacity(MyList *list) {
int capacity(myList *list) {
return list->capacity;
}
/* 访问元素 */
int get(MyList *list, int index) {
int get(myList *list, int index) {
assert(index < list->size);
return list->nums[index];
}
/* 更新元素 */
void set(MyList *list, int index, int num) {
void set(myList *list, int index, int num) {
assert(index < list->size);
list->nums[index] = num;
}
/* 尾部添加元素 */
void add(MyList *list, int num) {
void add(myList *list, int num) {
if (size(list) == capacity(list)) {
extendCapacity(list); // 扩容
}
@ -65,10 +67,10 @@ void add(MyList *list, int num) {
}
/* 中间插入元素 */
void insert(MyList *list, int index, int num) {
void insert(myList *list, int index, int num) {
assert(index < size(list));
for (int i = size(list); i > index; --i) {
list->nums[i] = list->nums[i-1];
list->nums[i] = list->nums[i - 1];
}
list->nums[index] = num;
list->size++;
@ -77,25 +79,25 @@ void insert(MyList *list, int index, int num) {
/* 删除元素 */
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888
int removeNum(MyList *list, int index) {
int removeNum(myList *list, int index) {
assert(index < size(list));
int num = list->nums[index];
for (int i = index; i < size(list) - 1; i++) {
list->nums[i] = list->nums[i+1];
list->nums[i] = list->nums[i + 1];
}
list->size--;
return num;
}
/* 列表扩容 */
void extendCapacity(MyList *list) {
void extendCapacity(myList *list) {
// 先分配空间
int newCapacity = capacity(list) * list->extendRatio;
int *extend = (int *)malloc(sizeof(int) * newCapacity);
int *extend = (int *) malloc(sizeof(int) * newCapacity);
int *temp = list->nums;
// 拷贝旧数据到新数据
for(int i = 0; i < size(list); i++)
for (int i = 0; i < size(list); i++)
extend[i] = list->nums[i];
// 释放旧数据
@ -107,53 +109,52 @@ void extendCapacity(MyList *list) {
}
/* 将列表转换为 Array 用于打印 */
int* toArray(MyList *list) {
int *toArray(myList *list) {
return list->nums;
}
int main() {
/* 初始化列表 */
MyList list;
newMyList(&list);
myList *list = newMyList();
/* 尾部添加元素 */
add(&list, 1);
add(&list, 3);
add(&list, 2);
add(&list, 5);
add(&list, 4);
add(list, 1);
add(list, 3);
add(list, 2);
add(list, 5);
add(list, 4);
printf("列表 list = ");
printArray(toArray(&list), size(&list));
printf("容量 = %d ,长度 = %d\r\n", capacity(&list), size(&list));
printArray(toArray(list), size(list));
printf("容量 = %d ,长度 = %d\n", capacity(list), size(list));
/* 中间插入元素 */
insert(&list, 3, 6);
insert(list, 3, 6);
printf("在索引 3 处插入数字 6 ,得到 list = ");
printArray(toArray(&list), size(&list));
printArray(toArray(list), size(list));
/* 删除元素 */
removeNum(&list, 3);
removeNum(list, 3);
printf("删除索引 3 处的元素,得到 list = ");
printArray(toArray(&list), size(&list));
printArray(toArray(list), size(list));
/* 访问元素 */
int num = get(&list, 1);
printf("访问索引 1 处的元素,得到 num = %d\r\n", num);
int num = get(list, 1);
printf("访问索引 1 处的元素,得到 num = %d\n", num);
/* 更新元素 */
set(&list, 1, 0);
set(list, 1, 0);
printf("将索引 1 处的元素更新为 0 ,得到 list = ");
printArray(toArray(&list), size(&list));
printArray(toArray(list), size(list));
/* 测试扩容机制 */
for (int i = 0; i < 10; i++) {
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
add(&list, i);
add(list, i);
}
printf("扩容后的列表 list = ");
printArray(toArray(&list), size(&list));
printf("容量 = %d ,长度 = %d\r\n", capacity(&list), size(&list));
printArray(toArray(list), size(list));
printf("容量 = %d ,长度 = %d\n", capacity(list), size(list));
/* 析构函数,释放分配内存 */
delMyList(&list);
/* 释放分配内存 */
delMyList(list);
}

@ -1,2 +1,3 @@
add_executable(time_complexity time_complexity.c )
add_executable(worst_best_time_complexity worst_best_time_complexity.c)
add_executable(worst_best_time_complexity worst_best_time_complexity.c)
add_executable(leetcode_two_sum leetcode_two_sum.c)

@ -0,0 +1,85 @@
/**
* File: leetcode_two_sum.c
* Created Time: 2023-01-19
* Author: Reanon (793584285@qq.com)
*/
#include "../include/include.h"
/* 暴力解法 */
int *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {
for (int i = 0; i < numsSize; ++i) {
for (int j = i + 1; j < numsSize; ++j) {
if (nums[i] + nums[j] == target) {
int *ret = malloc(sizeof(int) * 2);
ret[0] = i, ret[1] = j;
*returnSize = 2;
return ret;
}
}
}
*returnSize = 0;
return NULL;
}
/* 哈希表 */
struct hashTable {
int key;
int val;
// 借助 LetCode 上常用的哈希表
UT_hash_handle hh;
};
typedef struct hashTable hashTable;
hashTable *find(hashTable *h, int key) {
hashTable *tmp;
HASH_FIND_INT(h, &key, tmp);
return tmp;
}
void insert(hashTable *h, int key, int val) {
hashTable *t = find(h, key);
if (t == NULL) {
hashTable *tmp = malloc(sizeof(hashTable));
tmp->key = key, tmp->val = val;
HASH_ADD_INT(h, key, tmp);
} else {
t->val = val;
}
}
int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
hashTable *hashtable = NULL;
for (int i = 0; i < numsSize; i++) {
hashTable *t = find(hashtable, target - nums[i]);
if (t != NULL) {
int *ret = malloc(sizeof(int) * 2);
ret[0] = t->val, ret[1] = i;
*returnSize = 2;
return ret;
}
insert(hashtable, nums[i], i);
}
*returnSize = 0;
return NULL;
}
int main() {
// ======= Test Case =======
int nums[] = {2, 7, 11, 15};
int target = 9;
// ====== Driver Code ======
int returnSize;
int *res = twoSumBruteForce(nums, sizeof(nums) / sizeof(int), target, &returnSize);
// 方法一
printf("方法一 res = ");
printArray(res, returnSize);
// 方法二
res = twoSumHashTable(nums, sizeof(nums) / sizeof(int), target, &returnSize);
printf("方法二 res = ");
printArray(res, returnSize);
return 0;
}

@ -63,7 +63,7 @@ int medianThree(int nums[], int left, int mid, int right) {
}
// 哨兵划分(三数取中值)
int QuickSortMedianPartition(int nums[], int left, int right) {
int quickSortMedianPartition(int nums[], int left, int right) {
// 选取三个候选元素的中位数
int med = medianThree(nums, left, (left + right) / 2, right);
// 将中位数交换至数组最左端
@ -87,7 +87,7 @@ void quickSortMedian(int nums[], int left, int right) {
if (left >= right)
return;
// 哨兵划分
int pivot = QuickSortMedianPartition(nums, left, right);
int pivot = quickSortMedianPartition(nums, left, right);
// 递归左子数组、右子数组
quickSortMedian(nums, left, pivot - 1);
quickSortMedian(nums, pivot + 1, right);

@ -6,118 +6,98 @@
#include "../include/include.h"
#define MAX_SIZE 5000
/* 基于数组实现的栈 */
struct ArrayStack {
int *stackTop;
struct arrayStack {
int *data;
int size;
int capacity;
};
typedef struct ArrayStack ArrayStack;
typedef struct arrayStack arrayStack;
/* 内部调用 */
/* 获取栈容量 */
static int capacity(ArrayStack *stk) {
return stk->capacity;
}
/* 栈自动扩容 */
static void extendCapacity(ArrayStack *stk) {
// 先分配空间
int newCapacity = capacity(stk) * 2;
int *extend = (int *)malloc(sizeof(int) * newCapacity);
int *temp = stk->stackTop;
// 拷贝旧数据到新数据
for(int i=0; i<stk->size; i++)
extend[i] = temp[i];
// 释放旧数据
free(temp);
// 更新新数据
stk->stackTop = extend;
stk->capacity = newCapacity;
}
/* 构造函数 */
void newArrayStack(ArrayStack *stk) {
stk->capacity = 10;
stk->size = 0;
stk->stackTop = (int *)malloc(sizeof(int) * stk->capacity);
}
/* 析构函数 */
void delArrayStack(ArrayStack *stk) {
stk->capacity = 0;
stk->size = 0;
free(stk->stackTop);
arrayStack *newArrayStack() {
arrayStack *s = malloc(sizeof(arrayStack));
// 初始化一个大容量,避免扩容
s->data = malloc(sizeof(int) * MAX_SIZE);
s->size = 0;
return s;
}
/* 获取栈的长度 */
int size(ArrayStack *stk) {
return stk->size;
int size(arrayStack *s) {
return s->size;
}
/* 判断栈是否为空 */
bool empty(ArrayStack *stk) {
return size(stk) == 0;
}
/* 访问栈顶元素 */
int top(ArrayStack *stk) {
return stk->stackTop[size(stk) - 1];
bool isEmpty(arrayStack *s) {
return s->size == 0;
}
/* 入栈 */
void push(ArrayStack *stk, int num) {
if (size(stk) == capacity(stk))
extendCapacity(stk); // 需要扩容
void push(arrayStack *s, int num) {
if (s->size == MAX_SIZE) {
printf("stack is full.\n");
return;
}
s->data[s->size] = num;
s->size++;
}
stk->stackTop[size(stk)] = num;
stk->size++;
/* 访问栈顶元素 */
int peek(arrayStack *s) {
if (s->size == 0) {
printf("stack is empty.\n");
return NIL;
}
return s->data[s->size - 1];
}
/* 出栈 */
void pop(ArrayStack *stk) {
int num = stk->stackTop[size(stk) - 1];
stk->size--;
int pop(arrayStack *s) {
if (s->size == 0) {
printf("stack is empty.\n");
return NIL;
}
int val = peek(s);
s->size--;
return val;
}
/* Driver Code */
int main() {
/* 初始化栈 */
ArrayStack stack;
newArrayStack(&stack);
arrayStack * stack = newArrayStack();
/* 元素入栈 */
push(&stack, 1);
push(&stack, 3);
push(&stack, 2);
push(&stack, 5);
push(&stack, 4);
push(stack, 1);
push(stack, 3);
push(stack, 2);
push(stack, 5);
push(stack, 4);
printf("栈 stack = ");
printArray(stack.stackTop, size(&stack));
printArray(stack->data, stack->size);
/* 访问栈顶元素 */
int stackTop = top(&stack);
printf("栈顶元素 top = %d\r\n", stackTop);
int val = peek(stack);
printf("栈顶元素 top = %d\n", val);
/* 元素出栈 */
pop(&stack);
printf("出栈元素 pop = %d, 出栈后 stack = ", stackTop);
printArray(stack.stackTop, size(&stack));
val = pop(stack);
printf("出栈元素 pop = %d,出栈后 stack = ", val);
printArray(stack->data, stack->size);
/* 获取栈的长度 */
int stackSize = size(&stack);
printf("栈的长度 size = %d\r\n", stackSize);
int size = stack->size;
printf("栈的长度 size = %d\n", size);
/* 判断是否为空 */
bool isEmpty = empty(&stack);
printf("栈是否为空 = %s\r\n", isEmpty ? "yes" : "no");
bool empty = isEmpty(stack);
printf("栈是否为空 = %s\n", empty ? "true" : "false");
// 释放内存
free(stack->data);
free(stack);
/* 析构函数 */
delArrayStack(&stack);
return 0;
}

@ -7,104 +7,109 @@
#include "../include/include.h"
/* 基于链表实现的栈 */
struct LinkedListStack {
ListNode* stackTop; // 将头结点作为栈顶
struct linkedListStack {
ListNode *top; // 将头结点作为栈顶
int size; // 栈的长度
};
typedef struct LinkedListStack LinkedListStack;
typedef struct linkedListStack linkedListStack;
/* 构造函数 */
void newLinkedListStack(LinkedListStack* stk) {
stk->stackTop = NULL;
stk->size = 0;
linkedListStack *newLinkedListStack() {
linkedListStack *s = malloc(sizeof(linkedListStack));
s->top = NULL;
s->size = 0;
return s;
}
/* 析构函数 */
void delLinkedListStack(LinkedListStack* stk) {
while(stk->stackTop) {
ListNode *n = stk->stackTop->next;
free(stk->stackTop);
stk->stackTop = n;
void delLinkedListStack(linkedListStack *s) {
while (s->top) {
ListNode *n = s->top->next;
free(s->top);
s->top = n;
}
stk->size = 0;
free(s);
}
/* 获取栈的长度 */
int size(LinkedListStack* stk) {
assert(stk);
return stk->size;
int size(linkedListStack *s) {
assert(s);
return s->size;
}
/* 判断栈是否为空 */
bool empty(LinkedListStack* stk) {
assert(stk);
return size(stk) == 0;
bool isEmpty(linkedListStack *s) {
assert(s);
return size(s) == 0;
}
/* 访问栈顶元素 */
int top(LinkedListStack* stk) {
assert(stk);
assert(size(stk) != 0);
return stk->stackTop->val;
int peek(linkedListStack *s) {
assert(s);
assert(size(s) != 0);
return s->top->val;
}
/* 入栈 */
void push(LinkedListStack* stk, int num) {
assert(stk);
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
node->next = stk->stackTop; // 更新新加结点指针域
node->val = num; // 更新新加结点数据域
stk->stackTop = node; // 更新栈顶
stk->size++; // 更新栈大小
void push(linkedListStack *s, int num) {
assert(s);
ListNode *node = (ListNode *) malloc(sizeof(ListNode));
node->next = s->top; // 更新新加结点指针域
node->val = num; // 更新新加结点数据域
s->top = node; // 更新栈顶
s->size++; // 更新栈大小
}
/* 出栈 */
void pop(LinkedListStack* stk) {
assert(stk);
int num = top(stk);
ListNode *tmp = stk->stackTop;
stk->stackTop = stk->stackTop->next;
int pop(linkedListStack *s) {
if (s->size == 0) {
printf("stack is empty.\n");
return NIL;
}
assert(s);
int val = peek(s);
ListNode *tmp = s->top;
s->top = s->top->next;
// 释放内存
free(tmp);
stk->size--;
s->size--;
return val;
}
/* Driver Code */
int main() {
/* 初始化栈 */
LinkedListStack stack;
/* 构造函数 */
newLinkedListStack(&stack);
// 构造函数
linkedListStack *stack = newLinkedListStack();
/* 元素入栈 */
push(&stack, 1);
push(&stack, 3);
push(&stack, 2);
push(&stack, 5);
push(&stack, 4);
push(stack, 1);
push(stack, 3);
push(stack, 2);
push(stack, 5);
push(stack, 4);
printf("栈 stack = ");
printLinkedList(stack.stackTop);
printLinkedList(stack->top);
/* 访问栈顶元素 */
int stackTop = top(&stack);
printf("栈顶元素 top = %d\r\n", stackTop);
int val = peek(stack);
printf("栈顶元素 top = %d\r\n", val);
/* 元素出栈 */
pop(&stack);
printf("出栈元素 pop = %d, 出栈后 stack = ", stackTop);
printLinkedList(stack.stackTop);
val = pop(stack);
printf("出栈元素 pop = %d, 出栈后 stack = ", val);
printLinkedList(stack->top);
/* 获取栈的长度 */
int stackSize = size(&stack);
printf("栈的长度 size = %d\r\n", stackSize);
printf("栈的长度 size = %d\n", size(stack));
/* 判断是否为空 */
bool isEmpty = empty(&stack);
printf("栈是否为空 = %s\r\n", isEmpty ? "yes" : "no");
bool empty = isEmpty(stack);
printf("栈是否为空 = %s\n", empty ? "true" : "false");
/* 析构函数 */
delLinkedListStack(&stack);
delLinkedListStack(stack);
return 0;
}

@ -1,4 +1,5 @@
add_executable(include
include_test.c
uthash.h
include.h print_util.h
list_node.h tree_node.h)

@ -14,6 +14,8 @@
#include <time.h>
#include <assert.h>
#include "uthash.h"
#include "list_node.h"
#include "tree_node.h"
#include "print_util.h"

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save