Skip to content

Commit 8229f94

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <[email protected]>
1 parent ec1138e commit 8229f94

File tree

13 files changed

+395
-419
lines changed

13 files changed

+395
-419
lines changed

0030_substring_with_concatenation_of_all_words/concatenation.c

+52-36
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,67 @@
33
#include <stdbool.h>
44
#include <string.h>
55

6+
67
#define container_of(ptr, type, member) \
78
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
89

910
#define list_entry(ptr, type, member) \
1011
container_of(ptr, type, member)
1112

12-
#define hlist_for_each(pos, head) \
13-
for (pos = (head)->first; pos; pos = pos->next)
14-
15-
struct hlist_node;
13+
#define list_for_each_entry(pos, head, member) \
14+
for (pos = list_entry((head)->next, typeof(*pos), member); \
15+
&(pos)->member != (head); \
16+
pos = list_entry((pos)->member.next, typeof(*pos), member))
1617

17-
struct hlist_head {
18-
struct hlist_node *first;
18+
struct list_head {
19+
struct list_head *next, *prev;
1920
};
2021

21-
struct hlist_node {
22-
struct hlist_node *next, **pprev;
22+
struct word_node {
23+
char *word;
24+
int index;
25+
struct list_head link;
2326
};
2427

25-
static inline void INIT_HLIST_HEAD(struct hlist_head *h) {
26-
h->first = NULL;
28+
static inline void INIT_LIST_HEAD(struct list_head *list)
29+
{
30+
list->next = list->prev = list;
2731
}
2832

29-
static inline int hlist_empty(struct hlist_head *h) {
30-
return !h->first;
33+
static inline int list_empty(const struct list_head *head)
34+
{
35+
return (head->next == head);
3136
}
3237

33-
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
38+
static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
3439
{
35-
if (h->first != NULL) {
36-
h->first->pprev = &n->next;
37-
}
38-
n->next = h->first;
39-
n->pprev = &h->first;
40-
h->first = n;
40+
next->prev = new;
41+
new->next = next;
42+
new->prev = prev;
43+
prev->next = new;
4144
}
4245

43-
struct word_node {
44-
struct hlist_node node;
45-
char *word;
46-
int index;
47-
};
46+
static inline void list_add(struct list_head *_new, struct list_head *head)
47+
{
48+
__list_add(_new, head, head->next);
49+
}
50+
51+
static inline void list_add_tail(struct list_head *_new, struct list_head *head)
52+
{
53+
__list_add(_new, head->prev, head);
54+
}
55+
56+
static inline void __list_del(struct list_head *entry)
57+
{
58+
entry->next->prev = entry->prev;
59+
entry->prev->next = entry->next;
60+
}
61+
62+
static inline void list_del(struct list_head *entry)
63+
{
64+
__list_del(entry);
65+
entry->next = entry->prev = NULL;
66+
}
4867

4968
static inline int BKDRHash(char *s, size_t size)
5069
{
@@ -56,26 +75,23 @@ static inline int BKDRHash(char *s, size_t size)
5675
return hash % size;
5776
}
5877

59-
static int find(char *word, struct hlist_head *heads, int size)
78+
static int find(char *word, struct list_head *heads, int size)
6079
{
80+
struct word_node *wn;
6181
int hash = BKDRHash(word, size);
62-
struct hlist_node *pos;
63-
hlist_for_each(pos, &heads[hash]) {
64-
struct word_node *wn = list_entry(pos, struct word_node, node);
82+
list_for_each_entry(wn, &heads[hash], link) {
6583
if (!strcmp(wn->word, word)) {
6684
return wn->index;
6785
}
6886
}
6987
return -1;
7088
}
7189

72-
static void add(char **words, int index, struct hlist_head *heads, int size, int *freqs)
90+
static void add(char **words, int index, struct list_head *heads, int size, int *freqs)
7391
{
74-
int hash = BKDRHash(words[index], size);
75-
struct hlist_node *pos;
7692
struct word_node *wn;
77-
hlist_for_each(pos, &heads[hash]) {
78-
wn = list_entry(pos, struct word_node, node);
93+
int hash = BKDRHash(words[index], size);
94+
list_for_each_entry(wn, &heads[hash], link) {
7995
if (!strcmp(wn->word, words[index])) {
8096
freqs[wn->index]++;
8197
return;
@@ -84,7 +100,7 @@ static void add(char **words, int index, struct hlist_head *heads, int size, int
84100
wn = malloc(sizeof(*wn));
85101
wn->word = words[index];
86102
wn->index = index;
87-
hlist_add_head(&wn->node, &heads[hash]);
103+
list_add(&wn->link, &heads[hash]);
88104
freqs[wn->index]++;
89105
}
90106

@@ -101,9 +117,9 @@ static int *findSubstring(char *s, char **words, int wordsSize, int *returnSize)
101117

102118
int i, j, cap = 10000, count = 0;
103119
int hash_size = wordsSize;
104-
struct hlist_head *heads = malloc(hash_size * sizeof(*heads));
120+
struct list_head *heads = malloc(hash_size * sizeof(*heads));
105121
for (i = 0; i < hash_size; i++) {
106-
INIT_HLIST_HEAD(&heads[i]);
122+
INIT_LIST_HEAD(&heads[i]);
107123
}
108124

109125
int *freqs = malloc(wordsSize * sizeof(int));

0105_construct_binary_tree_from_preorder_and_inorder_traversal/binary_tree_build.c

+42-46
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,67 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4+
45
#define container_of(ptr, type, member) \
56
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
67

78
#define list_entry(ptr, type, member) \
89
container_of(ptr, type, member)
910

10-
#define hlist_for_each(pos, head) \
11-
for (pos = (head)->first; pos; pos = pos->next)
11+
#define list_for_each_entry(pos, head, member) \
12+
for (pos = list_entry((head)->next, typeof(*pos), member); \
13+
&(pos)->member != (head); \
14+
pos = list_entry((pos)->member.next, typeof(*pos), member))
1215

13-
struct hlist_node;
16+
struct list_head {
17+
struct list_head *next, *prev;
18+
};
1419

15-
struct hlist_head {
16-
struct hlist_node *first;
20+
struct TreeNode {
21+
int val;
22+
struct TreeNode *left;
23+
struct TreeNode *right;
1724
};
1825

19-
struct hlist_node {
20-
struct hlist_node *next, **pprev;
26+
struct order_node {
27+
struct list_head link;
28+
int val;
29+
int index;
2130
};
2231

23-
static inline void INIT_HLIST_HEAD(struct hlist_head *h) {
24-
h->first = NULL;
32+
static inline void INIT_LIST_HEAD(struct list_head *list)
33+
{
34+
list->next = list->prev = list;
2535
}
2636

27-
static inline int hlist_empty(struct hlist_head *h) {
28-
return !h->first;
37+
static inline int list_empty(const struct list_head *head)
38+
{
39+
return (head->next == head);
2940
}
3041

31-
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
42+
static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
3243
{
33-
if (h->first != NULL) {
34-
h->first->pprev = &n->next;
35-
}
36-
n->next = h->first;
37-
n->pprev = &h->first;
38-
h->first = n;
44+
next->prev = new;
45+
new->next = next;
46+
new->prev = prev;
47+
prev->next = new;
3948
}
4049

41-
static inline void hlist_del(struct hlist_node *n)
50+
static inline void list_add(struct list_head *_new, struct list_head *head)
4251
{
43-
struct hlist_node *next = n->next;
44-
struct hlist_node **pprev = n->pprev;
45-
*pprev = next;
46-
if (next != NULL) {
47-
next->pprev = pprev;
48-
}
52+
__list_add(_new, head, head->next);
4953
}
5054

51-
struct TreeNode {
52-
int val;
53-
struct TreeNode *left;
54-
struct TreeNode *right;
55-
};
56-
57-
struct order_node {
58-
struct hlist_node node;
59-
int val;
60-
int index;
61-
};
55+
static inline void list_add_tail(struct list_head *_new, struct list_head *head)
56+
{
57+
__list_add(_new, head->prev, head);
58+
}
6259

63-
static int find(int num, int size, struct hlist_head *heads)
60+
static int find(int num, int size, struct list_head *heads)
6461
{
65-
struct hlist_node *p;
62+
struct order_node *on;
6663
int hash = (num < 0 ? -num : num) % size;
67-
hlist_for_each(p, &heads[hash]) {
68-
struct order_node *on = list_entry(p, struct order_node, node);
64+
list_for_each_entry(on, &heads[hash], link) {
6965
if (num == on->val) {
7066
return on->index;
7167
}
@@ -74,7 +70,7 @@ static int find(int num, int size, struct hlist_head *heads)
7470
}
7571

7672
static struct TreeNode *dfs(int *preorder, int pre_low, int pre_high, int *inorder,
77-
int in_low, int in_high, struct hlist_head *in_heads, int size)
73+
int in_low, int in_high, struct list_head *in_heads, int size)
7874
{
7975
if (in_low > in_high || pre_low > pre_high) {
8076
return NULL;
@@ -87,21 +83,21 @@ static struct TreeNode *dfs(int *preorder, int pre_low, int pre_high, int *inord
8783
return tn;
8884
}
8985

90-
static void node_add(int val, int index, int size, struct hlist_head *heads)
86+
static void node_add(int val, int index, int size, struct list_head *heads)
9187
{
9288
struct order_node *on = malloc(sizeof(*on));
9389
on->val = val;
9490
on->index = index;
9591
int hash = (val < 0 ? -val : val) % size;
96-
hlist_add_head(&on->node, &heads[hash]);
92+
list_add(&on->link, &heads[hash]);
9793
}
9894

99-
static struct TreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize)
95+
struct TreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize)
10096
{
10197
int i;
102-
struct hlist_head *in_heads = malloc(inorderSize * sizeof(*in_heads));
98+
struct list_head *in_heads = malloc(inorderSize * sizeof(*in_heads));
10399
for (i = 0; i < inorderSize; i++) {
104-
INIT_HLIST_HEAD(&in_heads[i]);
100+
INIT_LIST_HEAD(&in_heads[i]);
105101
}
106102
for (i = 0; i < inorderSize; i++) {
107103
node_add(inorder[i], i, inorderSize, in_heads);

0 commit comments

Comments
 (0)