blob: 0a5925d7bb690ee89ea22921cdef176224b315c5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#include "leetcode.h"
void two_d_arr_init(struct two_d_arr *obj, int row_size, int col_size,
int arr[row_size][col_size]) {
obj->row_size = row_size;
obj->col_size = (int *)malloc(row_size * sizeof(int));
obj->arr = (int **)malloc(row_size * sizeof(int *));
for (int i = 0; i < row_size; i++) {
obj->col_size[i] = col_size;
obj->arr[i] = (int *)malloc(col_size * sizeof(int));
for (int j = 0; j < col_size; j++)
obj->arr[i][j] = arr[i][j];
}
}
void two_d_arr_free(struct two_d_arr *obj) {
for (int i = 0; i < obj->row_size; i++)
free(obj->arr[i]);
free(obj->arr);
free(obj->col_size);
}
struct ListNode *listnode_create(int val) {
struct ListNode *new = (struct ListNode *)malloc(sizeof(struct ListNode));
new->val = val;
new->next = NULL;
return new;
}
void listnode_print(struct ListNode *head) {
struct ListNode *curr = head;
while (curr != NULL) {
printf("%d ", curr->val);
curr = curr->next;
}
printf("\n");
}
void listnode_free(struct ListNode *head) {
struct ListNode *tmp;
while (head) {
tmp = head;
head = head->next;
free(tmp);
}
}
struct TreeNode *treenode_create(int val) {
struct TreeNode *new = (struct TreeNode *)malloc(sizeof(struct TreeNode));
new->val = val;
new->left = NULL;
new->right = NULL;
return new;
}
void treenode_print(struct TreeNode *root) {
if (!root)
return;
treenode_print(root->left);
printf("%d ", root->val);
treenode_print(root->right);
}
void treenode_free(struct TreeNode *root) {
if (!root)
return;
treenode_free(root->left);
treenode_free(root->right);
free(root);
}
struct Node *node_create(int val, int numChildren) {
struct Node *new = (struct Node *)malloc(sizeof(struct Node));
new->val = val;
new->numChildren = numChildren;
if (numChildren) {
new->children = (struct Node **)malloc(numChildren * sizeof(struct Node *));
for (int i = 0; i < numChildren; i++)
new->children[i] = NULL;
} else
new->children = NULL;
return new;
}
void node_print(struct Node *root, int depth) {
if (!root)
return;
printf("%d\n", root->val);
for (int i = 0; i < root->numChildren; i++)
node_print(root->children[i], depth + 1);
}
void node_free(struct Node *root) {
if (!root)
return;
for (int i = 0; i < root->numChildren; i++)
node_free(root->children[i]);
free(root->children);
free(root);
}
|