Created
November 19, 2013 19:11
-
-
Save icholy/7550780 to your computer and use it in GitHub Desktop.
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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
struct Link_t { | |
int value; | |
struct Link_t* next; | |
}; | |
typedef struct Link_t Link; | |
Link* | |
ll_new(int value) { | |
Link* l = (Link*)malloc(sizeof(Link)); | |
if (l == NULL) { | |
return NULL; | |
} | |
l->value = value; | |
l->next = NULL; | |
return l; | |
} | |
void | |
ll_append(Link* l, int value) { | |
while (l->next != NULL) { l = l->next; } | |
Link* new = (Link*)malloc(sizeof(Link)); | |
new->next = NULL; | |
new->value = value; | |
l->next = new; | |
} | |
void | |
ll_each(Link* l, void(*fn)(int)) { | |
while (l != NULL) { | |
fn(l->value); | |
l = l->next; | |
} | |
} | |
void | |
print_int(int x) { | |
printf("%d\n", x); | |
} | |
int main(void) { | |
Link* l = ll_new(0); | |
for (int i = 0; i < 10; i++) { | |
ll_append(l, i+10); | |
} | |
ll_each(l, &print_int); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment