* Some troubles with linked list/tree
@ 2004-04-29 15:39 Alphex K.
2004-04-30 4:25 ` Glynn Clements
0 siblings, 1 reply; 6+ messages in thread
From: Alphex K. @ 2004-04-29 15:39 UTC (permalink / raw)
Cc: linux-c-programming
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:Alphex@Crew.Org.RU
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Some troubles with linked list/tree
2004-04-29 15:39 Some troubles with linked list/tree Alphex K.
@ 2004-04-30 4:25 ` Glynn Clements
2004-04-30 9:15 ` Alphex K.
0 siblings, 1 reply; 6+ messages in thread
From: Glynn Clements @ 2004-04-30 4:25 UTC (permalink / raw)
To: Alphex K.; +Cc: linux-c-programming
Alphex K. wrote:
> 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.
> 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?
> 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).
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Some troubles with linked list/tree
2004-04-30 4:25 ` Glynn Clements
@ 2004-04-30 9:15 ` Alphex K.
2004-04-30 13:38 ` Glynn Clements
0 siblings, 1 reply; 6+ messages in thread
From: Alphex K. @ 2004-04-30 9:15 UTC (permalink / raw)
To: Glynn Clements; +Cc: linux-c-programming
On Fri, 30 Apr 2004 05:25:52 +0100
Glynn Clements <glynn.clements@virgin.net> wrote:
>
>
> Alphex K. wrote:
>
> > 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.
I khown this, but in a project I must use this method
>
> > 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
>
> > 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 ?
>
> --
> Glynn Clements <glynn.clements@virgin.net>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> 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:Alphex@Crew.Org.RU
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Some troubles with linked list/tree
2004-04-30 9:15 ` Alphex K.
@ 2004-04-30 13:38 ` Glynn Clements
2004-04-30 15:18 ` Alphex K.
0 siblings, 1 reply; 6+ messages in thread
From: Glynn Clements @ 2004-04-30 13:38 UTC (permalink / raw)
To: Alphex K.; +Cc: linux-c-programming
Alphex K. wrote:
> > > 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().
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Some troubles with linked list/tree
2004-04-30 13:38 ` Glynn Clements
@ 2004-04-30 15:18 ` Alphex K.
2004-04-30 18:38 ` Glynn Clements
0 siblings, 1 reply; 6+ messages in thread
From: Alphex K. @ 2004-04-30 15:18 UTC (permalink / raw)
To: Glynn Clements; +Cc: linux-c-programming
On Fri, 30 Apr 2004 14:38:06 +0100
Glynn Clements <glynn.clements@virgin.net> wrote:
>
>
> Alphex K. wrote:
>
> > > > 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().
>
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.
> --
> Glynn Clements <glynn.clements@virgin.net>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> 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:Alphex@Crew.Org.RU
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Some troubles with linked list/tree
2004-04-30 15:18 ` Alphex K.
@ 2004-04-30 18:38 ` Glynn Clements
0 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2004-04-30 18:38 UTC (permalink / raw)
To: Alphex K.; +Cc: linux-c-programming
Alphex K. wrote:
> > > > > 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().
>
> 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;
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.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-04-30 18:38 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-04-29 15:39 Some troubles with linked list/tree Alphex K.
2004-04-30 4:25 ` Glynn Clements
2004-04-30 9:15 ` Alphex K.
2004-04-30 13:38 ` Glynn Clements
2004-04-30 15:18 ` Alphex K.
2004-04-30 18:38 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).