home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,914 of 33,346   
   alexo to All   
   Re: insert [sorted] a node in a linked l   
   11 Mar 13 09:38:38   
   
   From: alelvb@inwind.it   
      
   Following your advices I've done a bit more than a half of the work.   
   The Head and List_Element were removed so was removed the call to   
   push. Now the list is sorted almost completely. The sole exception   
   is the very first node inserted that remains as last Node.   
   Any suggestion?   
      
   thank you   
      
   #include    
      
   using std::cout;   
   using std::endl;   
   using std::cerr;   
      
   class Node   
   {   
       public:   
      
           Node() : next(0) {}   
           Node(int value): next(0), itsDatum(value) { }   
          ~Node() { cout << "node " << itsDatum << " deleted." << endl; }   
      
           int getValue() const { return itsDatum; }   
           Node * next;   
      
       private:   
      
           int itsDatum;   
   };   
      
   class Linked_List   
   {   
       public:   
      
           Linked_List() { head = new Node(); }   
           ~Linked_List() { delete head; }   
      
           void empty_list();   
            int pop();   
           void print_list();   
           void push(int);   
           void push_back(int);   
           void insert_sorted(int);   
      
      
       private:   
      
           Node *head;   
      
           void insert_after(int, Node *);   
   };   
      
   // the function main of the program   
      
   int main()   
   {   
       Linked_List * ll = new Linked_List();   
      
       ll->insert_sorted(1);   
       ll->insert_sorted(2);   
       ll->insert_sorted(-3);   
       ll->insert_sorted(4);   
       ll->insert_sorted(5);   
       ll->insert_sorted(6);   
       ll->insert_sorted(0);   
       ll->insert_sorted(8);   
       ll->insert_sorted(-1);   
      
       ll->print_list();   
      
       ll->empty_list();   
      
       delete ll;   
      
       return 0;   
   }   
      
   // insert a node after another one   
      
   void Linked_List::insert_after(int value, Node * after_me)   
   {   
       Node * new_node = new Node(value);   
       new_node->next = after_me->next;   
       after_me->next = new_node;   
   }   
      
   // inserts value in a sorted way   
      
   void Linked_List::insert_sorted(int value)   
   {   
       Node * temp = head->next;   
       Node * after_me = head;   
      
      
       if(temp == 0) // if the list is empty   
       {   
           insert_after(value, head);   
           print_list();   
       }   
       else   
       {   
               while( (temp->next != 0) && (temp->getValue() < value ) )   
               {   
                   after_me = temp;   
                   temp = temp->next;   
               }   
               insert_after(value, after_me);   
   //            print_list(); // for debugging only   
       }   
   }   
      
   // pops an element from the top of the list   
      
   int Linked_List::pop()   
   {   
       Node * popped = head->next;   
       int to_return = popped->getValue();   
      
       head->next = popped->next;   
      
       delete popped;   
       return to_return;   
   }   
      
   // prints the list checking whether it is an empty list   
      
   void Linked_List::print_list()   
   {   
       if(head->next != 0)   
       {   
           Node *to_print = head->next; // to_print points to the first Node   
      
           while(to_print != 0)   
           {   
               cout << to_print->getValue() << " ";   
               to_print = to_print->next;   
           }   
           cout << endl;   
       }   
       else   
       {   
           cout << "\athe list is empty." << endl;   
       }   
   }   
      
   // delete the entire list   
      
   void Linked_List::empty_list()   
   {   
       while(head->next != 0)   
       {   
           pop();   
       }   
   }   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca