Loop won't quit!

This is a discussion on Loop won't quit! within the C++ Programming forums, part of the General Programming Boards category; I know this may soud silly, but I have been messing with this itty-bitty loop all night! Can anyone see ...

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600

    Loop won't quit!

    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:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    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:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571


    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 01:13 AM
  2. Replies: 8
    Last Post: 12-01-2008, 09:09 AM
  3. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-30-2006, 11:59 PM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21