Skip to content

Instantly share code, notes, and snippets.

@KokoseiJ
Created December 17, 2021 08:03
Show Gist options
  • Save KokoseiJ/e3b14938e2df5e6aee7e56b5ec1e4e88 to your computer and use it in GitHub Desktop.
Save KokoseiJ/e3b14938e2df5e6aee7e56b5ec1e4e88 to your computer and use it in GitHub Desktop.
Simple *char-key linked list implementation
#include "llist.h"
Linkedlist *llist_put(Linkedlist *list, char *key, void *value) {
if (list == NULL) {
list = calloc(1, sizeof(Linkedlist));
*list = (Linkedlist) {key, value, NULL};
return list;
}
if (!strcmp(key, list->key)) {
list->value = value;
} else {
list->next = llist_put(list->next, key, value);
}
return list;
}
void *llist_get(Linkedlist *list, char *key) {
if (list == NULL) return NULL;
if (!strcmp(key, list->key)) return list->value;
else return llist_get(list->next, key);
}
void llist_free(Linkedlist *list) {
if (list != NULL) {
llist_free(list->next);
free(list);
}
return;
}
#ifndef INCL_LLIST_H
#define INCL_LLIST_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Linkedlist Linkedlist;
typedef struct Linkedlist {
char *key;
void *value;
Linkedlist *next;
} Linkedlist;
Linkedlist *llist_put(Linkedlist *list, char *key, void *value);
void *llist_get(Linkedlist *list, char *key);
void llist_free(Linkedlist *list);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment