49 lines
938 B
C
49 lines
938 B
C
#include <stdio.h>
|
|
|
|
typedef struct st st;
|
|
typedef struct node node; // forward declaration
|
|
|
|
typedef struct node {
|
|
const void* key;
|
|
void* value;
|
|
node* prev;
|
|
node* next;
|
|
} node;
|
|
|
|
struct st {
|
|
node* head;
|
|
node* tail;
|
|
};
|
|
|
|
// add new node to the end
|
|
void st_add(st table, void* key, void* value) {
|
|
node n = {key, value, table.tail};
|
|
if (table.head == NULL) {
|
|
table.head = &n;
|
|
table.tail = &n;
|
|
}
|
|
else {
|
|
(*(table.tail)).next = &n;
|
|
table.tail = &n;
|
|
}
|
|
}
|
|
|
|
void* nodeSearch(node* n, void* key) {
|
|
if (n != NULL) {
|
|
if ((*n).key == key) {
|
|
return (*n).value;
|
|
}
|
|
else {
|
|
return nodeSearch((*n).next, key);
|
|
}
|
|
}
|
|
else {
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
// returns the pointer to key of the specified node, or null, if it does not exist
|
|
void* st_search(st table, void* key) {
|
|
return nodeSearch(table.head, key);
|
|
}
|