I know this may soud silly, but I have been messing with this itty-bitty loop all night! Can anyone see the flaw?
Code:~List() { List * ptr = this->Next(); List * memory; while(ptr) { memory = ptr; ptr = ptr->Next(); delete memory; } }
I know this may soud silly, but I have been messing with this itty-bitty loop all night! Can anyone see the flaw?
Code:~List() { List * ptr = this->Next(); List * memory; while(ptr) { memory = ptr; ptr = ptr->Next(); delete memory; } }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Whenever you are adding nodes to your linked list do you set the new node in front of it to NULL? Also shouldn't you start at the head and not the one after the head? What if only one element exists?
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
The next and prev ptr's are initialized in the constructor. Thus, the loop should run correctly and also without error even if this->next == NULL...
Here's the whole template:
Code://..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //... A generic Linked List Class ...// template < class data_type > class List { private: data_type data; List * next; List * prev; public: List(): next(0), prev(0), data(0) {} //..............................................................................// //..............................................................................// List * Get_Tail() { List * tail = this; while(tail->next) tail = tail->next; return tail; } //..............................................................................// //... The "New()" member function implements the element-linkage, etc... //..............................................................................// List * New() { List * old_tail = Get_Tail(); List * new_element = new List; if( new_element == NULL ) return old_tail; old_tail->next = new_element; new_element->prev = old_tail; return new_element; } //..............................................................................// //..............................................................................// List * Current_Position(List * That = NULL) { static List * current_position = this; //... effectively a static member variable accessed only thru this func... if( That == NULL ) return current_position = current_position->next; return current_position = That; } //..............................................................................// //..............................................................................// List * Push( data_type Data ) { static bool instantiated = false; List * new_element = List::New(); if(!instantiated) { Current_Position(this); instantiated = true; } new_element->data = Data; return new_element; } //..............................................................................// //..............................................................................// data_type Pop( bool *reached_end ) { *reached_end = false; List *current = List::Current_Position(); if( !current->next ) { *reached_end = true; List::Current_Position(this); } return current->data; } //..............................................................................// //..............................................................................// List * Next() { return next; } List * Previous() { return prev; } ~List() { List * ptr = this->Next(); List * memory; while(ptr) { memory = ptr; ptr = ptr->Next(); delete memory; } } }; //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// //..............................................................................// int main(int argc, char *argv[]) { List <float> nums; float i = 0; bool done = false; while( i++ < 60 ) nums.Push( i ); do{ printf( "\n%g", nums.Pop( &done ) ); }while( !done ); printf("\n\nMade It Here..."); return 0; }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
The this pointer was pointing at the tail.
Code:~List() { List * ptr = this; while( ptr = this->Previous() ) {} List * memory; while(ptr) { memory = ptr; ptr = ptr->Next(); delete memory; } } };
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers