I can use temp. position, length, headPtr, pre Implement the getNodeAtPosition f
ID: 3800331 • Letter: I
Question
I can use temp. position, length, headPtr, pre
Implement the getNodeAtPosition function of the LinkedList class .
Node * LinkedList::getNodeAtPosition(int position)
The job of this function is to return a pointer to the node the node at the given position. If the list is empty, position <= 0 or position > length, then the function should return the null pointer. This is a helper function that can be used by various other member functions (e.g. insertAtPosition, removeAtPosition, getValue. setValue).
Explanation / Answer
Hi, Please find my code.
Please let me know in case of any issue.
Node * LinkedList::getNodeAtPosition(int position){
// I am assuming that 'length' is defined
if(headPtr == NULL || position <= 0 || position > length)
return NULL;
Node *temp = headPtr;
for(int i=1; i<position-1; i++)
temp = temp.getNext();
return temp;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.