| | |
pls help. link list prob
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 1
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
t=malloc(sizeof(NODE)); t->lname[i]=ln[i]; t->fname[i]=fn[i]; t->add[i]=ad[i]; t->telno[i]=tn[i]; t->next=NULL; i++; if(*h==NULL){ printf("\nCreating record..."); *h=t; } else{ p=*h; if(p->next!=NULL) { while(p->next!=NULL){ if(strcmp(p->lname[i],ln[i])>=0){ p->prev->next=t; t->next=p; } p->prev=p; } } else { if(strcmp(p->lname[i],ln[i])>=0) { p->next=t; t->prev=p; t->next=NULL; } else { p->prev=t; t->next=p; } *h=p; } }
could help me with my code? i can't make the 3rd node and when i try to make the 2nd node and print it the 2nd node will replace the data inside my first node.
Last edited by aoi; Sep 27th, 2008 at 11:04 am.
I presume h is a pointer to the first node? Like:
NODE* h;
In that case, this code:
Because (*h) is actualy the node itself, and not a pointer.
NODE* h;
In that case, this code:
c Syntax (Toggle Plain Text)
if(*h==NULL) //should be: if (h == NULL)
Hi there,
Please try to be more specific about your queries... Looks like this is a doubly linked list... a queue?...
Like Sci@phy said, if h is a pointer to first node, you should use
or if it is an argument you have passed to this function of type NODE**, then its correct...
Further more, do a type cast while memory allocating, like
How many nodes are you trying to add at one go?... And are you trying to add the node in a reverse manner?
Please try to be more specific about your queries... Looks like this is a doubly linked list... a queue?...
Like Sci@phy said, if h is a pointer to first node, you should use
C Syntax (Toggle Plain Text)
if(h == NULL)
or if it is an argument you have passed to this function of type NODE**, then its correct...
Further more, do a type cast while memory allocating, like
C Syntax (Toggle Plain Text)
t=(NODE *)malloc(sizeof(NODE));
How many nodes are you trying to add at one go?... And are you trying to add the node in a reverse manner?
regards,
Ahamed.
Ahamed.
•
•
•
•
t=(NODE *)malloc(sizeof(NODE));
@OP for me most of of the things seems to be fine, except the head node pointer deferenceing like it has been mentioned by others already. Unless you've got your head node like this
c Syntax (Toggle Plain Text)
void Insert( NODE **h, data );
???
ssharish
Last edited by ssharish2005; Sep 28th, 2008 at 8:15 pm.
"Any fool can know, point is to understand"
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
I think what you are trying to do is insert values in a doubly linked list in alphabetical order by the last name ?
if so, you aren't going about it correctly .. lets assume you have a set of 5 elements you want to insert in the list; We'll try to insert them in descending order by the last name..
if so, you aren't going about it correctly .. lets assume you have a set of 5 elements you want to insert in the list; We'll try to insert them in descending order by the last name..
C Syntax (Toggle Plain Text)
/* lets insert 5 elements in the list */ for(j = 0; j < 5; j++){ /* there are no elements in the list. add the first one */ if(h==NULL){ h=&t[j]; } else{ p = h; done = 0; /* lets try to figure out the node, around which we want to insert this new element */ while(p->next != NULL && !done){ if(strcmp(p->lname,t[j].lname) >= 0){ p = p->next; } else{ done = 1; } } /* do we want to insert the element before the current node or after the current node ? */ if(strcmp(p->lname,t[j].lname)>=0){ /* after -- manipulate the prev and next pointers for the new node. the prev pointer for the node after the current node and the next pointer for the current node */ t[j].next = p->next; t[j].prev = p; if(p->next) p->next->prev = &t[j]; p->next = &t[j]; } else{ /* before -- manipulate the prev and next pointers for the new node. the next pointer for the node before the current node and the prev pointer for the current node */ t[j].prev = p->prev; t[j].next = p; if(p->prev) p->prev->next = &t[j]; p->prev = &t[j]; } } }
•
•
•
•
Why the hell would you cast the malloc. You shouldn't do that! Read this.
@OP for me most of of the things seems to be fine, except the head node pointer deferenceing like it has been mentioned by others already. Unless you've got your head node like this
c Syntax (Toggle Plain Text)
void Insert( NODE **h, data );
???
ssharish
regards,
Ahamed.
Ahamed.
![]() |
Other Threads in the C Forum
- Previous Thread: serial port access
- Next Thread: Find a character in a string
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives loopinsideloop. lowest match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi





