Re: Hall Fame ARENA

From: Mike Breuer (mbreuer@new.rr.com)
Date: 09/06/01


----- Original Message -----
From: "Eduardo Bertin" <damage@LINKWAY.COM.BR>


>  while (fscanf(fl, "%s %d %d", name, &date, &award) == 3) {
>    CREATE(next_node, struct hall_of_fame_element, 1);
>    strncpy(next_node->name, name, MAX_NAME_LENGTH);
>    next_node->name[MAX_NAME_LENGTH] = '\0';
>    next_node->date = date;
>    next_node->award = award;
>    next_node->next = fame_list;
>    fame_list = next_node;
>  }

Since the list is in reverse order of the file, and I assume you are writing
new entries to the end of your file, you can simply walk the list 5 elements
and trim off anything after that:

void trim_list( struct hall_of_fame_element *list)
{
  int count = 0;
  struct hall_of_fame_element *next;

  if (!list) return;

  while (++count < 5 && list->next)
    list = list->next;

  next = list->next;
  list->next = 0;

  while (next) {
    list = next;
    next = list->next;
    free(list);
  }

}

Where you might encounter some difficulty is with saving the shortened list
back to the file (if that is your intention).  Since you are reading the
entries in reverse order, you will need to write them back out in reverse
order again so that the correct person is bumped off the list each time.  A
better solution would be to add your entries to the tail of your list as you
read them from the file, and use a counter to determine how many entries to
trim from the head.

Mike

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/06/01 PST