Well, this is something I do a lot of in every one of my applications.  If
the data needs be sorted, I always use a linked list.  This has the
advantage of sorting 100+ items instantaneously.  It's a little complex,
however.

typedef struct bob_t {
  // Put the data you wish to list here... VFS attributes, etc.
  Char *caption;

  struct bob_t *next;
} BobType, *BobPtr;

Once you have the data structure (note the *next pointer pointing to the
same structure...) down pat, you need a global head pointer and some
functions.  If you wish to be able to add or remove an element anywhere in
the list, you'll need a doubly-linked list.  That's a little more complex,
but is a simple extension of the following.

BobPtr  head;   // A pointer to the first item in the list... always.
UInt32  count;  // A running count of the items in the list.
BobPtr *sorted; // The sorted array of items.

BobPtr addNode(Char *caption) {
  BobPtr p = MemPtrNew(sizeof(BobType));
  if ( !p ) return NULL; // error...
  count++;
  p->next = head;       // the next item used to be the first...
  head = p;             // make the new item the head.
                        // Note: this effectively reverses the sort.
  if ( caption ) {
    p->caption = MemPtrNew(sizeof(Char) * (StrLen(caption)+1));
    StrCopy(p->caption, caption);
  }
}

// allow you to use different heads...
void deleteNodes(BobPtr head) {
  BobPtr p = head, q;

  while ( p ) {
    q = p->next;

    if ( p->caption ) MemPtrFree(p->caption);
    MemPtrFree(p);

    p = q;    
  }

  count = 0;
}

// This function is called by QuickSort... sort by caption.
Int16 captionSortCallback(void *p1, void *p2, Int32 other) {
  BobPtr i = *(BobPtr *)p1,
         j = *(BobPtr *)p2;

  return StrCompare(i->caption, j->caption);
}

// Sorting is a whole other matter...
void sortNodes(void) {
  BobPtr p, **r;

  if ( sorted ) MemPtrFree(sorted);

  sorted = MemPtrNew(sizeof(BobPtr) * count + 1);
  if ( !sorted ) return; // error!
  MemSet(sorted, MemPtrSize(sorted), 0); // just to be safe.

  // This is the magic that creates the array of pointers.
  for ( r = sorted, p = head; p; p = p->next, r++ ) *r = p;

  SysQSort(sorted, count, sizeof(BobPtr *), &captionSortCallback, NULL);
}

// Now you can address the sorted list like this:
// sorted[20]->caption // returns the 21'st caption.

This, of course, may be more than you are looking for.  Trust me, though,
when I say linked lists are the way to go - they're blinding fast when
adding, removing, and sorting items.  In fact, with multiple sorted pointer
arrays, you can pre-cache multiple sorts, combine them, etc.


> -----Original Message-----
> From: Roberto Pedrozo Mendes [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, June 22, 2004 10:30 AM
> To: Palm Developer Forum
> Subject: creating list dinamically
> 
> Hi
> How can I to create a list dinamically in my aplication ?
> Thanks
> 
> =====
> Roberto Pedrozo Mendes
> Arquiteto Sistemas
> HST - CPqD - Campinas
> [EMAIL PROTECTED]
> 
> 
> 
> 
> ______________________________________________________________________
> 
> Yahoo! Mail - agora com 100MB de espa�o, anti-spam e antiv�rus gr�tis!
> http://br.info.mail.yahoo.com/
> 
> -- 
> For information on using the Palm Developer Forums, or to 
> unsubscribe, please see http://www.palmos.com/dev/support/forums/
> 
> 
> 



--
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to