Created
May 19, 2014 16:19
-
-
Save REPOmAN2v2/761b793a60a1e08c00b8 to your computer and use it in GitHub Desktop.
Linked list
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
#include <assert.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct node | |
{ | |
int value; | |
struct node* next; | |
} node; | |
void append(struct node** const list, int const val) | |
{ | |
assert(list); | |
if (!*list) | |
{ | |
*list = malloc(sizeof (struct node)); | |
**list = (struct node) { val, NULL }; | |
} | |
else | |
append(&(*list)->next, val); | |
} | |
void print_reverse(struct node const* const list, FILE* const of) | |
{ | |
if (!list) | |
return; | |
print_reverse(list->next, of); | |
fprintf(of, "%d%c", list->value, list->next ? ',' : 0); | |
} | |
int main(void) | |
{ | |
struct node* list = NULL; | |
append(&list, 42); | |
append(&list, 666); | |
append(&list, 9001); | |
print_reverse(list, stdout); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment