Class Link-List help!?

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

Join Date: Apr 2008
Posts: 27
Reputation: DevC++4.9.9.2 is an unknown quantity at this point 
Solved Threads: 1
DevC++4.9.9.2 DevC++4.9.9.2 is offline Offline
Light Poster

Class Link-List help!?

 
0
  #1
Oct 6th, 2008
I am stuck, im trying to insert and print out this list of numbers from a file, however i just get an infinite loop every time... any suggestions?

  1. // This is the implementation file for class List
  2.  
  3. #include <iostream>
  4. #include <stddef.h> // to access NULL
  5. #include "List.h"
  6. #include<fstream.h>
  7. using namespace std;
  8.  
  9. typedef NodeType* NodePtr;
  10.  
  11. struct NodeType
  12. {
  13. ItemType item;
  14. NodePtr next;
  15. };
  16.  
  17. List::List()
  18. // Post: listPtr is set to NULL.
  19. {
  20. listPtr = NULL;
  21. length = 0;
  22. }
  23.  
  24. //***********************************************************
  25.  
  26. //List::List(const List& otherList)
  27. // Copy-constructor for List.
  28. //{
  29.  
  30. //}
  31.  
  32. //***********************************************************
  33.  
  34.  
  35. bool List::IsThere(ItemType item) const
  36. // Post: If item is in the list IsThere is
  37. // True; False, otherwise.
  38. {
  39. NodeType *cur_pos;
  40. cur_pos = listPtr;
  41. if (cur_pos == NULL)
  42. return false;
  43. while((cur_pos -> next)!= NULL)
  44. {
  45. if (cur_pos -> item == item)
  46. return true;
  47. cur_pos = cur_pos -> next;
  48. }
  49. if (cur_pos -> item == item)
  50. return true;
  51. return false;
  52. }
  53.  
  54. //***********************************************************
  55.  
  56. void List::Insert(ItemType item)
  57. // Pre: item is not already in the list.
  58. // Post: item is the first item in the list.
  59. {
  60. NodeType *ptr;
  61. ptr = new NodeType;
  62. ptr->item;
  63. ptr->next = listPtr;
  64. listPtr = ptr;
  65. delete ptr;
  66. length++;
  67. }
  68.  
  69. //***********************************************************
  70.  
  71. void List::Delete(ItemType item)
  72. // Pre: item is in the list.
  73. // Post: item is no longer in the list.
  74. {
  75. // FILL IN Code.
  76. }
  77.  
  78. //***********************************************************
  79.  
  80. void List::Print() const
  81. // Post: Items on the list are printed on the screen.
  82. {
  83. if (listPtr == NULL)
  84. cout << "List is empty\n";
  85. else
  86. {
  87. NodeType *ptr;
  88. ptr = listPtr;
  89. while(ptr->next !=NULL)
  90. {
  91. cout << ptr->item << endl;
  92. ptr = ptr->next;
  93. }
  94. cout << ptr->item << endl;
  95. delete ptr;
  96. }
  97. }
  98.  
  99. //***********************************************************
  100.  
  101. int List::Length() const
  102. // Post: Number of items have been counted; result returned.
  103. {
  104. return length;
  105. }
  106.  
  107. //***********************************************************
  108.  
  109. List::~List()
  110. // Post: All the components are deleted.
  111. {
  112. // FILL IN Code.
  113. }
  114. int main()
  115. {
  116. List mylist;
  117. ifstream data;
  118. data.open("int.dat");
  119. if (!data)
  120. cout << "Error opening file\n";
  121. else
  122. {
  123. int item;
  124. while (!data.eof())
  125. {
  126. data >> item;
  127. cout << "Item: " << item << endl;
  128. mylist.Insert(item);
  129. }
  130. mylist.Print();
  131. }
  132. system("PAUSE");
  133. return 0;
  134. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Class Link-List help!?

 
0
  #2
Oct 6th, 2008
62: ptr->item;
what about it?


89. while(ptr->next !=NULL)
figure out why in ptr->next never becomes null

I'm betting on the problem being in your insert function.
Last edited by chococrack; Oct 6th, 2008 at 12:53 pm.
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 27
Reputation: DevC++4.9.9.2 is an unknown quantity at this point 
Solved Threads: 1
DevC++4.9.9.2 DevC++4.9.9.2 is offline Offline
Light Poster

Re: Class Link-List help!?

 
0
  #3
Oct 6th, 2008
i fixedthe line 62 to :
ptr->item = item;
but why would ptr->next never = NULL ?
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: Class Link-List help!?

 
0
  #4
Oct 6th, 2008
Why on earth are you deleting ptr in List::insert()

delete ptr;
Once you delete it your ListPtr points to garbage, and essentially at the end of the insert you will have no valid elements in the list.

You need to delete your list in the destructor.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 27
Reputation: DevC++4.9.9.2 is an unknown quantity at this point 
Solved Threads: 1
DevC++4.9.9.2 DevC++4.9.9.2 is offline Offline
Light Poster

Re: Class Link-List help!?

 
0
  #5
Oct 6th, 2008
wow, thank you, i feel very noob lol
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