learning-cc4e

Solution to https://cc4e.com/. If you copy these you're not right in the head.
git clone https://kaka.farm/~git/learning-cc4e
Log | Files | Refs

main.c (1346B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 struct dnode {
      6     char *key;
      7     char *value;
      8     struct dnode *next;
      9 };
     10 
     11 struct pydict {
     12   struct dnode *head;
     13   struct dnode *tail;
     14   int count;
     15 };
     16 
     17 /* Constructor - dct = dict() */
     18 struct pydict * pydict_new() {
     19     struct pydict *p = malloc(sizeof(*p));
     20     p->head = NULL;
     21     p->tail = NULL;
     22     p->count = 0;
     23     return p;
     24 }
     25 
     26 /* Destructor - del(dct) */
     27 void pydict_del(struct pydict* self) {
     28     struct dnode *cur, *next;
     29     cur = self->head;
     30     while(cur) {
     31         free(cur->key);
     32         free(cur->value);
     33         next = cur->next;
     34         free(cur);
     35         cur = next;
     36     }
     37     free((void *)self);
     38 }
     39 
     40 int main(void)
     41 {
     42     struct dnode * cur;
     43     struct pydict * dct = pydict_new();
     44 
     45     setvbuf(stdout, NULL, _IONBF, 0);  /* Internal */
     46 
     47     pydict_put(dct, "z", "Catch phrase");
     48     pydict_print(dct);
     49     pydict_put(dct, "z", "W");
     50     pydict_print(dct);
     51     pydict_put(dct, "y", "B");
     52     pydict_put(dct, "c", "C");
     53     pydict_put(dct, "a", "D");
     54     pydict_print(dct);
     55     printf("Length =%d\n",pydict_len(dct));
     56 
     57     printf("z=%s\n", pydict_get(dct, "z"));
     58     printf("x=%s\n", pydict_get(dct, "x"));
     59 
     60     printf("\nDump\n");
     61     for(cur = dct->head; cur != NULL ; cur = cur->next ) {
     62         printf("%s=%s\n", cur->key, cur->value);
     63     }
     64 
     65     pydict_del(dct);
     66 }