Skip to content

Commit

Permalink
Harmonize comments in utils between multiple programming languages
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Mar 30, 2024
1 parent 3050f33 commit a7d3eab
Show file tree
Hide file tree
Showing 29 changed files with 107 additions and 235 deletions.
3 changes: 0 additions & 3 deletions codes/c/utils/common_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ void testListNode() {
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() {
Expand Down
12 changes: 2 additions & 10 deletions codes/c/utils/list_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ListNode *newListNode(int val) {
return node;
}

/* Generate a linked list with an array */
/* 将数组反序列化为链表 */
ListNode *arrToLinkedList(const int *arr, size_t size) {
if (size <= 0) {
return NULL;
Expand All @@ -41,15 +41,7 @@ ListNode *arrToLinkedList(const int *arr, size_t size) {
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;
}

/* Free the memory allocated to a linked list */
/* 释放分配给链表的内存空间 */
void freeMemoryLinkedList(ListNode *cur) {
// 释放内存
ListNode *pre;
Expand Down
17 changes: 10 additions & 7 deletions codes/c/utils/print_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
extern "C" {
#endif

/* Print an Array */
/* 打印数组 */
void printArray(int arr[], int size) {
if (arr == NULL || size == 0) {
printf("[]");
Expand All @@ -31,7 +31,7 @@ void printArray(int arr[], int size) {
printf("%d]\n", arr[size - 1]);
}

/* Print an Array */
/* 打印数组 */
void printArrayFloat(float arr[], int size) {
if (arr == NULL || size == 0) {
printf("[]");
Expand All @@ -44,7 +44,7 @@ void printArrayFloat(float arr[], int size) {
printf("%.2f]\n", arr[size - 1]);
}

/* Print a linked list */
/* 打印链表 */
void printLinkedList(ListNode *node) {
if (node == NULL) {
return;
Expand All @@ -69,7 +69,6 @@ Trunk *newTrunk(Trunk *prev, char *str) {
return trunk;
}

/* Helper function to print branches of the binary tree */
void showTrunks(Trunk *trunk) {
if (trunk == NULL) {
return;
Expand All @@ -78,7 +77,11 @@ void showTrunks(Trunk *trunk) {
printf("%s", trunk->str);
}

/* Help to print a binary tree, hide more details */
/**
* 打印二叉树
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
void printTreeHelper(TreeNode *node, Trunk *prev, bool isRight) {
if (node == NULL) {
return;
Expand Down Expand Up @@ -106,12 +109,12 @@ void printTreeHelper(TreeNode *node, Trunk *prev, bool isRight) {
printTreeHelper(node->left, trunk, false);
}

/* Print a binary tree */
/* 打印二叉树 */
void printTree(TreeNode *root) {
printTreeHelper(root, NULL, false);
}

/* Print a Heap */
/* 打印堆 */
void printHeap(int arr[], int size) {
TreeNode *root;
printf("堆的数组表示:");
Expand Down
14 changes: 3 additions & 11 deletions codes/cpp/utils/list_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

using namespace std;

/* Definition for a singly-linked list node */
/* 链表节点 */
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {
}
};

/* Generate a linked list with a vector */
/* 将列表反序列化为链表 */
ListNode *vecToLinkedList(vector<int> list) {
ListNode *dum = new ListNode(0);
ListNode *head = dum;
Expand All @@ -30,15 +30,7 @@ ListNode *vecToLinkedList(vector<int> list) {
return dum->next;
}

/* Get a list node with specific value from a linked list */
ListNode *getListNode(ListNode *head, int val) {
while (head != nullptr && head->val != val) {
head = head->next;
}
return head;
}

/* Free the memory allocated to a linked list */
/* 释放分配给链表的内存空间 */
void freeMemoryLinkedList(ListNode *cur) {
// 释放内存
ListNode *pre;
Expand Down
31 changes: 15 additions & 16 deletions codes/cpp/utils/print_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ string strRepeat(string str, int n) {
return os.str();
}

/* Print an Array */
/* 打印数组 */
template <typename T> void printArray(T *arr, int n) {
cout << "[";
for (int i = 0; i < n - 1; i++) {
Expand All @@ -61,20 +61,20 @@ template <typename T> string getVectorString(vector<T> &list) {
return "[" + strJoin(", ", list) + "]";
}

/* Print a vector */
/* 打印列表 */
template <typename T> void printVector(vector<T> list) {
cout << getVectorString(list) << '\n';
}

/* Print a vector matrix */
/* 打印矩阵 */
template <typename T> void printVectorMatrix(vector<vector<T>> &matrix) {
cout << "[" << '\n';
for (vector<T> &list : matrix)
cout << " " + getVectorString(list) + "," << '\n';
cout << "]" << '\n';
}

/* Print a linked list */
/* 打印链表 */
void printLinkedList(ListNode *head) {
vector<int> list;
while (head != nullptr) {
Expand All @@ -85,10 +85,6 @@ void printLinkedList(ListNode *head) {
cout << strJoin(" -> ", list) << '\n';
}

/**
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
struct Trunk {
Trunk *prev;
string str;
Expand All @@ -98,7 +94,6 @@ struct Trunk {
}
};

/* Helper function to print branches of the binary tree */
void showTrunks(Trunk *p) {
if (p == nullptr) {
return;
Expand All @@ -108,7 +103,11 @@ void showTrunks(Trunk *p) {
cout << p->str;
}

/* Print a binary tree */
/**
* 打印二叉树
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
void printTree(TreeNode *root, Trunk *prev, bool isRight) {
if (root == nullptr) {
return;
Expand Down Expand Up @@ -140,12 +139,12 @@ void printTree(TreeNode *root, Trunk *prev, bool isRight) {
printTree(root->left, &trunk, false);
}

/* The interface of the tree printer */
/* 打印二叉树 */
void printTree(TreeNode *root) {
printTree(root, nullptr, false);
}

/* Print a stack */
/* 打印栈 */
template <typename T> void printStack(stack<T> stk) {
// Reverse the input stack
stack<T> tmp;
Expand All @@ -167,7 +166,7 @@ template <typename T> void printStack(stack<T> stk) {
cout << "[" + s.str() + "]" << '\n';
}

/* Print a queue */
/* 打印队列 */
template <typename T> void printQueue(queue<T> queue) {
// Generate the string to print
ostringstream s;
Expand All @@ -183,7 +182,7 @@ template <typename T> void printQueue(queue<T> queue) {
cout << "[" + s.str() + "]" << '\n';
}

/* Print a deque */
/* 打印双向队列 */
template <typename T> void printDeque(deque<T> deque) {
// Generate the string to print
ostringstream s;
Expand All @@ -199,7 +198,7 @@ template <typename T> void printDeque(deque<T> deque) {
cout << "[" + s.str() + "]" << '\n';
}

/* Print a HashMap */
/* 打印哈希表 */
// 定义模板参数 TKey 和 TValue ,用于指定键值对的类型
template <typename TKey, typename TValue> void printHashMap(unordered_map<TKey, TValue> map) {
for (auto kv : map) {
Expand All @@ -217,7 +216,7 @@ template <typename T, typename S, typename C> S &Container(priority_queue<T, S,
return HackedQueue::Container(pq);
}

/* Print a Heap (PriorityQueue) */
/* 打印堆(优先队列) */
template <typename T, typename S, typename C> void printHeap(priority_queue<T, S, C> &heap) {
vector<T> vec = Container(heap);
cout << "堆的数组表示:";
Expand Down
12 changes: 2 additions & 10 deletions codes/csharp/utils/ListNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace hello_algo.utils;

/* Definition for a singly-linked list node */
/* 链表节点 */
public class ListNode(int x) {
public int val = x;
public ListNode? next;

/* Generate a linked list with an array */
/* 将数组反序列化为链表 */
public static ListNode? ArrToLinkedList(int[] arr) {
ListNode dum = new(0);
ListNode head = dum;
Expand All @@ -20,14 +20,6 @@ public class ListNode(int x) {
return dum.next;
}

/* Get a list node with specific value from a linked list */
public static ListNode? GetListNode(ListNode? head, int val) {
while (head != null && head.val != val) {
head = head.next;
}
return head;
}

public override string? ToString() {
List<string> list = [];
var head = this;
Expand Down
19 changes: 9 additions & 10 deletions codes/csharp/utils/PrintUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Trunk(Trunk? prev, string str) {
};

public static class PrintUtil {
/* Print a list */
/* 打印列表 */
public static void PrintList<T>(IList<T> list) {
Console.WriteLine("[" + string.Join(", ", list) + "]");
}
Expand All @@ -21,7 +21,7 @@ public static class PrintUtil {
return $"[ {string.Join(", ", list.Select(x => x?.ToString() ?? "null"))} ]";
}

/* Print a matrix (Array) */
/* 打印矩阵 (Array) */
public static void PrintMatrix<T>(T[][] matrix) {
Console.WriteLine("[");
foreach (T[] row in matrix) {
Expand All @@ -30,7 +30,7 @@ public static class PrintUtil {
Console.WriteLine("]");
}

/* Print a matrix (List) */
/* 打印矩阵 (List) */
public static void PrintMatrix<T>(List<List<T>> matrix) {
Console.WriteLine("[");
foreach (List<T> row in matrix) {
Expand All @@ -39,7 +39,7 @@ public static class PrintUtil {
Console.WriteLine("]");
}

/* Print a linked list */
/* 打印链表 */
public static void PrintLinkedList(ListNode? head) {
List<string> list = [];
while (head != null) {
Expand All @@ -50,15 +50,15 @@ public static class PrintUtil {
}

/**
* The interface of the tree printer
* 打印二叉树
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
public static void PrintTree(TreeNode? root) {
PrintTree(root, null, false);
}

/* Print a binary tree */
/* 打印二叉树 */
public static void PrintTree(TreeNode? root, Trunk? prev, bool isRight) {
if (root == null) {
return;
Expand Down Expand Up @@ -90,7 +90,6 @@ public static class PrintUtil {
PrintTree(root.left, trunk, false);
}

/* Helper function to print branches of the binary tree */
public static void ShowTrunks(Trunk? p) {
if (p == null) {
return;
Expand All @@ -100,14 +99,14 @@ public static class PrintUtil {
Console.Write(p.str);
}

/* Print a hash map */
/* 打印哈希表 */
public static void PrintHashMap<K, V>(Dictionary<K, V> map) where K : notnull {
foreach (var kv in map.Keys) {
Console.WriteLine(kv.ToString() + " -> " + map[kv]?.ToString());
}
}

/* Print a heap */
/* 打印堆 */
public static void PrintHeap(Queue<int> queue) {
Console.Write("堆的数组表示:");
List<int> list = [.. queue];
Expand All @@ -117,7 +116,7 @@ public static class PrintUtil {
PrintTree(tree);
}

/* Print a PriorityQueue */
/* 打印优先队列 */
public static void PrintHeap(PriorityQueue<int, int> queue) {
var newQueue = new PriorityQueue<int, int>(queue.UnorderedItems, queue.Comparer);
Console.Write("堆的数组表示:");
Expand Down
4 changes: 2 additions & 2 deletions codes/dart/utils/list_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
* Author: Jefferson (JeffersonHuang77@gmail.com)
*/

/* Definition for a singly-linked list node */
/* 链表节点 */
class ListNode {
int val;
ListNode? next;

ListNode(this.val, [this.next]);
}

/* Generate a linked list with a list */
/* 将列表反序列化为链表 */
ListNode? listToLinkedList(List<int> list) {
ListNode dum = ListNode(0);
ListNode? head = dum;
Expand Down
Loading

0 comments on commit a7d3eab

Please sign in to comment.