1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
/********************************************************************\
Copyright (c) 2014 by Aleksey Cheusov
See LICENSE file in the distribution.
\********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mkc_RB.h>
struct berry {
RB_ENTRY(berry) link;
char *berry;
};
static int berrys_cmp (struct berry *a, struct berry *b)
{
return strcmp (a->berry, b->berry);
}
static RB_HEAD (berrys_entries, berry) berrys = RB_INITIALIZER(&berrys);
RB_PROTOTYPE (berrys_entries, berry, link, berrys_cmp);
RB_GENERATE (berrys_entries, berry, link, berrys_cmp);
static void output_berries (void)
{
struct berry *data;
data = (struct berry *) RB_MIN (berrys_entries, &berrys);
while (data){
puts (data->berry);
data = (struct berry *) RB_NEXT (berrys_entries, &berrys, data);
}
}
static void destroy_berries (void)
{
struct berry *data, *next;
data = (struct berry *) RB_MIN (berrys_entries, &berrys);
while (data){
next = (struct berry *) RB_NEXT (berrys_entries, &berrys, data);
RB_REMOVE (berrys_entries, &berrys, data);
free (data->berry);
free (data);
data = next;
}
}
static void add_berry (char *s)
{
struct berry *n = malloc (sizeof (*n));
struct berry *data;
n->berry = s;
data = RB_INSERT (berrys_entries, &berrys, n);
if (data){
free (s);
free (n);
}
}
int main (int argc, char **argv)
{
char buf [100];
size_t len;
while (fgets (buf, sizeof (buf), stdin)){
len = strlen (buf);
if (len > 0 && buf [len-1] == '\n')
buf [len-1] = 0;
add_berry (strdup (buf));
}
output_berries ();
destroy_berries ();
return 0;
}
|