943,537 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 373
  • C++ RSS
Dec 4th, 2008
0

Help (link lists referent)

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008
Dec 4th, 2008
0

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:
C++ Syntax (Toggle Plain Text)
  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";
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 4th, 2008
0

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008
Dec 4th, 2008
0

Re: Help (link lists referent)

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

C++ Syntax (Toggle Plain Text)
  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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008
Dec 4th, 2008
0

Re: Help (link lists referent)

Ok my mistake. I fixed it, sorry
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Debug Assertion Failed! Error - how do I fix this?
Next Thread in C++ Forum Timeline: Simple Recursion (Multiplying by using Addition)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC