Re: G List - How to show specific item



Hello,

On Fri, 2001-10-26 at 15:16, William O'Shea wrote:
Hello,

I have created a GList which displays "Item 1", "Item 2" and "Item 3"

"Item 1" displays by default.

I'm guess by display, you mean access, so....

 
How can I tell the GList widget to display "Item 2" instead upon loading
without repacking.

Well, take a look at the code below:

     GList * list;

     // I'll put some stuff in the list.
     list = g_list_append(list, "First");
     list = g_list_append(list, "Second");
     list = g_list_append(list, "Third");

And then this would mean:

     list->data               has a value of   "First"
     list->next->data         has a value of   "Second"
     list->next->next->data   has a value of   "Third"

     // And list->next->next->next has a value of NULL, so it
     // doesn't have anything there!!!

Of course, you never do it that way.  What you'd do
might be some thing like this:

     GList * node;
     for (node = list; node; node != NULL = node->next) {
         g_print("This node has a value of %s", node->data);
     }


Usually, pointer based data structures are explained using pictures
and diagrams, but ASCII doesn't lend itself well to making diagrams,
so I can't draw you one, to explain this better.  But a GList is
(basically) a struct as follows:

     struct GList {
         void * data;
         GList * next;
         GList * prev;
     }

I don't know if know that helps you any, but a GList is a node in the
list... and to get to the next node, you have to access the 'next'
member of the struct.


See ya

     Charles Iliya Krempeaux
     tnt @ linux.ca
     ckrempea @ alumni.sfu.ca




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]