Help (link lists referent)

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 17
Reputation: lmastex is an unknown quantity at this point 
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

Help (link lists referent)

 
0
  #1
Dec 4th, 2008
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:

  1. template <typename ListElement>
  2. int List <ListElement>::order()
  3.  
  4.  
  5. {
  6.  
  7. NodePtr temp1;
  8. NodePtr temp2;
  9.  
  10.  
  11.  
  12. temp1 = Head;
  13. temp2 = temp1->Next;
  14.  
  15. while(temp1 != NULL) {
  16.  
  17. if(temp1->Info.Age > temp2->Info.Age) {
  18.  
  19. cout << "Descendent order" << endl;
  20.  
  21. } else if(temp1->Info.Age < temp2->Info.Age) {
  22. cout << "Ascendent Order" << endl;
  23.  
  24. } else
  25.  
  26. cout << "No order" << endl;
  27.  
  28. temp2 = temp1->Next;
  29.  
  30.  
  31. }
  32. return 0;
  33. }

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.
Last edited by lmastex; Dec 4th, 2008 at 11:04 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,576
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help (link lists referent)

 
0
  #2
Dec 4th, 2008
>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:
  1. node *curr = head;
  2.  
  3. bool ascending = true;
  4. bool descending = true;
  5.  
  6. while ( curr->next != 0 ) {
  7. if ( curr->data < curr->next->data )
  8. descending = false;
  9. else if ( curr->data > curr->next->data )
  10. ascending = false;
  11.  
  12. curr = curr->next;
  13. }
  14.  
  15. if ( ascending )
  16. cout<<"Ascending order\n";
  17.  
  18. if ( descending )
  19. cout<<"Descending order\n";
  20.  
  21. if ( !ascending && !descending )
  22. cout<<"No order\n";
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 17
Reputation: lmastex is an unknown quantity at this point 
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

Re: Help (link lists referent)

 
0
  #3
Dec 4th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 17
Reputation: lmastex is an unknown quantity at this point 
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

Re: Help (link lists referent)

 
0
  #4
Dec 4th, 2008
Hey again. I edited the code and it gives me an error.

  1.  
  2. template <typename ListElement>
  3. void List <ListElement>::order()
  4.  
  5.  
  6. {
  7.  
  8.  
  9.  
  10. NodePtr temp1;
  11.  
  12.  
  13. bool ascending = true;
  14. bool descending = true;
  15.  
  16.  
  17. temp1 = Head;
  18.  
  19.  
  20.  
  21.  
  22. while(temp1 != 0) {
  23.  
  24. if (temp1->Info.Age > temp1->Next->Info.Age) {
  25.  
  26. ascending = false;
  27.  
  28. } else if (temp1->Info.Age < temp1->Next->Info.Age)
  29.  
  30. descending = false;
  31.  
  32. temp1 = temp1->Next;
  33.  
  34. }
  35.  
  36. if ( ascending ) {
  37.  
  38. cout<<"Ascending order\n";
  39.  
  40.  
  41. }else if ( descending ){
  42.  
  43. cout<<"Descending order\n";
  44.  
  45.  
  46.  
  47. } else if ( !ascending && !descending )
  48.  
  49. cout<<"No order\n";
  50.  
  51.  
  52. }

At first it worked but the display was in an infinite loop (the ascending order message continue forever)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 17
Reputation: lmastex is an unknown quantity at this point 
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

Re: Help (link lists referent)

 
0
  #5
Dec 4th, 2008
Ok my mistake. I fixed it, sorry
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC