#include #include #include #define Hash int #define perror(x) fprintf(stderr, "%s\n", x) #define KEY_SIZE 64 typedef struct Node { struct Node *next; struct Node *prev; char key[KEY_SIZE]; int value; }Node; typedef struct List { Node *root; Node *current; }List; typedef struct Hash { } Hash *hash_create(); Hash *hash_destroy(Hash *h); void hash_add(Hash *h, char *key, int value); Node *hash_get(Hash *h, char *key); void hash_del(Hash *h, char *key); List *list_create(); List *list_destroy(List *l); void list_remove_node(List *l, Node *n); void print_list(List *l); void list_push(List *l, char *key, int val); Node *node_create(char *key, int value); Node *node_destroy(Node *n); int set(Hash *h, List *l, char *key, int value); int get(Hash *h, List *l, char *key); int del(Hash *h, List *l, char *key); Node *node_create(char *key, int value) { Node *n = NULL; if (NULL == key) { perror("Null key passed into node_create."); return NULL; } if(NULL == (n = calloc(sizeof(Node), 1))) { perror("Failed to allocate new node in node_create."); return NULL; } n->next = NULL; n->prev = NULL; strncpy(n->key, key, KEY_SIZE); return n; } Node *node_destroy(Node *n) { if (NULL == n) { perror("Null node passed into node_destroy."); return NULL; } n->prev = NULL; n->next = NULL; n->value = 0; memset(n->key, 0, KEY_SIZE); return n; } List *list_create() { List *l = NULL; if (NULL == (l = calloc(sizeof(List), 1))) { perror("list allocation failed"); return NULL; } l->root = NULL; l->current = NULL; return l; } List *list_destroy(List *l) { if (NULL == l) { perror("Null value passed into list_destroy."); return NULL; } while(l->root) list_remove_node(l, l->root); return l; } void list_remove_node(List *l, Node *n) { Node *prev = NULL, *next = NULL; if (NULL == l || NULL == n) { perror("Null argument passed to remove_node."); return; } prev = n->prev; next = n->next; if (NULL != next) next->prev = prev; if (NULL != prev) prev->next = next; else l->root = next; node_destroy(n); } void print_list(List *l) { if (NULL == l || NULL == l->root) { perror("Null value passed into print_list."); return; } l->current = l->root; while(l->current != NULL) { printf("(%s, %d),", l->current->key, l->current->value); l->current = l->current->next; } } void list_push(List *l, char *key, int value) { Node *n = NULL; if (NULL == l || NULL == key) { perror("Null value passed into list_push."); return; } if(NULL != (n = node_create(key, value))) { perror("Failed to allocate new node in list_push."); return; } if(NULL != l->root) l->root->prev = n; n->next = l->root; l->root = n; } int set(Hash *h, List *l, char *key, int value) { Node *n = hash_get(h, key); list_remove_node(l, n); list_push(l, key, value); hash_set(h, key, value); return 0; } int get(Hash *h, List *l, char *key) { int ret_val; Node *n = hash_get(h, key); ret_val = n->value; list_remove_node(l, n); list_push(l, key, ret_val); return n->value; } int del(Hash *h, List *l, char *key) { Node *n = hash_get(h, key); list_remove_node(l, n); return 0; } int main() { /* Simple example usage*/ Hash *h = hash_create(); List *l = list_create(); set(h, l, "a", 1); set(h, l, "b", 2); set(h, l, "c", 3); printf("%d\n", get(h, l, "a")); print_list(l); del(h, l, "b"); print_list(l); list_destroy(l); hash_destroy(h); return 0; }