Skip to contentSkip to conversation

Some troubles with linked list/tree

DISCUSSION Alphex K. 2 participants 6 messages Apr 29, 2004 – Apr 30, 2004

lore
Share & tools
lore

Alphex K.

author
#0 lore raw

Hi guys!
I'm have a one question
so I'm have a two linked lists
typedef struct
{
int id;
int sf_id;
char *from_date;
char *to_date;
struct SfList *next;
} SfList;

typedef struct
{
int id;
char *name;
char *author;
char *note;
int cl_id;
SfList * list;
struct OapAdvAction *next;
} OapAdvAction;

so and functions to operate with there
SfList * AddSfEntry(SfList * list,int sf_id,char *from_date,char *to_date) {
int id=0;
SfList * lp=list;
if(list!=NULL)

     {                                                                               
        id=1;                                                                        
        while(list->next!=NULL)                                                      
          {                                                                          
             list=list->next;                                                        
             id++;                                                                   
          }                                                                          
        list->next=(struct SfList  *) malloc(sizeof(SfList));                        
        list=list->next;                                                             
        list->next=NULL;                                                             
        list->id=id;                                                                 
        list->sf_id=sf_id;                                                           
        list->from_date=from_date;                                                   
        list->to_date=to_date;                                                       
        return lp;                                                                   
     }                                                                               
   else                                                                              
     {                                                                               
        list=(struct SfList  *) malloc(sizeof(SfList));                              
        list->next=NULL;                                                             
        list->id=id;                                                                 
        list->sf_id=sf_id;                                                           
        list->from_date=from_date;                                                   
        list->to_date=to_date;                                                       
        return list;                                                                 
     }                                                                               

// end ;-)
}

and for OapAdvAction structure
OapAdvAction * AddAdvAction(OapAdvAction * action,char *name,char *author,
char *note,int cl_id,SfList * list)
{
OapAdvAction * lp=action;
int id=0;
if(action!=NULL)

     {                                                                               
        id=1;                                                                        
        while(action->next!=NULL)                                                    
          {                                                                          
             action=action->next;                                                    
             id++;                                                                   
          }                                                                          
        action->next=(struct OapAdvAction  *) malloc(sizeof(OapAdvAction)+sizeof(SfL$
        action=action->next;                                                         
        action->next=NULL;                                                           
        action->id=id;                                                               
        action->name=name;                                                           
        action->author=author;                                                       
        action->note=note;                                                           
        action->cl_id=cl_id;                                                         
        // adding list                                                               
        /*while(list!=NULL)                                                          
          {                                                                          
             action->list=AddSfEntry(action->list,list->sf_id,list->from_date,       
                                     list->to_date);                                 
             list=list->next;                                                        
          }*/                                                                        
        action->list=(struct SfList  *) malloc(sizeof(list));                        
        action->list=list;                                                           
        return lp;                                                                   
     }                                                                               
   else                                                                              
     {                                                                               
        action=(struct OapAdvAction  *) malloc(sizeof(OapAdvAction)+sizeof(SfList)); 
        action->next=NULL;                                                           
        action->id=id;                                                               
        action->name=name;                                                           
        action->author=author;                                                       
        action->note=note;                                                           
        action->cl_id=cl_id;                                                         
        // adding list                                                               
        /*while(list!=NULL)                                                          
          {                                                                          
             action->list=AddSfEntry(action->list,list->sf_id,list->from_date,       
                                     list->to_date);                                 
             list=list->next;                                                        
          }*/                                                                        
        action->list=(struct SfList  *) malloc(sizeof(list));                        
        action->list=list;                                                           
        return action;
}

}

what the right way to add SfList to OapAdvAction ? I try some methods but it doesn't work?

Thanx

---===---
Alphex Kaanoken
Senior developer of Crew IT research labs
web: http://crew.org.ru
mailto:[email protected]

#1 lore raw
Quoted 11 lines · Alphex K.

so I'm have a two linked lists

so and functions to operate with there

        while(list->next!=NULL)
          {
             list=list->next;
             id++;
          }
        list->next=(struct SfList  *) malloc(sizeof(SfList));
        list=list->next;

Note: it's simpler, and more efficient, to add new members to the start of the list.

Quoted 3 lines

and for OapAdvAction structure

what the right way to add SfList to OapAdvAction ?

	action->list = list;

Or do you want to add a copy of the list?

Quoted 1 line

I try some methods but it doesn't work?

You are trying to add to action->list, but action->list starts out uninitialised (i.e. containing garbage).

Signature · 1 line
Glynn Clements <[email protected]>
author
#2 lore raw
Quoted 16 lines · On Fri, 30 Apr 2004 05:25:52 +0100 Glynn Clements <[email protected]>…
Quoted 11 lines · Alphex K.

so I'm have a two linked lists

so and functions to operate with there

        while(list->next!=NULL)
          {
             list=list->next;
             id++;
          }
        list->next=(struct SfList  *) malloc(sizeof(SfList));
        list=list->next;
Quoted 2 lines

Note: it's simpler, and more efficient, to add new members to the start of the list.

I khown this, but in a project I must use this method

Quoted 8 lines
Quoted 4 lines

and for OapAdvAction structure

what the right way to add SfList to OapAdvAction ?

	action->list = list;

Or do you want to add a copy of the list?

Yes, I want to have a copy of SfList * list, which initializated out of function , in OapAdvAction lnked list

Quoted 5 lines
Quoted 2 lines

I try some methods but it doesn't work?

You are trying to add to action->list, but action->list starts out uninitialised (i.e. containing garbage).

How I can initializate it in OapAdvAction * AddAdvAction(OapAdvAction * action,char *name,char *author,
char *note,int cl_id,SfList * list) ?? it's my question.
Of couse I can initializate it in this function, but it doesn't work ;-(
What is better working method to add ?

Quoted 7 lines
Signature · 6 lines
-- 
Glynn Clements <[email protected]>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

---===---
Alphex Kaanoken
Senior developer of Crew IT research labs
web: http://crew.org.ru
mailto:[email protected]

#3 lore raw
Quoted 7 lines · Alphex K.
Quoted 4 lines
Quoted 1 line

I try some methods but it doesn't work?

You are trying to add to action->list, but action->list starts out uninitialised (i.e. containing garbage).

How I can initializate it in OapAdvAction * AddAdvAction(OapAdvAction * action,char *name,char *author,
char *note,int cl_id,SfList * list) ?? it's my question.

Set:

	action->list=NULL;

before attempting to add entries with AddSfEntry().

Signature · 1 line
Glynn Clements <[email protected]>
author
#4 lore raw
Quoted 9 lines · On Fri, 30 Apr 2004 14:38:06 +0100 Glynn Clements <[email protected]>…
Quoted 5 lines · Alphex K.
Quoted 4 lines
Quoted 1 line

I try some methods but it doesn't work?

You are trying to add to action->list, but action->list starts out uninitialised (i.e. containing garbage).

Quoted 9 lines
Quoted 2 lines

How I can initializate it in OapAdvAction * AddAdvAction(OapAdvAction * action,char *name,char *author,
char *note,int cl_id,SfList * list) ?? it's my question.

Set:

	action->list=NULL;

before attempting to add entries with AddSfEntry().

thanx
It work, but when I try to read from OapAdvAction I fecth "Seg fault" or incorrect value in on of the fields - and broken SfList * list; - But when I try view this in first attempt - it worked normally Where is my error -
so it's function show content - (to gtk clist)
void _FillActionsTable(OapAdvAction *list,int what)
{
OapAdvAction * lp=NULL;
OapAdvAction * temp=NULL;
temp=OapCpyAdvAction(temp,actions);
lp=OapCpyAdvAction(lp,actions);
char *entr[3];
while(lp!=NULL)

     {                                                                               
        entr[0]=calloc(1,8);                                                         
        sprintf(entr[0],"-%d-",lp->id);                                                
        entr[1]=lp->name;                                                            
        entr[2]=lp->author;                                                          
        entr[3]=lp->note;                                                            
        if(what==0)                                                                  
          gtk_clist_append(GTK_CLIST(imanactlist),entr);                             
        else                                                                         
          gtk_clist_append(GTK_CLIST(iviewactactionlist),entr);                      
        free(entr[1]);                                                               
        lp=lp->next;                                                                 
     }                                                                               

lp=OapFreeAdvAction(lp); actions=OapFreeAdvAction(actions); actions=OapCpyAdvAction(actions,temp); temp=OapFreeAdvAction(temp);
}
where actions is global variable (OapAdvAction * actions) and functions -
OapAdvAction * OapFreeAdvAction(OapAdvAction * action)
{
while(action!=NULL)

     {                                                                               
        action->list=FreeSfList(action->list);                                       
        action=DelAdvAction(action,action->id);                                      
     }                                                                               

return action;
}

OapAdvAction * OapCpyAdvAction(OapAdvAction * daction,OapAdvAction * saction) {
daction=NULL;
while(saction!=NULL)

     {                                                                               
        daction=AddAdvAction(daction,saction->name,saction->author,saction->note,    
                             saction->cl_id,saction->list);                          
        saction=saction->next;                                                       
     }                                                                               

return daction;
}

thanx for advance.

Quoted 6 lines
Signature · 5 lines
Glynn Clements <[email protected]>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

---===---
Alphex Kaanoken
Senior developer of Crew IT research labs
web: http://crew.org.ru
mailto:[email protected]

#5 lore raw
Quoted 23 lines · Alphex K.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

I try some methods but it doesn't work?

You are trying to add to action->list, but action->list starts out uninitialised (i.e. containing garbage).

Quoted 8 lines
Quoted 2 lines

How I can initializate it in OapAdvAction * AddAdvAction(OapAdvAction * action,char *name,char *author,
char *note,int cl_id,SfList * list) ?? it's my question.

Set:

	action->list=NULL;

before attempting to add entries with AddSfEntry().

It work, but when I try to read from OapAdvAction I fecth "Seg fault" or incorrect value in on of the fields - and broken SfList * list; - But when I try view this in first attempt - it worked normally
Where is my error -

so it's function show content - (to gtk clist) void _FillActionsTable(OapAdvAction *list,int what)

Quoted 13 lines
{                                                                                    
   OapAdvAction * lp=NULL;
   OapAdvAction * temp=NULL;
   temp=OapCpyAdvAction(temp,actions);
   lp=OapCpyAdvAction(lp,actions);
   char *entr[3];
   while(lp!=NULL)
     {
        entr[0]=calloc(1,8);
        sprintf(entr[0],"-%d-",lp->id);
        entr[1]=lp->name;
        entr[2]=lp->author;
        entr[3]=lp->note;

For a start, "entr" only has three elements (entr[0] to entr[2]), so you shouldn't be assigning to entr[3]. Doing so will typically overwrite one of the other local variables.

Signature · 1 line
Glynn Clements <[email protected]>