Skip to content

Instantly share code, notes, and snippets.

@REPOmAN2v2
Created May 19, 2014 16:19
Show Gist options
  • Save REPOmAN2v2/761b793a60a1e08c00b8 to your computer and use it in GitHub Desktop.
Save REPOmAN2v2/761b793a60a1e08c00b8 to your computer and use it in GitHub Desktop.
Linked list
#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