Вам нужно объявить i и увеличивать счетчик при прохождении списка:
void checkNode(LinkedList* head, int v)
{
LinkedList* p = head;
int i=0; // Declare i
while (p != NULL) {
if (p->data == v) {
cout << i; // output i
return;
}
++i; // Increment index counter
p = p->next;
}
// we've searched through the entire list
cout << -1; // not found
}
В случае, если вы действительно хотите вернуть индекс, как вы указали, а не так, как предлагает код. Это будет выглядеть так:
int checkNode(LinkedList* head, int v)
{
LinkedList* p = head;
int i=0; // Declare i
while (p != NULL) {
if (p->data == v) {
return i;
}
++i; // Increment index counter
p = p->next;
}
// we've searched through the entire list
return -1; // not found
}