/* linkedlist.c -- Einfach verkettete Liste von Zeichenketten */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct string_node {
  char *text;
  struct string_node *next;
};

struct string_list {
  struct string_node *head;
};


struct string_list *new_string_list()
{
  struct string_list *list = malloc(sizeof(struct string_list));
  list->head = NULL;
  return list;
}

void free_string_list(struct string_list *list)
{
  struct string_node *node = list->head;
  free(list);
  while (node != NULL) {
    struct string_node *next = node->next;
    free(node->text);
    free(node);
    node = next;
  }
}

void string_list_push(struct string_list *list, char *text)
{
  struct string_node *node = malloc(sizeof(struct string_node));
  node->text = text;
  node->next = list->head;
  list->head = node;
}

/* Gesamtlaenge aller Zeichenketten: */

int total_length(struct string_list *list)
{
  int length = 0;
  struct string_node *node = list->head;
  while (node != NULL) {
    length += strlen(node->text);
    node = node->next;
  }
  return length;
}

/* Zaehlt Anzahl Zeichenketten, die mit H bzw. G beginnen. */
void count_H_and_G(struct string_list *list,
		   int *num_H_p,
		   int *num_G_p)
{
  *num_H_p = 0;
  *num_G_p = 0;
  struct string_node *node = list->head;
  while (node != NULL) {
    switch (node->text[0]) {
    case 'H':
      (*num_H_p)++;
      break;
    case 'G':
      (*num_G_p)++;
      break;
    }
    node = node->next;
  }
}

int main ()
{
  struct string_list *list = new_string_list();
  string_list_push(list, strdup("Hello"));
  string_list_push(list, strdup("Good-bye"));
  printf("Gesamtlaenge: %d\n", total_length(list));
  {
    int num_H, num_G;
    count_H_and_G(list, &num_H, &num_G);
    printf("Anzahl H...: %d,  Anzahl G...: %d\n", num_H, num_G);
  }
  return 0;
}
