udpate reverse_linked_number

This commit is contained in:
xwy@asus 2019-09-04 02:18:53 +01:00
parent a75821353d
commit 4df154882a

View File

@ -28,41 +28,28 @@ st_node *node_sum_r(int carry_up, st_node *p, st_node *q)
st_node *node_sum(st_node *p, st_node *q) st_node *node_sum(st_node *p, st_node *q)
{ {
if (!p || !q) { if (!p || !q) return 0;
return 0;
}
return node_sum_r(0, p, q); return node_sum_r(0, p, q);
} }
st_node *str_to_node(char *num) st_node *str_to_node(char *num)
{ {
st_node *node = 0; if (!*num) return 0;
for (; *num; num++) { return node_alloc(*num - '0', str_to_node(num + 1));
st_node *new = node_alloc(*num - '0', 0);
new->next = node;
node = new;
}
return node;
} }
int node_len(st_node *node) void node_show_r(st_node *node)
{ {
int len = 0; if (!node) return;
for (; node; node = node->next) len += 1; node_show_r(node->next);
return len; printf("%d", node->val);
return;
} }
void node_show(st_node *node) void node_show(st_node *node)
{ {
int len = node_len(node); node_show_r(node);
char *buf = (char *)malloc(len + 1); printf("\n");
char *p = buf + len;
*p-- = 0;
for (; node; node = node->next) {
*p-- = node->val + '0';
}
printf("%s\n", buf);
free(buf);
return; return;
} }