Exemplo n.º 1
0
CHtbl CHTBLinit(int buckets, int (*h)(const void *key), int (*match)(const void *key1, const void *key2), void (*destroy)(void *data))
{
  CHtbl htbl;
  int i;

  if ((htbl = (CHtbl)malloc(sizeof(struct CHtbl_)))==NULL)
    return NULL;

  if ((htbl->table = (Slist *)malloc(buckets * sizeof(Slist))) == NULL)
    return NULL;

  htbl->buckets = buckets;

  for (i = 0; i < htbl->buckets; i++)
    {
      if ((htbl->table[i] = SLISTinit(destroy)) == NULL)
        return NULL;
      SLISTsetmatch(htbl->table[i], match);
    }

  htbl->h = h;
  htbl->match = match;
  htbl->destroy = destroy;
  htbl->size = 0;

  return htbl;
}
Exemplo n.º 2
0
int main(void)
{
  /* Declare YOUR variables here ! */
  Slist mylist;
  int menu_choice;

  /* Seed to random generator and clear the screen.. */
  srand((unsigned int)time(NULL));

  if ((mylist = SLISTinit(my_destroy)) == NULL)
    {
      printf("\nFatal error... - bailing out!");
      SLISTdestroy(mylist);
      exit(-1);
    }

  /* Set match-callback into list structure.. */
  SLISTsetmatch(mylist, my_match);

  /* Populate the (empty) list.. */
  create_random_nodes(mylist, NR_OF_ITEMS);

  /* Enter menu loop... */
  do
    {
      menu_choice = menu(MAIN_MENU_ROW, 0, 5);

      switch (menu_choice)
        {
        case 1:
          ins_nodes(mylist);
          break;
        case 2:
          rem_nodes(mylist);
          break;
        case 3:
          search_node(mylist);
          break;
        case 4:
          sort_list(mylist);
          break;
        case 5:
          print_list(mylist);
          break;
        default:
          final_status(mylist);
          break;
        }
    }
  while (menu_choice); 

  /* ..and finally destroy the list. */
  prompt_and_pause("\n\nLet's tidy up and destroy the list..- Bye!");
  SLISTdestroy(mylist);

  return 0;
}