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

solution.c (2076B)


      1 /* print(dct) */
      2 /* {'z': 'W', 'y': 'B', 'c': 'C', 'a': 'D'} */
      3 void pydict_print(struct pydict* self)
      4 {
      5   struct dnode *curr;
      6   printf("{");
      7   for (curr = self->head;
      8        curr != NULL;
      9        curr = curr->next) {
     10     printf("'%s': '%s'",
     11            curr->key,
     12            curr->value);
     13     if (curr->next != NULL) {
     14       printf(", ");
     15     }
     16   }
     17   printf("}\n");
     18 }
     19 
     20 int pydict_len(const struct pydict* self)
     21 {
     22   struct dnode *curr;
     23   int count = 0;
     24   for (curr = self->head;
     25        curr != NULL;
     26        curr = curr->next) {
     27     ++count;
     28   }
     29   return count;
     30 }
     31 
     32 /* find a node - used in get and put */
     33 struct dnode* pydict_find(struct pydict* self, char *key)
     34 {
     35   struct dnode *current_node;
     36   for (current_node = self->head;
     37        current_node != NULL;
     38        current_node = current_node->next) {
     39     if (strcmp(current_node->key, key) == 0) {
     40       return current_node;
     41     }
     42   }
     43   return NULL;
     44 }
     45 
     46 /* x.get(key) - Returns NULL if not found */
     47 char* pydict_get(struct pydict* self, char *key)
     48 {
     49   struct dnode *found_node = pydict_find(self, key);
     50   if (found_node == NULL) {
     51     return NULL;
     52   }
     53 
     54   char *new_string = (
     55     (char *)
     56     malloc(
     57       strlen(
     58         found_node->value
     59       )
     60       + 1
     61     )
     62   );
     63 
     64   new_string = strcpy(new_string, found_node->value);
     65 
     66   return new_string;
     67 }
     68 
     69 /* x[key] = value; Insert or replace the value associated with a key */
     70 void pydict_put(struct pydict* self, char *key, char *value)
     71 {
     72   struct dnode *node = pydict_find(self, key);
     73   char *new_key, *new_value;
     74 
     75   new_value = (char *) malloc(sizeof(char) * (strlen(value) + 1));
     76   new_value = strcpy(new_value, value);
     77 
     78   if (node != NULL) {
     79     free(node->value);
     80     node->value = new_value;
     81     return;
     82   }
     83 
     84   new_key = (char *) malloc(sizeof(char) * (strlen(key) + 1));
     85   new_key = strcpy(new_key, key);
     86 
     87   node = (struct dnode *) malloc(sizeof(struct dnode));
     88   node->key = new_key;
     89   node->value = new_value;
     90   node->next = NULL;
     91 
     92   if (self->head == NULL) {
     93     self->head = node;
     94     self->tail = node;
     95     return;
     96   }
     97 
     98   self->tail->next = node;
     99   self->tail = node;
    100 }