DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Help (link lists referent) (http://www.daniweb.com/forums/thread160713.html)

lmastex Dec 4th, 2008 11:02 am
Help (link lists referent)
 
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.

Narue Dec 4th, 2008 11:24 am
Re: Help (link lists referent)
 
>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";

lmastex Dec 4th, 2008 12:00 pm
Re: Help (link lists referent)
 
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! :)

lmastex Dec 4th, 2008 12:29 pm
Re: Help (link lists referent)
 
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)

lmastex Dec 4th, 2008 12:38 pm
Re: Help (link lists referent)
 
Ok my mistake. I fixed it, sorry :P


All times are GMT -4. The time now is 4:05 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC