Skip to content

Instantly share code, notes, and snippets.

@nsmaciej
Last active August 29, 2015 13:56
Show Gist options
  • Save nsmaciej/9142817 to your computer and use it in GitHub Desktop.
Save nsmaciej/9142817 to your computer and use it in GitHub Desktop.
Single linked list in C
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