943,809 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 801
  • C RSS
Sep 27th, 2008
0

pls help. link list prob

Expand Post »
  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.
aoi
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aoi is offline Offline
1 posts
since Sep 2008
Sep 27th, 2008
0

Re: pls help. link list prob

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Sep 27th, 2008
0

Re: pls help. link list prob

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.
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Sep 28th, 2008
0

Re: pls help. link list prob

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?
Reputation Points: 51
Solved Threads: 14
Junior Poster
ahamed101 is offline Offline
114 posts
since Jul 2008
Sep 28th, 2008
0

Re: pls help. link list prob

Quote ...
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.
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006
Sep 30th, 2008
0

Re: pls help. link list prob

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. }
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Sep 30th, 2008
0

Re: pls help. link list prob

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...
Reputation Points: 51
Solved Threads: 14
Junior Poster
ahamed101 is offline Offline
114 posts
since Jul 2008

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: serial port access
Next Thread in C Forum Timeline: Find a character in a string





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


Follow us on Twitter


© 2011 DaniWeb® LLC