Created
December 17, 2021 08:03
-
-
Save KokoseiJ/e3b14938e2df5e6aee7e56b5ec1e4e88 to your computer and use it in GitHub Desktop.
Simple *char-key linked list implementation
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
#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; | |
} |
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
#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