lundi 10 février 2020

Swap Linked list objects

Following code works for sorting of the list (Peter,10) (John,32) (Mary,50) (Carol,31) Ordered lists: List 1: (Carol,31) (John,32) (Mary,50) (Carol,31) however the last element is not getting sorted, please help to suggest where Iam going wrong.

LinkedList& LinkedList::order()
{
  current=head;
  Node* temp=current;
  Node* current2=current->get_next();

  do
  {
    while(current2 != NULL)
    {
        const student_type& cstudent= current->get_data();
        const student_type& tstudent= current2->get_data();

        if( cstudent >  tstudent)
        {
            temp->set_Node(current->get_data());
            const student_type& tempstudent= temp->get_data();
            current->set_Node(current2->get_data());
            current2->set_Node(temp->get_data());

        }current2=current2->get_next();
     }current=current->get_next();
   }while(current != NULL);
   temp= NULL;
   return *this;
 } 

Aucun commentaire:

Enregistrer un commentaire