| | |
query: allocating memory to nested linked lists
![]() |
•
•
Join Date: Oct 2004
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2004
Posts: 29
Reputation:
Solved Threads: 0
I think your main problem is here
K.
C Syntax (Toggle Plain Text)
//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
![]() |
Similar Threads
- Nested Linked List Class? (C++)
- Please help! Unknown memory leak with linked list nodes... (C)
- Basic question about linked lists (C++)
- Looking up and displaying values in linked lists (C++)
- Linked Lists (C)
- C/ Need Help with Linked Lists please (C)
- stack of linked lists (C++)
Other Threads in the C Forum
- Previous Thread: I am from China
- Next Thread: I did it can U? (a challenge from me to you)
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush fgets file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming pyramidusingturboccodes read recursion recv repetition research scanf scheduling segmentationfault send shape socket socketprograming socketprogramming stack standard string suggestions systemcall test unix urboc user voidmain() wab win32api windows.h





