943,834 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 686
  • C++ RSS
Oct 6th, 2008
0

Class Link-List help!?

Expand Post »
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?

c++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
DevC++4.9.9.2 is offline Offline
39 posts
since Apr 2008
Oct 6th, 2008
0

Re: Class Link-List help!?

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.
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Oct 6th, 2008
0

Re: Class Link-List help!?

i fixedthe line 62 to :
ptr->item = item;
but why would ptr->next never = NULL ?
Reputation Points: 10
Solved Threads: 1
Light Poster
DevC++4.9.9.2 is offline Offline
39 posts
since Apr 2008
Oct 6th, 2008
0

Re: Class Link-List help!?

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

Re: Class Link-List help!?

wow, thank you, i feel very noob lol
Reputation Points: 10
Solved Threads: 1
Light Poster
DevC++4.9.9.2 is offline Offline
39 posts
since Apr 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: Game evaluation - Mastermind
Next Thread in C++ Forum Timeline: Trouble with loops - Calculate sum of even and odd integers





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


Follow us on Twitter


© 2011 DaniWeb® LLC