Last active
December 10, 2019 13:22
-
-
Save is3ka1/862315ea5123d653520a8408b4fd6fbf to your computer and use it in GitHub Desktop.
It's the homework of my college, implementing a function to reverse a simple one-way linked list. And I think this function I came up with was fun.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
. | |
. | |
. | |
typedef struct reg { | |
int ID; | |
int score; | |
struct reg *next; | |
} tReg; | |
. | |
. | |
. | |
*/ | |
void *_reverse_after(tReg *node) { | |
void **p; | |
if (node->next == NULL) { | |
p = malloc(sizeof(void *) * 2); | |
p[0] = node; // tail | |
} else { | |
p = _reverse_after(node->next); | |
*(tReg **)p[1] = node; | |
} | |
p[1] = &node->next; | |
return p; | |
} | |
void reverse(tReg **head) { | |
void **p = _reverse_after(*head); | |
*head = p[0]; | |
*(tReg **)p[1] = NULL; | |
free(p); | |
} | |
/* | |
. | |
. | |
. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment