#include #include #include #include "ustr.h" /* * List node for storing a unique string as element */ struct Node { struct Node *next; struct UStr ustr; }; /* * list head: list points to the first list node */ static struct Node *list; const struct UStr * UStrAdd_(const char *s, bool *added) { size_t len = strlen(s); /* * if added is the null pointer it gets ignored. Otherwise initialized with * true. */ if (added) { *added = true; } /* * Search in list if it already contains string s */ for (struct Node *n = list; n; n = n->next) { /* * TODO: * - Check if node n contains the string * - If this is the case: * - If added is not the null pointer set *added to false * - return a pointer to the unique string object stored in this node */ } /* * String needs to be added */ struct Node *n = 0; // <- TODO: Allocate a sufficiently large memory block // for a node that can store a string element // with length len. // Do not forget the null byte! if (!n) { fprintf(stderr, "makeUStr: out of memory\n"); abort(); } /* * Initialize the unique string object stored in this node: */ n->ustr.len = len; // TODO: Initialize also n->ustr.cstr by copying s into this buffer /* * TODO: Append the new node to the head of the list. I.e. the new node * will become the new head of the list. The successor of the new * node will be the old list head. */ /* * Return a pointer to the unique string object */ return &n->ustr; } const struct UStr * UStrAdd(const char *s) { return UStrAdd_(s, 0); } void UStrPrintPool(void) { for (struct Node *n = list; n; n = n->next) { printf("%s\n", n->ustr.cstr); } }