linked lists

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 67
Reputation: mauro21pl is an unknown quantity at this point 
Solved Threads: 0
mauro21pl mauro21pl is offline Offline
Junior Poster in Training

linked lists

 
0
  #1
Sep 17th, 2008
Hi
Does anybody knows what is wrong with taht code? Why it says it is empty link list? It should fill with the numbers I enter. Could anybody help to answer the question?

Thanks

  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. struct Node
  5. {
  6. int number;
  7. struct Node *link;
  8. };
  9.  
  10. typedef struct Node* NodePtr;
  11.  
  12. void printNode(NodePtr head);
  13. void insertNode(NodePtr *head,int numb);
  14.  
  15. int main(void)
  16. {
  17. NodePtr head;
  18. int data;
  19.  
  20. head=NULL;
  21.  
  22. printf("\nWhat is the number : ");
  23. scanf("%d", &data);
  24.  
  25. while(data>0)
  26. {
  27. insertNode(&head,data);
  28. printNode(head);
  29. printf("\nWhat is the number : ");
  30. scanf("%d", &data);
  31. }
  32.  
  33.  
  34.  
  35. getch();
  36. return 0;
  37. }
  38.  
  39. void printNode(NodePtr head)
  40. {
  41. if (head==NULL)
  42. printf("The linked lists is NULL");
  43. else
  44. {
  45. printf("The nodes contain the numbers: \n");
  46. while (head!=NULL)
  47. {
  48. printf("%d -->",head->number);
  49. head=head->link;
  50. }
  51. printf(" NULL");
  52. }
  53. }
  54.  
  55. void insertNode(NodePtr *head,int numb)
  56. {
  57. NodePtr newPtr;
  58. NodePtr previousPtr;
  59. NodePtr currentPtr;
  60.  
  61. newPtr=malloc(sizeof(struct Node));
  62. newPtr->number=numb;
  63. newPtr->link=NULL;
  64.  
  65. previousPtr=NULL;
  66. currentPtr=*head;
  67.  
  68. while(currentPtr!=NULL && numb>currentPtr->number)
  69. {
  70. previousPtr=currentPtr;
  71. currentPtr=currentPtr->link;
  72. }
  73.  
  74. if (previousPtr==NULL)
  75. {
  76. newPtr->link=currentPtr;
  77. currentPtr=newPtr;
  78. }
  79. else
  80. {
  81. previousPtr->link=newPtr;
  82. newPtr->link=currentPtr;
  83. }
  84. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,856
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: linked lists

 
1
  #2
Sep 17th, 2008
Take a close look at insertNode and tell me where you update head.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 67
Reputation: mauro21pl is an unknown quantity at this point 
Solved Threads: 0
mauro21pl mauro21pl is offline Offline
Junior Poster in Training

Re: linked lists

 
0
  #3
Sep 17th, 2008
I got that one, thanks, I will keep going with linked lists then ...
Reply With Quote Quick reply to this message  
Reply

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




Views: 286 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC