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


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 struct lnode {
      6     char *text;
      7     struct lnode *next;
      8 };
      9 
     10 struct pylist {
     11   struct lnode *head;
     12   struct lnode *tail;
     13   int count;
     14 };
     15 
     16 /* Constructor - lst = list() */
     17 struct pylist * pylist_new() {
     18     struct pylist *p = malloc(sizeof(*p));
     19     p->head = NULL;
     20     p->tail = NULL;
     21     p->count = 0;
     22     return p;
     23 }
     24 
     25 /* Destructor - del(lst) */
     26 void pylist_del(struct pylist* self) {
     27     struct lnode *cur, *next;
     28     cur = self->head;
     29     while(cur) {
     30         free(cur->text);
     31         next = cur->next;
     32         free(cur);
     33         cur = next;
     34     }
     35     free((void *)self);
     36 }
     37 
     38 int main(void)
     39 {
     40     setvbuf(stdout, NULL, _IONBF, 0);  /* Internal */
     41 
     42     struct pylist * lst = pylist_new();
     43     pylist_append(lst, "Hello world");
     44     pylist_print(lst);
     45     pylist_append(lst, "Catch phrase");
     46     pylist_print(lst);
     47     pylist_append(lst, "Brian");
     48     pylist_print(lst);
     49     printf("Length = %d\n", pylist_len(lst));
     50     printf("Brian? %d\n", pylist_index(lst, "Brian"));
     51     printf("Bob? %d\n", pylist_index(lst, "Bob"));
     52     pylist_del(lst);
     53 }