Rename the common modules in Java, C++ and C.

This commit is contained in:
krahets
2023-04-24 04:11:18 +08:00
parent c6eecfd0dc
commit 145975b335
120 changed files with 122 additions and 380 deletions
+5
View File
@@ -0,0 +1,5 @@
add_executable(utils
common_test.c
common.h print_util.h
list_node.h tree_node.h
uthash.h)
+32
View File
@@ -0,0 +1,32 @@
/**
* File: common.h
* Created Time: 2022-12-20
* Author: MolDuM (moldum@163.com)、Reanon (793584285@qq.com)
*/
#ifndef C_INCLUDE_H
#define C_INCLUDE_H
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "list_node.h"
#include "print_util.h"
#include "tree_node.h"
// hash table lib
#include "uthash.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif // C_INCLUDE_H
+38
View File
@@ -0,0 +1,38 @@
/**
* File: include_test.c
* Created Time: 2023-01-10
* Author: Reanon (793584285@qq.com)
*/
#include "common.h"
void testListNode() {
int nums[] = {2, 3, 5, 6, 7};
int size = sizeof(nums) / sizeof(int);
ListNode *head = arrToLinkedList(nums, size);
printLinkedList(head);
ListNode *node = getListNode(head, 5);
printf("find node: %d\n", node->val);
}
void testTreeNode() {
int nums[] = {1, 2, 3, INT_MAX, 5, 6, INT_MAX};
int size = sizeof(nums) / sizeof(int);
TreeNode *root = arrToTree(nums, size);
// print tree
printTree(root);
// tree to arr
int *arr = treeToArr(root);
printArray(arr, size);
}
int main(int argc, char *argv[]) {
printf("==testListNode==\n");
testListNode();
printf("==testTreeNode==\n");
testTreeNode();
return 0;
}
+58
View File
@@ -0,0 +1,58 @@
/**
* File: list_node.h
* Created Time: 2023-01-09
* Author: Reanon (793584285@qq.com)
*/
#ifndef LIST_NODE_H
#define LIST_NODE_H
#ifdef __cplusplus
extern "C" {
#endif
/* 链表节点结构体 */
struct ListNode {
int val; // 节点值
struct ListNode *next; // 指向下一节点的指针(引用)
};
// typedef 作用是为一种数据类型定义一个新名字
typedef struct ListNode ListNode;
/* 构造函数,初始化一个新节点 */
ListNode *newListNode(int val) {
ListNode *node, *next;
node = (ListNode *)malloc(sizeof(ListNode));
node->val = val;
node->next = NULL;
return node;
}
/* Generate a linked list with a vector */
ListNode *arrToLinkedList(const int *arr, size_t size) {
if (size <= 0) {
return NULL;
}
ListNode *dummy = newListNode(0);
ListNode *node = dummy;
for (int i = 0; i < size; i++) {
node->next = newListNode(arr[i]);
node = node->next;
}
return dummy->next;
}
/* Get a list node with specific value from a linked list */
ListNode *getListNode(ListNode *head, int val) {
while (head != NULL && head->val != val) {
head = head->next;
}
return head;
}
#ifdef __cplusplus
}
#endif
#endif // LIST_NODE_H
+125
View File
@@ -0,0 +1,125 @@
/**
* File: print_util.h
* Created Time: 2022-12-21
* Author: MolDum (moldum@163.com)、Reanon (793584285@qq.com)
*/
#ifndef PRINT_UTIL_H
#define PRINT_UTIL_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list_node.h"
#include "tree_node.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Print an Array */
static void printArray(int arr[], int size) {
printf("[");
if (arr != NULL && size != 0) {
for (int i = 0; i < size - 1; i++) {
if (arr[i] != INT_MAX) {
printf("%d, ", arr[i]);
} else {
printf("NULL, ");
}
}
if (arr[size - 1] != INT_MAX) {
printf("%d]\n", arr[size - 1]);
} else {
printf("NULL]\n");
}
} else {
printf("]");
}
}
/* Print a linked list */
static void printLinkedList(ListNode *node) {
if (node == NULL) {
return;
}
while (node->next != NULL) {
printf("%d -> ", node->val);
node = node->next;
}
printf("%d\n", node->val);
}
struct Trunk {
struct Trunk *prev;
char *str;
};
typedef struct Trunk Trunk;
Trunk *newTrunk(Trunk *prev, char *str) {
Trunk *trunk = (Trunk *)malloc(sizeof(Trunk));
trunk->prev = prev;
trunk->str = (char *)malloc(sizeof(char) * 10);
strcpy(trunk->str, str);
return trunk;
}
/* Helper function to print branches of the binary tree */
void showTrunks(Trunk *trunk) {
if (trunk == NULL) {
return;
}
showTrunks(trunk->prev);
printf("%s", trunk->str);
}
/* Help to print a binary tree, hide more details */
static void printTreeHelper(TreeNode *node, Trunk *prev, bool isLeft) {
if (node == NULL) {
return;
}
char *prev_str = " ";
Trunk *trunk = newTrunk(prev, prev_str);
printTreeHelper(node->right, trunk, true);
if (prev == NULL) {
trunk->str = "———";
} else if (isLeft) {
trunk->str = "/———";
prev_str = " |";
} else {
trunk->str = "\\———";
prev->str = prev_str;
}
showTrunks(trunk);
printf("%d\n", node->val);
if (prev != NULL) {
prev->str = prev_str;
}
trunk->str = " |";
printTreeHelper(node->left, trunk, false);
}
/* Print a binary tree */
static void printTree(TreeNode *root) {
printTreeHelper(root, NULL, false);
}
/* Print a Heap */
static void printHeap(int arr[], int size) {
TreeNode *root;
printf("堆的数组表示:");
printArray(arr, size);
printf("堆的树状表示:\n");
root = arrToTree(arr, size);
printTree(root);
}
#ifdef __cplusplus
}
#endif
#endif // PRINT_UTIL_H
+128
View File
@@ -0,0 +1,128 @@
/**
* File: tree_node.h
* Created Time: 2023-01-09
* Author: Reanon (793584285@qq.com)
*/
#ifndef TREE_NODE_H
#define TREE_NODE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <limits.h>
#define MAX_NODE_SIZE 5000
/* 二叉树节点结构体 */
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;
}
/* Generate a binary tree with an array */
TreeNode *arrToTree(const int *arr, size_t size) {
if (size <= 0) {
return NULL;
}
int front, rear, index;
TreeNode *root, *node;
TreeNode **queue;
/* 根节点 */
root = newTreeNode(arr[0]);
/* 辅助队列 */
queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
// 队列指针
front = 0, rear = 0;
// 将根节点放入队尾
queue[rear++] = root;
// 记录遍历数组的索引
index = 0;
while (front < rear) {
// 取队列中的头节点,并让头节点出队
node = queue[front++];
index++;
if (index < size) {
// represent null with INT_MAX
if (arr[index] != INT_MAX) {
node->left = newTreeNode(arr[index]);
queue[rear++] = node->left;
}
}
index++;
if (index < size) {
if (arr[index] != INT_MAX) {
node->right = newTreeNode(arr[index]);
queue[rear++] = node->right;
}
}
}
return root;
}
/* Generate a binary tree with an array */
int *treeToArr(TreeNode *root) {
if (root == NULL) {
return NULL;
}
int front, rear;
int index, *arr;
TreeNode *node;
TreeNode **queue;
/* 辅助队列 */
queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
// 队列指针
front = 0, rear = 0;
// 将根节点放入队尾
queue[rear++] = root;
/* 辅助数组 */
arr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
// 数组指针
index = 0;
while (front < rear) {
// 取队列中的头节点,并让头节点出队
node = queue[front++];
if (node != NULL) {
arr[index] = node->val;
queue[rear++] = node->left;
queue[rear++] = node->right;
} else {
arr[index] = INT_MAX;
}
index++;
}
return arr;
}
/* Free the memory allocated to a tree */
void freeMemoryTree(TreeNode *root) {
if (root == NULL)
return;
freeMemoryTree(root->left);
freeMemoryTree(root->right);
// 释放内存
free(root);
}
#ifdef __cplusplus
}
#endif
#endif // TREE_NODE_H
File diff suppressed because it is too large Load Diff