Format the C code in Clang-Format Style: Microsoft

This commit is contained in:
krahets
2023-04-17 21:13:15 +08:00
parent 1d6b7a5644
commit 9a98ff8a5e
46 changed files with 215 additions and 216 deletions

View File

@@ -7,7 +7,7 @@
#include "../include/include.h"
/* 随机返回一个数组元素 */
int randomAccess(int* nums, int size) {
int randomAccess(int *nums, int size) {
// 在区间 [0, size) 中随机抽取一个数字
int randomIndex = rand() % size;
// 获取并返回随机元素
@@ -16,9 +16,9 @@ int randomAccess(int* nums, int size) {
}
/* 扩展数组长度 */
int* extend(int* nums, int size, int enlarge) {
int *extend(int *nums, int size, int enlarge) {
// 初始化一个扩展长度后的数组
int* res = (int *)malloc(sizeof(int) * (size + enlarge));
int *res = (int *)malloc(sizeof(int) * (size + enlarge));
// 将原数组中的所有元素复制到新数组
for (int i = 0; i < size; i++) {
res[i] = nums[i];
@@ -32,7 +32,7 @@ int* extend(int* nums, int size, int enlarge) {
}
/* 在数组的索引 index 处插入元素 num */
void insert(int* nums, int size, int num, int index) {
void insert(int *nums, int size, int num, int index) {
// 把索引 index 以及之后的所有元素向后移动一位
for (int i = size - 1; i > index; i--) {
nums[i] = nums[i - 1];
@@ -42,7 +42,7 @@ void insert(int* nums, int size, int num, int index) {
}
/* 删除索引 index 处元素 */
void removeItem(int* nums, int size, int index) {
void removeItem(int *nums, int size, int index) {
// 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < size - 1; i++) {
nums[i] = nums[i + 1];
@@ -50,7 +50,7 @@ void removeItem(int* nums, int size, int index) {
}
/* 遍历数组 */
void traverse(int* nums, int size) {
void traverse(int *nums, int size) {
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < size; i++) {
@@ -59,7 +59,7 @@ void traverse(int* nums, int size) {
}
/* 在数组中查找指定元素 */
int find(int* nums, int size, int target) {
int find(int *nums, int size, int target) {
for (int i = 0; i < size; i++) {
if (nums[i] == target)
return i;
@@ -67,7 +67,6 @@ int find(int* nums, int size, int target) {
return -1;
}
/* Driver Code */
int main() {
/* 初始化数组 */
@@ -76,21 +75,21 @@ int main() {
printf("数组 arr = ");
printArray(arr, size);
int nums[5] = { 1, 3, 2, 5, 4 };
int nums[5] = {1, 3, 2, 5, 4};
printf("数组 nums = ");
printArray(nums, size);
/* 随机访问 */
int randomNum = randomAccess(nums, size);
printf("在 nums 中获取随机元素 %d", randomNum);
/* 长度扩展 */
int enlarge = 3;
int* res = extend(nums, size, enlarge);
int *res = extend(nums, size, enlarge);
size += enlarge;
printf("将数组长度扩展至 8 ,得到 nums = ");
printArray(res, size);
/* 插入元素 */
insert(res, size, 6, 3);
printf("在索引 3 处插入数字 6 ,得到 nums = ");
@@ -100,10 +99,10 @@ int main() {
removeItem(res, size, 2);
printf("删除索引 2 处的元素,得到 nums = ");
printArray(res, size);
/* 遍历数组 */
traverse(res, size);
/* 查找元素 */
int index = find(res, size, 3);
printf("在 res 中查找元素 3 ,得到索引 = %d\n", index);

View File

@@ -7,7 +7,7 @@
#include "../include/include.h"
/* 在链表的节点 n0 之后插入节点 P */
void insert(ListNode* n0, ListNode* P) {
void insert(ListNode *n0, ListNode *P) {
ListNode *n1 = n0->next;
P->next = n1;
n0->next = P;
@@ -16,7 +16,7 @@ void insert(ListNode* n0, ListNode* P) {
/* 删除链表的节点 n0 之后的首个节点 */
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888
void removeNode(ListNode* n0) {
void removeNode(ListNode *n0) {
if (!n0->next)
return;
// n0 -> P -> n1
@@ -28,7 +28,7 @@ void removeNode(ListNode* n0) {
}
/* 访问链表中索引为 index 的节点 */
ListNode* access(ListNode* head, int index) {
ListNode *access(ListNode *head, int index) {
while (head && head->next && index) {
head = head->next;
index--;
@@ -37,7 +37,7 @@ ListNode* access(ListNode* head, int index) {
}
/* 在链表中查找值为 target 的首个节点 */
int find(ListNode* head, int target) {
int find(ListNode *head, int target) {
int index = 0;
while (head) {
if (head->val == target)
@@ -48,22 +48,21 @@ int find(ListNode* head, int target) {
return -1;
}
/* Driver Code */
int main() {
/* 初始化链表 */
// 初始化各个节点
ListNode* n0 = newListNode(1);
ListNode* n1 = newListNode(3);
ListNode* n2 = newListNode(2);
ListNode* n3 = newListNode(5);
ListNode* n4 = newListNode(4);
// 初始化各个节点
ListNode *n0 = newListNode(1);
ListNode *n1 = newListNode(3);
ListNode *n2 = newListNode(2);
ListNode *n3 = newListNode(5);
ListNode *n4 = newListNode(4);
// 构建引用指向
n0->next = n1;
n1->next = n2;
n2->next = n3;
n3->next = n4;
printf("初始化的链表为\r\n");
printf("初始化的链表为\r\n");
printLinkedList(n0);
/* 插入节点 */
@@ -77,7 +76,7 @@ int main() {
printLinkedList(n0);
/* 访问节点 */
ListNode* node = access(n0, 3);
ListNode *node = access(n0, 3);
printf("链表中索引 3 处的节点的值 = %d\r\n", node->val);
/* 查找节点 */

View File

@@ -93,7 +93,7 @@ int removeNum(myList *list, int index) {
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;
// 拷贝旧数据到新数据
@@ -113,6 +113,7 @@ int *toArray(myList *list) {
return list->nums;
}
/* Driver Code */
int main() {
/* 初始化列表 */
myList *list = newMyList();
@@ -157,4 +158,6 @@ int main() {
/* 释放分配内存 */
delMyList(list);
return 0;
}