Write a function member called ascendingOrder() to the linked list to check if t
ID: 3621560 • Letter: W
Question
Write a function member called ascendingOrder() to the linked list to check if the elements in the list are in ascending order or not. If the list elements are in ascending order, return true. Otherwise, return false. Please not that if the list is empty or has one element, you should return true. The following present the prototype of the function:
bool ascendingOrder():
Explanation / Answer
Let me know if you have any questions. Please rate. :) Assuming your linked list has a structure somewhat similar to this: class linkedlist { public: node * head; bool ascendingOrder(); }; and the node is: class node { int data; node * next; }; Then the function, ascendingOrder, can be implemented as such: bool linkedlist::ascendingOrder() { node * cur = head; while (cur != NULL) { if (cur->next != NULL) { if (cur->data > cur->next->data) { // these two elements are in descending order, so return false return false; } } // move forward to the next node in the list cur = cur->next; } // if we get out of the loop without returning false, then all items are ascending return true; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.