如何阻止程序在选择选项5后自行终止

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

how to stop program from terminating itself after chosing option 5

问题

我正在制作在链表中特定位置插入节点的函数。

除了选项5)在特定位置插入和9)显示之外,一切都正常运行。

如果我选择5),程序会要求输入一个数字。在提供数字后,程序会自行终止。

同样,当我选择9),程序会显示节点中的数据,然后终止,尽管我已经放置了一个while循环来停止。

英文:

I am making a function of inserting a node at a specific position in a linked list.

Everything is running fine except for option 5) inserting at a specific position and 9) Display.

If I choose 5), the program asks for a number. After providing a number, it terminates on its own.

Similarly, when I choose 9), the program shows the data in the node and then terminates, yet I have put in a while loop for stoppage.

My main file:

  1. #include <iostream>
  2. using namespace std;
  3. int value, count, value2, count2;
  4. struct node
  5. {
  6. int data;
  7. node * next;
  8. };
  9. node *list = nullptr;
  10. node * p;
  11. node * q;
  12. node * r;
  13. void insertFront()
  14. {
  15. cout << "ENTER A VALUE=";
  16. cin >> value;
  17. if (list == nullptr)
  18. {
  19. p = new node();
  20. p->data = value;
  21. p->next = nullptr;
  22. list = p;
  23. }
  24. else
  25. {
  26. p = new node();
  27. p->data = value;
  28. p->next = list;
  29. list = p;
  30. }
  31. }
  32. void display()
  33. {
  34. p = list;
  35. while (p != nullptr)
  36. {
  37. cout << p->data << " ";
  38. p = p->next;
  39. }
  40. cout << p->data << endl;
  41. }
  42. void insertSpec()
  43. {
  44. cout << "Enter the number after which you want to enter a node=";
  45. cin >> value;
  46. if (list == nullptr && value > 1)
  47. {
  48. cout << "THERE IS NO OTHER NODE SO YOU CANNOT PUT A NODE AT " << value;
  49. }
  50. if (list != nullptr && value > 0)
  51. {
  52. cout << "ENTER THE NUMBER YOU WANT TO ENTER=";
  53. cin >> value2;
  54. count = 1;
  55. count2 = 1;
  56. while (count != (value))
  57. {
  58. p = p->next;
  59. count++;
  60. }
  61. while (count2 != (value + 1))
  62. {
  63. q = q->next;
  64. count2++;
  65. }
  66. r = new node();
  67. r->data = value2;
  68. p->next = r;
  69. r->next = q;
  70. }
  71. }
  72. int main()
  73. {
  74. int choice;
  75. cout << "1) Insert at front" << endl;
  76. cout << "5) Insert at specified place" << endl;
  77. cout << "9) Display" << endl << endl;
  78. while (choice != 99)
  79. {
  80. cout << "Your choice:";
  81. cin >> choice;
  82. switch (choice)
  83. {
  84. case 1:
  85. {
  86. insertFront();
  87. break;
  88. }
  89. case 5:
  90. {
  91. insertSpec();
  92. break;
  93. }
  94. case 9:
  95. {
  96. display();
  97. break;
  98. }
  99. case 99:
  100. {
  101. cout << "PROGRAM TERMINATED :)";
  102. break;
  103. }
  104. }
  105. }
  106. return 0;
  107. }

答案1

得分: 1

  1. `insertSpec()`函数中,如果用户指定的节点编号超过了列表的末尾,第一个循环就会产生*未定义行为*,第二个循环由于`q`从未被赋值为指向有效的`node`,也会产生*未定义行为*
  2. `display()`函数在循环结束后尝试访问`p->data`时会产生*未定义行为*,因为此时`p``nullptr`
  3. 可以尝试使用以下方式改进:
  4. ```cpp
  5. #include <iostream>
  6. using namespace std;
  7. struct node
  8. {
  9. int data;
  10. node *next = nullptr;
  11. };
  12. node *list = nullptr;
  13. void insertFront()
  14. {
  15. int value;
  16. cout << "输入一个值=";
  17. cin >> value;
  18. list = new node{value, list};
  19. }
  20. void display()
  21. {
  22. for (node *p = list; p != nullptr; p = p->next)
  23. {
  24. cout << p->data << " ";
  25. }
  26. cout << endl;
  27. }
  28. void insertSpec()
  29. {
  30. int number, value;
  31. cout << "输入要在哪个节点后插入新节点=";
  32. cin >> number;
  33. if (number < 1)
  34. {
  35. cout << "无效的节点编号!" << endl;
  36. return;
  37. }
  38. node **p = &list;
  39. int n = number;
  40. while (*p != nullptr && n > 0)
  41. {
  42. p = &((*p)->next);
  43. --n;
  44. }
  45. if (n > 0)
  46. {
  47. cout << "无法在" << number << "后插入节点" << endl;
  48. return;
  49. }
  50. cout << "输入要添加的值=";
  51. cin >> value;
  52. *p = new node{value, *p};
  53. }
  54. int main()
  55. {
  56. int choice;
  57. cout << "1) 在前面插入" << endl;
  58. cout << "5) 在指定位置插入" << endl;
  59. cout << "9) 显示" << endl;
  60. cout << "99) 退出" << endl << endl;
  61. do
  62. {
  63. cout << "您的选择:";
  64. cin >> choice;
  65. switch (choice)
  66. {
  67. case 1:
  68. insertFront();
  69. break;
  70. case 5:
  71. insertSpec();
  72. break;
  73. case 9:
  74. display();
  75. break;
  76. }
  77. }
  78. while (choice != 99);
  79. cout << "程序已结束 :)";
  80. return 0;
  81. }

在线演示

  1. <details>
  2. <summary>英文:</summary>
  3. In `insertSpec()`, the 1st loop has *undefined behavior* if the user&#39;s specified node number is past the end of the list, and the 2nd loop has *undefined behavior* because `q` is never assigned to point at a valid `node`.
  4. `display()` has *undefined behavior* when it tries to access `p-&gt;data` after the loop is finished because `p` is `nullptr` by then.
  5. Try something more like this instead:
  6. ```c++
  7. #include &lt;iostream&gt;
  8. using namespace std;
  9. struct node
  10. {
  11. int data;
  12. node *next = nullptr;
  13. };
  14. node *list = nullptr;
  15. void insertFront()
  16. {
  17. int value;
  18. cout &lt;&lt; &quot;ENTER A VALUE=&quot;;
  19. cin &gt;&gt; value;
  20. list = new node{value, list};
  21. }
  22. void display()
  23. {
  24. for(node *p = list; p != nullptr; p = p-&gt;next)
  25. {
  26. cout &lt;&lt; p-&gt;data &lt;&lt; &quot; &quot;;
  27. }
  28. cout &lt;&lt; endl;
  29. }
  30. void insertSpec()
  31. {
  32. int number, value;
  33. cout &lt;&lt; &quot;Enter the node number after which you want to enter a new node=&quot;;
  34. cin &gt;&gt; number;
  35. if (number &lt; 1)
  36. {
  37. cout &lt;&lt; &quot;INVALID NODE NUMBER!&quot; &lt;&lt; endl;
  38. return;
  39. }
  40. node **p = &amp;list;
  41. int n = number;
  42. while (*p != nullptr &amp;&amp; n &gt; 0)
  43. {
  44. p = &amp;((*p)-&gt;next);
  45. --n;
  46. }
  47. if (n &gt; 0)
  48. {
  49. cout &lt;&lt; &quot;YOU CANNOT PUT A NODE AFTER &quot; &lt;&lt; number &lt;&lt; endl;
  50. return;
  51. }
  52. cout &lt;&lt; &quot;ENTER THE VALUE YOU WANT TO ADD=&quot;;
  53. cin &gt;&gt; value;
  54. *p = new node{value, *p};
  55. }
  56. int main()
  57. {
  58. int choice;
  59. cout &lt;&lt; &quot;1) Insert at front&quot; &lt;&lt; endl;
  60. cout &lt;&lt; &quot;5) Insert at specified place&quot; &lt;&lt; endl;
  61. cout &lt;&lt; &quot;9) Display&quot; &lt;&lt; endl;
  62. cout &lt;&lt; &quot;99) Exit&quot; &lt;&lt; endl &lt;&lt; endl;
  63. do
  64. {
  65. cout &lt;&lt; &quot;Your choice:&quot;;
  66. cin &gt;&gt; choice;
  67. switch (choice)
  68. {
  69. case 1:
  70. insertFront();
  71. break;
  72. case 5:
  73. insertSpec();
  74. break;
  75. case 9:
  76. display();
  77. break;
  78. }
  79. }
  80. while (choice != 99);
  81. cout &lt;&lt; &quot;PROGRAM TERMINATED :)&quot;;
  82. return 0;
  83. }

Online Demo

答案2

得分: 0

  1. 显示函数:如果我们追踪代码,我们会注意到这里的问题:
    假设列表包含:123

    1. void display()
    2. {
    3. p = list; // 将指向1
    4. while (p != nullptr)
    5. {
    6. cout << p->data << " "; // 打印: 1:2:3
    7. p = p->next; // p 指向: 2:3:NULL
    8. }
    9. cout << p->data << endl;
    10. // 基本上这里的 p 指向 null,因此这个语句导致了问题,如果你删除了这个 cout,代码将正常运行。
    11. cout << endl; // 仅使用
    12. }
  2. 对于 insertSpec 函数:

首先,你需要声明 pq 并设置它们的值:

> p = list;
> q = list;
> 还有一件事需要考虑,我建议你添加一个变量来保存列表的长度... 主要原因是为了验证索引与长度是否匹配。
如果你需要任何帮助或遇到任何问题,随时可以问。希望这对你有所帮助。

英文:

Well for the two problems:

  1. Display function: If we track the code, we will notice the problem here:
    Assume the list contained: 123

    1. void display()
    2. {
    3. p = list; // will point to the 1
    4. while (p != nullptr)
    5. {
    6. cout &lt;&lt; p-&gt;data &lt;&lt; &quot; &quot;; //printing: 1:2:3
    7. p = p-&gt;next;// p point to: 2:3:NULL
    8. }
    9. cout &lt;&lt; p-&gt;data &lt;&lt; endl;
    10. // Basically p here is pointing to null so this statement is causing the problem if you removed this cout the code will run fine.
    11. cout&lt;&lt;endl; // Use only
    12. }
  2. For the insertSpec function:

First thing you need to declare p and q and set their values:

> p = list;
> q = list;
> More thing to be considered I prefer if you a variable to have the
> length of the list .... the main reason is to validate the indexes
> with the length.
if you need any help or faced any problem you can ask any time.
Hope this was helpful.

huangapple
  • 本文由 发表于 2023年4月4日 03:13:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923007.html
匿名

发表评论

匿名网友

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

确定