Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote