query: allocating memory to nested linked lists

Reply

Join Date: Oct 2004
Posts: 3
Reputation: sachin kumar is an unknown quantity at this point 
Solved Threads: 0
sachin kumar sachin kumar is offline Offline
Newbie Poster

query: allocating memory to nested linked lists

 
0
  #1
Oct 25th, 2004
Hi All
I' m a new member of Dani Web Community.my problem is with nested linked list implementation.when i insert second node in linked list, it always overwrites the first.
my declarations are as follows:

struct dataset
{
int key ;
char value[30];
struct dataset *ptrdataset;

}*ptrd;
struct geometry
{
char type[25] ;
struct dataset *ptrdataset;
struct geometry *ptrgeometry;

}*ptrg;
struct dxfdata
{
struct geometry *ptrgeometry ;
struct dxfdata *ptrdxfdata;

}*dxf,*temp2,*ptrdxfe;

//and I allocate memory as
dxf =(struct dxfdata *)malloc(sizeof(struct dxfdata));
dxf->ptrgeometry = (struct geometry *)malloc(sizeof(struct geometry));
temp = dxf;
temp->ptrdxfdata = NULL;
temp->ptrgeometry = NULL;

ptrgtempe=(struct geometry *)malloc(sizeof(struct geometry));
ptrgtempe->ptrgeometry = NULL;
ptrgtempe->ptrdataset=NULL;
if(ptrdxfe->ptrgeometry == NULL)
{
ptrdxfe->ptrgeometry = ptrgtempe;
ptrge = ptrgtempe;
}
else
{
while(ptrdxfe->ptrgeometry!=NULL)
ptrdxfe->ptrgeometry = ptrdxfe->ptrgeometry->ptrgeometry;
ptrdxfe->ptrgeometry = ptrgtempe;
}
addentitydataset(ptrdxfe,ptr);//ptrdxfe is a node of dxfdata and ptr
//is a node of of a single linked
// tnat is to added to the ptrdxfe
void addentitydataset(struct dxfdata *adddxf,struct sarray *sarray)
{

struct dataset *ptrdump1;

temp2= adddxf;


ptrdump1 = (struct dataset *)malloc(sizeof(struct dataset));
ptrdump1->key = atof(sarray->element);
strcpy(ptrdump1->value , sarray->ptrsarray->element);
ptrdump1->ptrdataset = NULL;
if(temp2->ptrgeometry->ptrdataset == NULL)
{
ptrdxfe->ptrgeometry->ptrdataset=ptrdump1;
ptrdtempe=ptrdxfe->ptrgeometry->ptrdataset;
ptrgtempe=ptrdxfe->ptrgeometry;
}
else
{
while(temp2->ptrgeometry->ptrdataset!=NULL)
temp2->ptrgeometry->ptrdataset=temp2->ptrgeometry->ptrdataset->ptrdataset;
temp2->ptrgeometry->ptrdataset = ptrdump1;

}
}

for example it works correctly for the first node addition in geometry(ptrgeometry) as ptrdxfe->ptrgeometry->ptrdataset=ptrdump1;
but for the second time it overwrites this node.
Please help me to sort out this problem.
sachin kumar
email id :sachin.kumar@optimal-point.com
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 29
Reputation: ZuK is an unknown quantity at this point 
Solved Threads: 0
ZuK ZuK is offline Offline
Light Poster

Re: query: allocating memory to nested linked lists

 
0
  #2
Oct 25th, 2004
I think your main problem is here
  1. //and I allocate memory as
  2. dxf =(struct dxfdata *)malloc(sizeof(struct dxfdata));
  3. dxf->ptrgeometry = (struct geometry *)malloc(sizeof(struct geometry));
  4. temp = dxf; // here you are assigning dxf to temp;
  5. temp->ptrdxfdata = NULL;
  6. temp->ptrgeometry = NULL; // dxf->ptrgeometry is lost now
  7. // you are actually setting dxf->ptrgeometry to NULL
K.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC