Last active
August 29, 2015 13:56
-
-
Save nsmaciej/9142817 to your computer and use it in GitHub Desktop.
Single linked list in C
This file contains hidden or 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 Node { | |
struct Node *next; | |
int val; | |
} Node; | |
typedef struct { | |
int length; | |
Node *next; | |
} List; | |
void list_append(List *l, int v) { | |
Node *cn; | |
l->length += 1; | |
if (l->next == NULL) { | |
l->next = calloc(1, sizeof(Node)); | |
l->next->val = v; | |
return; | |
} | |
cn = l->next; | |
while (true) { | |
if (cn->next == NULL) { | |
cn->next = calloc(1, sizeof(Node)); | |
cn->next->val = v; | |
return; | |
} | |
cn = cn->next; | |
} | |
} | |
void list_print(List l, const char *s) { | |
Node *cn = l.next; | |
while (cn != NULL) { | |
printf("%d", cn->val); | |
printf("%s", s); | |
cn = cn->next; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment