Finding an Element in a LinkedList – C++ 在链表中查找元素

huangapple go评论110阅读模式
英文:

Finding an Element in a LinkedList - C++

问题

我正在尝试编写一个函数 find(Node* node, int valueInput),该函数从给定的节点开始,返回具有值 valueInput 的节点的索引,如果 valueInput 不存在则返回 -1。

以下是代码:

  1. #include <iostream>
  2. class Node {
  3. public:
  4. int value;
  5. Node* next = NULL;
  6. };
  7. int find(Node* node, int valueInput)
  8. {
  9. Node* tempNode = node;
  10. int count = 0;
  11. while (tempNode != NULL)
  12. {
  13. if (tempNode->value == valueInput)
  14. {
  15. return count;
  16. }
  17. ++count;
  18. tempNode = tempNode->next;
  19. }
  20. return -1;
  21. }
  22. int main()
  23. {
  24. int valueInput, currentValue;
  25. char arrow;
  26. Node* node1, * previous, * head;
  27. std::cin >> valueInput;
  28. std::cin >> currentValue;
  29. node1 = new Node();
  30. node1->value = currentValue;
  31. node1->next = NULL;
  32. previous = node1;
  33. head = node1;
  34. while (std::cin)
  35. {
  36. std::cin >> arrow;
  37. std::cin >> currentValue;
  38. node1 = new Node();
  39. node1->value = currentValue;
  40. previous->next = node1;
  41. previous = node1;
  42. }
  43. std::cout << find(head, valueInput);
  44. }

目前,我的程序始终返回 -1。

示例输入和输出:

示例输入:
5
1->2->5

示例输出:
2

英文:

I am trying to write a function find(Node* node, int valueInput) that starts at the given node and returns the index of the node with the value valueInput, and return -1 if valueInput does not exist.

Here is the code

  1. #include &lt;iostream&gt;
  2. class Node {
  3. public:
  4. int value;
  5. Node* next = NULL;
  6. };
  7. int find(Node* node, int valueInput)
  8. {
  9. Node* tempNode = node;
  10. int count = 0;
  11. while (tempNode != NULL)
  12. {
  13. if (tempNode-&gt;value == 0)
  14. {
  15. break;
  16. }
  17. ++count;
  18. tempNode = tempNode-&gt;next;
  19. }
  20. return -1;
  21. }
  22. int main()
  23. {
  24. int valueInput, currentValue;
  25. int listValues;
  26. char arrow;
  27. Node* node1, * previous, * head;
  28. std::cin &gt;&gt; valueInput;
  29. std::cin &gt;&gt; currentValue;
  30. node1 = new Node();
  31. node1-&gt;value = currentValue;
  32. node1-&gt;next = NULL;
  33. previous = node1;
  34. head = node1;
  35. while (std::cin)
  36. {
  37. std::cin &gt;&gt; arrow;
  38. std::cin &gt;&gt; arrow;
  39. std::cin &gt;&gt; currentValue;
  40. node1 = new Node();
  41. node1-&gt;value = currentValue;
  42. previous-&gt;next = node1;
  43. previous = node1;
  44. }
  45. std::cout &lt;&lt; find(head, valueInput);
  46. }

Currently, my program returns -1 always
Sample input and outputs:

Sample Input:
5
1->2->5

Sample Output:
2

答案1

得分: 0

这是因为您的代码只有一个return -1语句。您应该使用return count而不是break。此外,您始终与0进行比较,而不是valueInput

  1. int find(Node* node, int valueInput)
  2. {
  3. Node* tempNode = node;
  4. int count = 0;
  5. while (tempNode != nullptr)
  6. {
  7. if (tempNode->value == valueInput)
  8. {
  9. return count;
  10. }
  11. ++count;
  12. tempNode = tempNode->next;
  13. }
  14. return -1;
  15. }

您还应该使用nullptr而不是NULL

英文:

That's because your code only has a return -1 statement. You should return count instead of break. Also, you are always comparing against 0 instead of the valueInput

  1. int find(Node* node, int valueInput)
  2. {
  3. Node* tempNode = node;
  4. int count = 0;
  5. while (tempNode != nullptr)
  6. {
  7. if (tempNode-&gt;value == valueInput)
  8. {
  9. return count;
  10. }
  11. ++count;
  12. tempNode = tempNode-&gt;next;
  13. }
  14. return -1;
  15. }

You should also be using nullptr instead of NULL

huangapple
  • 本文由 发表于 2020年1月7日 01:44:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定