| | |
Classes and Linked Nodes
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 3
Reputation:
Solved Threads: 0
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
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.
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
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <vector> #include <iostream> #include <fstream> using namespace std; class huffman_tree { public: char _char; double count; huffman_tree *next; /* huffman_tree( int stats[256] ); string encde_to_string( unsigned char c); vector<unsigned char> decode_string (string s); */ }; int main(int argc, char *argv[]) { unsigned char a; vector <char> input; vector <char> letterstat; vector <int> numberstat; ifstream IFile; char letter, tempchar; int x, y, i, j, tempint; int counter = 0; IFile.open("file.txt"); for( ; ; ) { a = IFile.get(); if ( !IFile.eof() ) { input.push_back(a); } else { break; } } // check the input against itself for the stats // arrange the input via the integer equivalent of the characters // then get the stats for ( i = 0 ; i < input.size() - 2 ; i++ ) { for ( j = i + 1 ; j < input.size() - 1 ; j++ ) { if( input[i] > input[j] ) { tempchar = input[i]; input[i] = input[j]; input[j] = tempchar; } } } // stat checking cout << "Before numerical sorting." << endl; for ( i = 0 ; i < input.size() - 1 ; i++ ) { counter = 0; for ( j = i ; j < input.size() ; j++ ) { if( input[i] == input[j] ) { counter++; } else { break; } } numberstat.push_back(counter); letterstat.push_back(input[i]); i = j - 1; } for( x = 0 ; x < numberstat.size() ; x++ ) { cout << letterstat[x] << " ---> " << numberstat[x] << endl; } // sort lang according to their frequency cout << endl << "After numerical sorting." << endl; for ( i = 0 ; i < numberstat.size() - 1 ; i++ ) { for ( j = i + 1 ; j < numberstat.size() ; j++ ) { if( numberstat[i] > numberstat[j] ) { tempchar = letterstat[i]; letterstat[i] = letterstat[j]; letterstat[j] = tempchar; tempint = numberstat[i]; numberstat[i] = numberstat[j]; numberstat[j] = tempint; } } } for( x = 0 ; x < numberstat.size() ; x++ ) { cout << letterstat[x] << " ---> " << numberstat[x] << endl; } /* constructing list */ /* initialize the starting/head node THIS IS IMPORTANT! - can be used for reference */ huffman_tree start; start._char = letterstat[0]; start.count = numberstat[0]; huffman_tree current = start; huffman_tree temp = start; //safety precaution, deleting stuff *current.next = start; *temp.next = start; cout << "Deletion of any prelim tree constructed." << endl; for( ; current.next != NULL ; current = temp ) { cout << "testline" << endl; temp = current.next; //current = *current.next; delete [] current.next; } cout << "Deletion complete." << endl; current = start; temp = start; //start the list for( i = 0 ; i < letterstat.size() ; i ++ ) { current.count = numberstat[i]; current._char = letterstat[i]; if( i == letterstat.size() - 1 ) { current.next = NULL; break; } else { current.next = new huffman_tree; current.count = numberstat[i]; current._char = letterstat[i]; current = *current.next; } } cout << endl << "The List is as follows:" << endl; *current.next = start; *temp.next = start; while( current.next != NULL ) { cout << current.count << " " << current._char << endl; current = *current.next; } *current.next = start; *temp.next = start; //delete again while( current.next != NULL ) { current = *current.next; delete [] temp.next; temp = current; } cout << "Memory freeing." << endl; system("PAUSE"); return EXIT_SUCCESS; }
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.
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
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).
I think the code should be something like:
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.
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).
c++ Syntax (Toggle Plain Text)
huffman_tree start; start._char = letterstat[0]; start.count = numberstat[0]; huffman_tree current = start; huffman_tree temp = start;
I think the code should be something like:
c++ Syntax (Toggle Plain Text)
huffman_tree * start = new huffman_tree; start->_char = letterstat[0]; start->count = numberstat[0]; start->next = NULL; huffman_tree * current = start; 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.
![]() |
Similar Threads
- Nested Linked List Class? (C++)
- Linked list (Computer Science)
- homework(easy): Java GUI/Netbeans using java.util.LinkedList (Java)
- Help with pointers and classes (C++)
- linked list question (C)
- Have I gone totally brain dead? cin.get()?? (C++)
- Concordance, Linked Lists and classes (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
- making sorted lists (was: Help Me!) (C)
Other Threads in the C++ Forum
- Previous Thread: list iterator not dereferencable
- Next Thread: Map of Pointers error
| Thread Tools | Search this Thread |
array bank bc30451 binary c++ certifications class classes commonfunctions compile courses date delete draw error errors function functions global gnu graph int java javascript jobs lines linkednodes linuxfoundation method methods nameisnotdeclared nodes oop php recursion reference scale scanner seminars staticmembers strings time training tree variable wxwidgets xml






