944,036 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3961
  • C RSS
Oct 25th, 2004
0

query: allocating memory to nested linked lists

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sachin kumar is offline Offline
3 posts
since Oct 2004
Oct 25th, 2004
0

Re: query: allocating memory to nested linked lists

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.
ZuK
Reputation Points: 11
Solved Threads: 0
Light Poster
ZuK is offline Offline
29 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: I am from China
Next Thread in C Forum Timeline: I did it can U? (a challenge from me to you)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC