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 (2619B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 struct MapEntry {
      6     char *key;  /* public */
      7     int value;  /* public */
      8     struct MapEntry *__prev;
      9     struct MapEntry *__next;
     10 };
     11 
     12 struct MapIter {
     13    struct MapEntry *__current;
     14 
     15    struct MapEntry* (*next)(struct MapIter* self);
     16    void (*del)(struct MapIter* self);
     17 };
     18 
     19 struct Map {
     20    /* Attributes */
     21    struct MapEntry *__head;
     22    struct MapEntry *__tail;
     23    int __count;
     24 
     25    /* Methods */
     26    void (*put)(struct Map* self, char *key, int value);
     27    int (*get)(struct Map* self, char *key, int def);
     28    int (*size)(struct Map* self);
     29    void (*dump)(struct Map* self);
     30    struct MapIter* (*iter)(struct Map* self);
     31    void (*del)(struct Map* self);
     32 };
     33 
     34 void __Map_del(struct Map* self) {
     35     struct MapEntry *cur, *next;
     36     cur = self->__head;
     37     while(cur) {
     38         free(cur->key);
     39         /* value is just part of the struct */
     40         next = cur->__next;
     41         free(cur);
     42         cur = next;
     43     }
     44     free((void *)self);
     45 }
     46 
     47 void __MapIter_del(struct MapIter* self) {
     48     free((void *)self);
     49 }
     50 
     51 void __Map_dump(struct Map* self)
     52 {
     53     struct MapEntry *cur;
     54     printf("Object Map count=%d\n", self->__count);
     55     for(cur = self->__head; cur != NULL ; cur = cur->__next ) {
     56          printf("  %s=%d\n", cur->key, cur->value);
     57     }
     58 }
     59 
     60 struct MapEntry* __Map_find(struct Map* self, char *key)
     61 {
     62     struct MapEntry *cur;
     63     if ( self == NULL || key == NULL ) return NULL;
     64     for(cur = self->__head; cur != NULL ; cur = cur->__next ) {
     65         if(strcmp(key, cur->key) == 0 ) return cur;
     66     }
     67     return NULL;
     68 }
     69 
     70 int __Map_get(struct Map* self, char *key, int def)
     71 {
     72     struct MapEntry *retval = __Map_find(self, key);
     73     if ( retval == NULL ) return def;
     74     return retval->value;
     75 }
     76 
     77 int __Map_size(struct Map* self)
     78 {
     79     return self->__count;
     80 }
     81 
     82 /* Student code will be inserted here */
     83 
     84 int main(void)
     85 {
     86     struct Map * map = Map_new();
     87     struct MapEntry *cur;
     88     struct MapIter *iter;
     89 
     90     /* Make sure we see all output up to an error */
     91     setvbuf(stdout, NULL, _IONBF, 0);
     92 
     93     printf("Map test\n");
     94     map->put(map, "z", 8);
     95     map->put(map, "z", 1);
     96     map->put(map, "y", 2);
     97     map->put(map, "b", 3);
     98     map->put(map, "a", 4);
     99     map->dump(map);
    100 
    101     printf("size=%d\n", map->size(map));
    102 
    103     printf("z=%d\n", map->get(map, "z", 42));
    104     printf("x=%d\n", map->get(map, "x", 42));
    105 
    106     printf("\nIterate\n");
    107     iter = map->iter(map);
    108     while(1) {
    109         cur = iter->next(iter);
    110         if ( cur == NULL ) break;
    111         printf("%s=%d\n", cur->key, cur->value);
    112     }
    113     iter->del(iter);
    114 
    115     map->del(map);
    116 }