Problem with linked lists



Hello. I`m still starting my journey with gtk programming and I have problem
with moving over double linked lists.
Below I`ve put a piece of my program where I`m trying to learn
how to deal with these lists. What I wanted to reach is that I add two
elements to the list. In the data field of Glist structure I
put my own structure. After adding first and second element I just for test
put 0 in the komp_list->data->numer of the first element
of double linked list and 1 in the second. Then, after putting 1 to the list
element i`m trying to go back from second element to
the first and check if the komp->data->numer field has 0 in there. But My
problem is that I put 0 in first element, print it (it`s ok),
adding and put 1 into second element (and also printing and is ok) and then
I`m trying to go back and print first element once more -
but there I get 1 once more - not 0 !!! I don`t know, where I made mistake -
maybe there is something to much or to less ?
or maybe I didn`t get the idea wright ? Can you help me ? I`ve got also
second question (below my program listing);

typedef struct {
char *numer;
        char *ip;
 char *stan;
    } KompStruct;

gchar *numerr[10];
GList *komp_list = NULL;
KompStruct *cos,*elementy;

numerr[0]="0";
numerr[1]="1";

elementy = g_new(KompStruct,1);
 komp_list = g_list_append(komp_list,elementy);
cos = (KompStruct *) komp_list->data;
cos->numer=g_strdup(numerr[0]);
printf("what we have in the numer field of FIRST element of the list
(komp_list->data->numer): %s\n",cos->numer);

elementy = g_new(KompStruct,1);
komp_list = g_list_append(komp_list,elementy);         // adding new element
to the list
komp_list = g_list_next(komp_list);         //moving from first to second
element

cos = (KompStruct *) komp_list->data;
cos->numer=g_strdup(numerr[1]);
printf("what we have in the numer field of SECOND element of the list
(komp_list->data->numer): %s\n",cos->numer);

komp_list = g_list_previous(komp_list);  //back to the FIRST element
cos = (KompStruct *) komp_list->data;
printf("once more what we have in FIRST element of the list
(komp_list->data->numer): %s\n",cos->numer);

Second question:
I wanted to use in the same program function g_list_position but I`m not
sure If I assigned wright the second argument
of this function - it shoud be some element of my list, and I should be able
to chceck his position but I`m not sure if temp_elem
could be this element and hou it should be assigned.

for example: (assuming that I my list is big enougth)
komp_list = g_list_next(komp_list);
komp_list = g_list_next(komp_list);
komp_list = g_list_next(komp_list);
temp_elem=komp_list;
pozycja_lista = g_list_position(komp_list,temp_elem);
g_print("the position of element is %d\n",pozycja_lista);

//for example should be 4 assuming that we start counting from 0

This information are very important to me so I will be very grateful.

Best regards

Szymon




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