c++ Recursion Function works correctly but starts index at 0. Need it to start at 1

  c++, data-structures, linked-list, recursion

Problem is that index is starting at 0, how do I rewrite this to start at 1.

This function is using recursion

Linked List Array:

1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 42 -> 10 -> NULL

Input: getPosition(head,42);

Outputs: 9

What I want:

Output: 10

template <class T>
int getPosition(Node<T>* head, T element) {
    //check for null
    if (head == NULL){
      return -1;
    }
    //continue if not null
    else{
      //if current elemnt does not equal wanted element
      if(head-> data != element){
        //temp var to iterate through using recursion
        int temp = getPosition(head -> next,element);
          //if temp is not equal to element, add 1 to temp
          if (temp!= -1){
              return temp+1;
          }
          //return -1 to keep loop going
          else{
              return -1;
          }
      }
      //if element found, return 0
      else{
        return 0;
      }
    }
}

Source: Windows Questions C++

LEAVE A COMMENT