DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   query: allocating memory to nested linked lists (http://www.daniweb.com/forums/thread12892.html)

sachin kumar Oct 25th, 2004 1:53 am
query: allocating memory to nested linked lists
 
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

ZuK Oct 25th, 2004 10:19 am
Re: query: allocating memory to nested linked lists
 
I think your main problem is here
//and I allocate memory as
dxf =(struct dxfdata *)malloc(sizeof(struct dxfdata));
dxf->ptrgeometry = (struct geometry *)malloc(sizeof(struct geometry));
temp = dxf;                      // here you are assigning dxf to temp;
temp->ptrdxfdata = NULL; 
temp->ptrgeometry = NULL;        // dxf->ptrgeometry is lost now
                                // you are actually setting dxf->ptrgeometry to NULL
K.


All times are GMT -4. The time now is 6:57 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC