fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node
  5. {
  6. int value;
  7. Node *next;
  8. };
  9.  
  10. int find(Node* head, int n) {
  11. int index = -1;
  12. while (head) {
  13. ++index;
  14. if (head->value == n) {
  15. return index;
  16. }
  17. head = head->next;
  18. }
  19. return -1;
  20. }
  21.  
  22.  
  23. int main()
  24. {
  25. Node *list = new Node{1, new Node{2, new Node{3, nullptr}}};
  26.  
  27. cout << find(list, 1) << endl;
  28. cout << find(list, 2) << endl;
  29. cout << find(list, 3) << endl;
  30. cout << find(list, 4) << endl;
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0s 4248KB
stdin
Standard input is empty
stdout
0
1
2
-1