Hi. Can you guys help me out with this link list method. The method issuppose to say if a list is in ascendent order or descendent order. I have this right now:

template <typename ListElement>
int List <ListElement>::order()


{
     
    NodePtr temp1;
    NodePtr temp2;
    
                   
     
   temp1 = Head;
   temp2 = temp1->Next;

         while(temp1 != NULL) {
   
     if(temp1->Info.Age > temp2->Info.Age) {
            
     cout << "Descendent order" << endl;
    
          } else if(temp1->Info.Age < temp2->Info.Age) {
                cout << "Ascendent Order" << endl;
              
                } else
                
                cout << "No order" << endl;
           
                temp2 = temp1->Next;
                
                  
                  } 
  return 0;
}

The methods, but when the list is in disorder for example (1,3,5,4) still says is in ascendent order. How could i fix that? Thanks
I know that the loop has to check not only the next node but the whole list, but i cant get that part.

>when the list is in disorder for example (1,3,5,4) still
>says is in ascendent order. How could i fix that?
You wait until after the loop to print the result, obviously:

node *curr = head;

bool ascending = true;
bool descending = true;

while ( curr->next != 0 ) {
  if ( curr->data < curr->next->data )
    descending = false;
  else if ( curr->data > curr->next->data )
    ascending = false;

  curr = curr->next;
}

if ( ascending )
  cout<<"Ascending order\n";

if ( descending )
  cout<<"Descending order\n";

if ( !ascending && !descending )
  cout<<"No order\n";

many thanks! I will try this logic in my insert method (which does not have to insert repeated elements) I hope to get it right! :)

Hey again. I edited the code and it gives me an error.

template <typename ListElement>
void List <ListElement>::order()


{
    
    
    
    NodePtr temp1;
 
     
    bool ascending = true;
    bool descending = true;
                   
     
    temp1 = Head;
  
   
   

         while(temp1 != 0) {
   
      if (temp1->Info.Age > temp1->Next->Info.Age) {
            
      ascending = false;
   
     } else if (temp1->Info.Age < temp1->Next->Info.Age) 
                
      descending = false;     
                 
      temp1 = temp1->Next;
                
          } 
      
      if ( ascending ) {
  
      cout<<"Ascending order\n";


    }else if ( descending ){

      cout<<"Descending order\n";

       
 
    }  else if ( !ascending && !descending )
 
      cout<<"No order\n";


}

At first it worked but the display was in an infinite loop (the ascending order message continue forever)

Ok my mistake. I fixed it, sorry :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.