#include <glib.h>

void list_foreach_cb (gpointer data, gpointer user_data) {
  GSList * list = (GSList *) user_data;

  g_print ("Item: %s\n", (char *) data);
  list = g_slist_remove (list, data);
  g_free (data);
}

gint main (gint argc, gchar ** argv) {
  GSList * list = NULL;
  const char * first = "first";
  const char * last = "last";
  const char * one = "one";

  g_slice_set_config (G_SLICE_CONFIG_WORKING_SET_MSECS, 0);

  list = g_slist_prepend (list, g_strdup (last));
  list = g_slist_append (list, g_strdup (first));
  list = g_slist_append (list, g_strdup (one));
  list = g_slist_prepend (list, g_strdup ("two"));
  list = g_slist_append (list, g_strdup ("five"));

  g_print ("List length: %d\n", g_slist_length (list));

  g_print ("3rd item: %s\n", (char *) g_slist_nth_data (list, 2));

  g_slist_foreach (list, (GFunc) list_foreach_cb, list);
  g_slist_free (list);
  list = NULL;

  g_print ("List length: %d\n", g_slist_length (list));

  g_slist_free (list);

  return 0;
}
