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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
/*
* Copyright (C) 2009 Lincoln de Sousa <lincoln@minaslivre.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <taningia/list.h>
int cmp(ta_list_t *a, ta_list_t *b) {
return (int) a->data - (int) b->data;
}
int
main (int argc, char **argv)
{
ta_list_t *list = NULL;
ta_list_t *otherlist = NULL;
ta_list_t *node = NULL;
ta_list_t *extended = NULL;
ta_list_t *newlist = NULL;
ta_list_t *list_to_sort = NULL;
list = ta_list_append (list, "foo");
list = ta_list_append (list, "bar");
list = ta_list_append (list, "foo");
list = ta_list_append (list, "baz");
/* len, first, last and count */
printf ("list length: %d\n\n", ta_list_len (list));
printf ("first item value: %s\n\n", (char *) (ta_list_first (list))->data);
printf ("last item value: %s\n\n", (char *) (ta_list_last (list))->data);
printf ("list items:\n");
for (node = list; node; node = node->next)
printf (" %s\n", (char *) node->data);
printf ("\n");
printf ("counts of 'foo': %d\n\n",
ta_list_count (list, "foo", (ta_list_cmp_func_t) strcmp));
otherlist = ta_list_append (otherlist, "a");
otherlist = ta_list_append (otherlist, "b");
otherlist = ta_list_append (otherlist, "c");
printf ("otherlist items:\n");
for (node = otherlist; node; node = node->next)
printf (" %s\n", (char *) node->data);
printf ("\n");
/* extend, index and item */
/* ps.: do not free otherlist. If you do that, you are going to free
* a piece of the extended list. */
extended = ta_list_extend (list, otherlist);
printf ("extended list items:\n");
for (node = extended; node; node = node->next)
printf (" %s\n", (char *) node->data);
printf ("\n");
printf ("the index of `a' in extended list is: %d\n\n",
ta_list_index (extended, "a"));
printf ("value of 4th position of the extended list is: %s\n\n",
(char *) ta_list_item (extended, 4));
/* remove */
newlist = ta_list_append (newlist, "1");
newlist = ta_list_append (newlist, "2");
newlist = ta_list_append (newlist, "3");
printf ("removing element `1' from newlist:\n");
newlist = ta_list_remove (newlist, "1", NULL);
printf ("new size: %d\n", ta_list_len (newlist));
for (node = newlist; node; node = node->next)
printf (" %s\n", (char *) node->data);
printf ("\n");
/* insert */
printf ("reinserting `1' in the 1st position:\n");
newlist = ta_list_insert (newlist, "1", 0);
printf ("inserting `1.5' at the 2nd position:\n");
newlist = ta_list_insert (newlist, "1.5", 1);
printf ("inserting `2.5' at the 3rd position:\n");
newlist = ta_list_insert (newlist, "2.5", 3);
printf ("inserting `3.5' at the 5th position:\n");
newlist = ta_list_insert (newlist, "3.5", 5);
printf ("new size: %d\n", ta_list_len (newlist));
for (node = newlist; node; node = node->next)
printf (" %s\n", (char *) node->data);
printf ("\n");
ta_list_free (newlist);
newlist = NULL;
/* reverse */
newlist = ta_list_append (newlist, "a");
newlist = ta_list_append (newlist, "b");
newlist = ta_list_append (newlist, "c");
printf ("reversed newlist items:\n");
newlist = ta_list_reverse (newlist);
for (node = newlist; node; node = node->next)
printf (" %s\n", (char *) node->data);
printf ("\n");
ta_list_free (list);
ta_list_free (newlist);
/* sort */
printf ("Sorting the list");
list_to_sort = ta_list_append (list_to_sort, (void*) 42);
list_to_sort = ta_list_append (list_to_sort, (void*) 95);
list_to_sort = ta_list_append (list_to_sort, (void*) 18);
list_to_sort = ta_list_append (list_to_sort, (void*) 34);
list_to_sort = ta_list_append (list_to_sort, (void*) 5);
printf (" [");
for (node = list_to_sort; node; node = node->next)
{
printf ("%d", (int) node->data);
if (node->next)
printf (", ");
}
printf ("]\n");
list_to_sort = ta_list_sort (list_to_sort, cmp);
printf (" [");
for (node = list_to_sort; node; node = node->next)
{
printf ("%d", (int) node->data);
if (node->next)
printf (", ");
}
printf ("]\n");
return 0;
}
|