linked list

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

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

linked list

 
0
  #1
Oct 3rd, 2008
Im just getting garbage in the output. It outputs the correct number of items but its not the right numbers. Im not sure why it is happening.

The text file just has numbers like this:
12
34
56
34
67
23
64
23

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. class List
  7. {
  8. public:
  9. void Insert(int);
  10. void Print();
  11. };
  12.  
  13. struct node
  14. {
  15. int age;
  16. node *nxt;
  17. };
  18. node *start_ptr = 0;
  19. node *current;
  20.  
  21. void List::Insert(int age)
  22. { node *temp, *temp2; // Temporary pointers
  23.  
  24. // Reserve space for new node and fill it with data
  25. temp = new node;
  26. temp->age;
  27.  
  28. temp->nxt = NULL;
  29.  
  30. // Set up link to this node
  31. if (start_ptr == NULL)
  32. { start_ptr = temp;
  33. current = start_ptr;
  34. }
  35. else
  36. { temp2 = start_ptr;
  37. // We know this is not NULL - list not empty!
  38. while (temp2->nxt != NULL)
  39. { temp2 = temp2->nxt;
  40. // Move to next link in chain
  41. }
  42. temp2->nxt = temp;
  43. }
  44. }
  45. void List::Print()
  46. {
  47. node *temp;
  48. temp = start_ptr;
  49.  
  50. if (temp == NULL)
  51. cout << "The list is empty" << endl;
  52. else
  53. { while (temp != NULL)
  54. { // Display details for what temp points to
  55. cout << "Age : " << temp->age << " ";
  56.  
  57. if (temp == current)
  58. cout << " <-- Current node";
  59. cout << endl;
  60. temp = temp->nxt;
  61.  
  62. }
  63. cout << "End of list!" << endl;
  64. }
  65. }
  66. void main()
  67. {
  68. start_ptr = NULL;
  69. List display;
  70. fstream inFile;
  71. node number;
  72. inFile.open("int.txt");
  73. while(inFile>>number.age)
  74. {
  75. display.Insert(number.age);
  76. }
  77. display.Print();
  78. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: linked list

 
1
  #2
Oct 3rd, 2008
Originally Posted by JackDurden View Post
Im just getting garbage in the output. It outputs the correct number of items but its not the right numbers. Im not sure why it is happening.

The text file just has numbers like this:
12
34
56
34
67
23
64
23

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. class List
  7. {
  8. public:
  9. void Insert(int);
  10. void Print();
  11. };
  12.  
  13. struct node
  14. {
  15. int age;
  16. node *nxt;
  17. };
  18. node *start_ptr = 0;
  19. node *current;
  20.  
  21. void List::Insert(int age)
  22. { node *temp, *temp2; // Temporary pointers
  23.  
  24. // Reserve space for new node and fill it with data
  25. temp = new node;
  26. temp->age;
  27.  
  28. temp->nxt = NULL;
  29.  
  30. // Set up link to this node
  31. if (start_ptr == NULL)
  32. { start_ptr = temp;
  33. current = start_ptr;
  34. }
  35. else
  36. { temp2 = start_ptr;
  37. // We know this is not NULL - list not empty!
  38. while (temp2->nxt != NULL)
  39. { temp2 = temp2->nxt;
  40. // Move to next link in chain
  41. }
  42. temp2->nxt = temp;
  43. }
  44. }
  45. void List::Print()
  46. {
  47. node *temp;
  48. temp = start_ptr;
  49.  
  50. if (temp == NULL)
  51. cout << "The list is empty" << endl;
  52. else
  53. { while (temp != NULL)
  54. { // Display details for what temp points to
  55. cout << "Age : " << temp->age << " ";
  56.  
  57. if (temp == current)
  58. cout << " <-- Current node";
  59. cout << endl;
  60. temp = temp->nxt;
  61.  
  62. }
  63. cout << "End of list!" << endl;
  64. }
  65. }
  66. void main()
  67. {
  68. start_ptr = NULL;
  69. List display;
  70. fstream inFile;
  71. node number;
  72. inFile.open("int.txt");
  73. while(inFile>>number.age)
  74. {
  75. display.Insert(number.age);
  76. }
  77. display.Print();
  78. }

These should really be data members of your List class, not global variables:

  1. node *start_ptr;
  2. node *current;

This line does nothing:

  1. temp->age;

I assume you meant:

  1. temp->age = age;
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: linked list

 
0
  #3
Oct 3rd, 2008
Thanks for the help!
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC