Need help with implementing some Linked List functions. Attached below are all my files. My main concerns are with the assign and the reverse function. Haven't had the chance to try splice yet so if anyone has tips for that as well please help!
I will attach snippets for those who don't want to dig through the files here:
in header (defined lines 77-81, 107-111):
// ex. my_list.assign(5, 3)
// should produce a list containing <3, 3, 3>
// Any existing data should be safely removed if necessary
void assign(int count, const T &value);
// Reverse the order of elements in the list
// The sentinel node should still be at the end (tail) of the reversed list
// COMMON INTERVIEW CHALLENGE
// Can be done with clever manipulation of pointers, no new/delete needed!
void reverse();
In .hpp starting line 131:
clear();
head = new ListNode<T>;
head -> data = value;
ListNode<T> *currentNode = head;
for(int i = 1; i < count; i++) {
ListNode<T> *newNode = new ListNode<T>;
newNode->data = value;
currentNode->next = newNode;
currentNode = newNode;
}
currentNode->next = nullptr;
num_elements = count;
}
template <typename T> void MyList<T>::reverse() {
ListNode<T> *currentNode = head;
ListNode<T> *nextNode = nullptr;
ListNode<T> *prevNode = nullptr;
while(currentNode != nullptr){
nextNode = currentNode -> next;
currentNode -> next = prevNode;
prevNode = currentNode;
currentNode = nextNode;
}
head = prevNode;
}