Classes and Linked Nodes

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

Join Date: Sep 2009
Posts: 3
Reputation: sleight is an unknown quantity at this point 
Solved Threads: 0
sleight sleight is offline Offline
Newbie Poster

Classes and Linked Nodes

 
0
  #1
Sep 28th, 2009
So, hi, this is my first post here. I can't find a good source for my problem here.

My problem: Creating a linked node using classes. I managed to create a list of linked nodes but the problem was that I forgot to delete the nodes I created so now I can't run the code again because the memory was full. I can't find a way to delete the previously created nodes.

Here is the code I'm working on so far.
The problem is over at the deletion after the sorting part, around line 129

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <vector>
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. class huffman_tree
  10. {
  11. public:
  12. char _char;
  13. double count;
  14. huffman_tree *next;
  15.  
  16. /*
  17.  
  18.   huffman_tree( int stats[256] );
  19.   string encde_to_string( unsigned char c);
  20.   vector<unsigned char> decode_string (string s);
  21. */
  22. };
  23.  
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27. unsigned char a;
  28. vector <char> input;
  29.  
  30. vector <char> letterstat;
  31. vector <int> numberstat;
  32.  
  33. ifstream IFile;
  34.  
  35. char letter, tempchar;
  36. int x, y, i, j, tempint;
  37. int counter = 0;
  38.  
  39. IFile.open("file.txt");
  40.  
  41. for( ; ; )
  42. {
  43. a = IFile.get();
  44. if ( !IFile.eof() )
  45. {
  46. input.push_back(a);
  47. }
  48. else
  49. {
  50. break;
  51. }
  52. }
  53.  
  54. // check the input against itself for the stats
  55. // arrange the input via the integer equivalent of the characters
  56. // then get the stats
  57. for ( i = 0 ; i < input.size() - 2 ; i++ )
  58. {
  59. for ( j = i + 1 ; j < input.size() - 1 ; j++ )
  60. {
  61. if( input[i] > input[j] )
  62. {
  63. tempchar = input[i];
  64. input[i] = input[j];
  65. input[j] = tempchar;
  66. }
  67.  
  68. }
  69. }
  70.  
  71. // stat checking
  72. cout << "Before numerical sorting." << endl;
  73. for ( i = 0 ; i < input.size() - 1 ; i++ )
  74. {
  75. counter = 0;
  76. for ( j = i ; j < input.size() ; j++ )
  77. {
  78. if( input[i] == input[j] )
  79. {
  80. counter++;
  81. }
  82. else
  83. {
  84. break;
  85. }
  86. }
  87. numberstat.push_back(counter);
  88. letterstat.push_back(input[i]);
  89. i = j - 1;
  90. }
  91.  
  92. for( x = 0 ; x < numberstat.size() ; x++ )
  93. {
  94. cout << letterstat[x] << " ---> "
  95. << numberstat[x] << endl;
  96. }
  97.  
  98.  
  99. // sort lang according to their frequency
  100. cout << endl << "After numerical sorting." << endl;
  101. for ( i = 0 ; i < numberstat.size() - 1 ; i++ )
  102. {
  103. for ( j = i + 1 ; j < numberstat.size() ; j++ )
  104. {
  105. if( numberstat[i] > numberstat[j] )
  106. {
  107. tempchar = letterstat[i];
  108. letterstat[i] = letterstat[j];
  109. letterstat[j] = tempchar;
  110.  
  111. tempint = numberstat[i];
  112. numberstat[i] = numberstat[j];
  113. numberstat[j] = tempint;
  114. }
  115.  
  116. }
  117. }
  118.  
  119. for( x = 0 ; x < numberstat.size() ; x++ )
  120. {
  121. cout << letterstat[x] << " ---> "
  122. << numberstat[x] << endl;
  123. }
  124.  
  125. /*
  126.   constructing list
  127.   */
  128.  
  129. /*
  130.   initialize the starting/head node
  131.   THIS IS IMPORTANT!
  132.   - can be used for reference
  133.   */
  134.  
  135.  
  136. huffman_tree start;
  137. start._char = letterstat[0];
  138. start.count = numberstat[0];
  139.  
  140. huffman_tree current = start;
  141. huffman_tree temp = start;
  142.  
  143. //safety precaution, deleting stuff
  144.  
  145. *current.next = start;
  146. *temp.next = start;
  147.  
  148.  
  149. cout << "Deletion of any prelim tree constructed." << endl;
  150. for( ; current.next != NULL ; current = temp )
  151. {
  152. cout << "testline" << endl;
  153. temp = current.next;
  154. //current = *current.next;
  155. delete [] current.next;
  156.  
  157. }
  158. cout << "Deletion complete." << endl;
  159.  
  160. current = start;
  161. temp = start;
  162.  
  163. //start the list
  164. for( i = 0 ; i < letterstat.size() ; i ++ )
  165. {
  166. current.count = numberstat[i];
  167. current._char = letterstat[i];
  168.  
  169. if( i == letterstat.size() - 1 )
  170. {
  171. current.next = NULL;
  172. break;
  173. }
  174. else
  175. {
  176. current.next = new huffman_tree;
  177.  
  178. current.count = numberstat[i];
  179. current._char = letterstat[i];
  180. current = *current.next;
  181.  
  182. }
  183.  
  184.  
  185. }
  186.  
  187.  
  188. cout << endl << "The List is as follows:" << endl;
  189. *current.next = start;
  190. *temp.next = start;
  191.  
  192. while( current.next != NULL )
  193. {
  194. cout << current.count << " " << current._char << endl;
  195. current = *current.next;
  196. }
  197.  
  198. *current.next = start;
  199. *temp.next = start;
  200.  
  201. //delete again
  202. while( current.next != NULL )
  203. {
  204. current = *current.next;
  205. delete [] temp.next;
  206. temp = current;
  207. }
  208.  
  209.  
  210. cout << "Memory freeing." << endl;
  211.  
  212. system("PAUSE");
  213. return EXIT_SUCCESS;
  214. }

that code is still full of errors. It does an infinite loop on the first deletion loop. I'm not even sure if I need to do a predeletion but I think I need to do it because I need to free up the nodes I dumped over at memory. Is this the right way of doing so?

Help is really appreciated. I'm also getting hints from my classmates that struct is better. I think I should use struct but I would still have the same problems. Again, any help will be appreciated.
Last edited by sleight; Sep 28th, 2009 at 10:27 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Classes and Linked Nodes

 
0
  #2
Sep 29th, 2009
For the most part in c++, a class is almost the same as a struct. The key difference is that members in a struct default to public, in a class they default to private. You made all of the members of your class public so it shouldn't really matter.

Your list handling needs some work though. In your code, start is a tree and current and temp are also trees (that are initialized to be a copy of start).

  1. huffman_tree start;
  2. start._char = letterstat[0];
  3. start.count = numberstat[0];
  4.  
  5. huffman_tree current = start;
  6. huffman_tree temp = start;

I think the code should be something like:
  1. huffman_tree * start = new huffman_tree;
  2.  
  3. start->_char = letterstat[0];
  4. start->count = numberstat[0];
  5. start->next = NULL;
  6.  
  7. huffman_tree * current = start;
  8. huffman_tree * temp = start;

In this case current and temp are pointers to the real start node and not just copies of it.

And you need to be EXTRA careful to NEVER assign to start so you can get back to the head of the list.

Hope that helps some, ask questions if I've left you confused.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 3
Reputation: sleight is an unknown quantity at this point 
Solved Threads: 0
sleight sleight is offline Offline
Newbie Poster

Re: Classes and Linked Nodes

 
0
  #3
Sep 30th, 2009
Oh, maybe that's why I filled up the memory, I created duplicates and not pointers. Thanks.
Reply With Quote Quick reply to this message  
Reply

Tags
class, classes, linkednodes, nodes

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC