pls help. link list prob

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 1
Reputation: aoi is an unknown quantity at this point 
Solved Threads: 0
aoi aoi is offline Offline
Newbie Poster

pls help. link list prob

 
0
  #1
Sep 27th, 2008
  1. t=malloc(sizeof(NODE));
  2. t->lname[i]=ln[i];
  3. t->fname[i]=fn[i];
  4. t->add[i]=ad[i];
  5. t->telno[i]=tn[i];
  6. t->next=NULL;
  7.  
  8. i++;
  9.  
  10.  
  11. if(*h==NULL){
  12.  
  13. printf("\nCreating record...");
  14. *h=t;
  15.  
  16. }
  17. else{
  18. p=*h;
  19.  
  20. if(p->next!=NULL)
  21. {
  22. while(p->next!=NULL){
  23. if(strcmp(p->lname[i],ln[i])>=0){
  24. p->prev->next=t;
  25. t->next=p;
  26. }
  27. p->prev=p;
  28. }
  29. }
  30. else
  31. {
  32. if(strcmp(p->lname[i],ln[i])>=0)
  33. {
  34. p->next=t;
  35. t->prev=p;
  36. t->next=NULL;
  37. }
  38. else
  39. {
  40. p->prev=t;
  41. t->next=p;
  42. }
  43. *h=p;
  44. }
  45. }

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: pls help. link list prob

 
0
  #2
Sep 27th, 2008
Hmm lets try adding code tags, so it becomes a bit more readable. Also couldyou give us a little clue as to what problems you are actually having cause thats alot to look over when we don't know what were looking for

Chris
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: pls help. link list prob

 
0
  #3
Sep 27th, 2008
I presume h is a pointer to the first node? Like:
NODE* h;

In that case, this code:
  1. if(*h==NULL)
  2. //should be:
  3. if (h == NULL)
Because (*h) is actualy the node itself, and not a pointer.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 113
Reputation: ahamed101 is an unknown quantity at this point 
Solved Threads: 14
ahamed101's Avatar
ahamed101 ahamed101 is offline Offline
Junior Poster

Re: pls help. link list prob

 
0
  #4
Sep 28th, 2008
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

  1. 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

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: pls help. link list prob

 
0
  #5
Sep 28th, 2008
t=(NODE *)malloc(sizeof(NODE));
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

  1. 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"
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: pls help. link list prob

 
0
  #6
Sep 30th, 2008
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..

  1.  
  2. /* lets insert 5 elements in the list */
  3. for(j = 0; j < 5; j++){
  4.  
  5. /* there are no elements in the list. add the first one */
  6. if(h==NULL){
  7. h=&t[j];
  8. }
  9. else{
  10. p = h;
  11. done = 0;
  12. /* lets try to figure out the node, around which we want to
  13.   insert this new element */
  14. while(p->next != NULL && !done){
  15. if(strcmp(p->lname,t[j].lname) >= 0){
  16. p = p->next;
  17. }
  18. else{
  19. done = 1;
  20. }
  21. }
  22. /* do we want to insert the element before the current node
  23.   or after the current node ? */
  24. if(strcmp(p->lname,t[j].lname)>=0){
  25. /* after -- manipulate the prev and next pointers
  26.   for the new node. the prev pointer for the
  27.   node after the current node and the next pointer
  28.   for the current node */
  29. t[j].next = p->next;
  30. t[j].prev = p;
  31. if(p->next)
  32. p->next->prev = &t[j];
  33. p->next = &t[j];
  34. }
  35. else{
  36. /* before -- manipulate the prev and next pointers
  37.   for the new node. the next pointer for the
  38.   node before the current node and the prev pointer
  39.   for the current node */
  40. t[j].prev = p->prev;
  41. t[j].next = p;
  42. if(p->prev)
  43. p->prev->next = &t[j];
  44. p->prev = &t[j];
  45. }
  46. }
  47.  
  48. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 113
Reputation: ahamed101 is an unknown quantity at this point 
Solved Threads: 14
ahamed101's Avatar
ahamed101 ahamed101 is offline Offline
Junior Poster

Re: pls help. link list prob

 
0
  #7
Sep 30th, 2008
Originally Posted by ssharish2005 View Post
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

  1. void Insert( NODE **h, data );

???

ssharish
Thanks for that info... i wasn't aware of that...
regards,
Ahamed.
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC