I am having problem for the splitStrange function, and here is the feedback I get from the professor, but I am not sure how to do it....
Inside the loop, if the odd element is the first in the
list(list_head) then you just call remove();
But if it's just another element in the list you need to delete it
differently
Like with using temp and pointers and delete
//Strange.h
template <typename ElementType>
bool strange(const ElementType &element) {
return true;
}
bool strange(const int &element) {
return (element % 2);
}
// splitStrange() template LewList *LewList::splitStrange() { LewList *rlist = new LewList; ElementType strangeval; if(num_elements == 0) { return rlist; } Node *curr = list_head; Node *temp = nullptr; Node *prev = list_head; while(curr != nullptr) { if(strange(curr->data)) { strangeval = curr->data; for(curr = list_head; curr->data != strangeval; curr = curr->next) { prev = curr; std::cout<data; } prev->next = curr->next; temp = curr; curr = curr->next; num_elements--; rlist->insert(strangeval); } else { curr = curr->next; } } return rlist; }
Let input linked list is sorted in increasing order.
1) If Linked list is empty then make the node as head and return it. 2) If the value of the node to be inserted is smaller than the value of the head node, then insert the node at the start and make it head. 3) In a loop, find the appropriate node after which the input node (let 9) is to be inserted. To find the appropriate node start from the head, keep moving until you reach a node GN (10 in the below diagram) who's value is greater than the input node. The node just before GN is the appropriate node (7). 4) Insert the node (9) after the appropriate node (7) found in step 3.
Get Answers For Free
Most questions answered within 1 hours.