| | |
Help (link lists referent)
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 17
Reputation:
Solved Threads: 0
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:
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.
C++ Syntax (Toggle Plain Text)
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.
Last edited by lmastex; Dec 4th, 2008 at 11:04 am.
>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:
>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)
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";
I'm here to prove you wrong.
•
•
Join Date: Jan 2008
Posts: 17
Reputation:
Solved Threads: 0
Hey again. I edited the code and it gives me an error.
At first it worked but the display was in an infinite loop (the ascending order message continue forever)
C++ Syntax (Toggle Plain Text)
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)
![]() |
Other Threads in the C++ Forum
- Previous Thread: Debug Assertion Failed! Error - how do I fix this?
- Next Thread: Simple Recursion (Multiplying by using Addition)
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






