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

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

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:

#include <iostream>

using namespace std;
int value, count, value2, count2;

struct node
{
    int data;
    node * next;
};

node *list = nullptr;
node * p;
node * q;
node * r;

void insertFront()
{
    cout << "ENTER A VALUE=";
    cin >> value;

    if (list == nullptr)
    {
        p = new node();
        p->data = value;
        p->next = nullptr;
        list = p;
    }
    else
    {
        p = new node();
        p->data = value;
        p->next = list;
        list = p;
    }
}

void display()
{
    p = list;
    while (p != nullptr)
    {
        cout << p->data << " ";
        p = p->next;
    }

    cout << p->data << endl;
}

void insertSpec()
{
    cout << "Enter the number after which you want to enter a node=";
    cin >> value;

    if (list == nullptr && value > 1)
    {
        cout << "THERE IS NO OTHER NODE SO YOU CANNOT PUT A NODE AT " << value;
    }

    if (list != nullptr && value > 0)
    {
        cout << "ENTER THE NUMBER YOU WANT TO ENTER=";
        cin >> value2;
        count = 1;
        count2 = 1;
        while (count != (value))
        {
            p = p->next;
            count++;
        }

        while (count2 != (value + 1))
        {
            q = q->next;
            count2++;
        }

        r = new node();
        r->data = value2;
        p->next = r;
        r->next = q;
    }
}

int main()
{
    int choice;
    cout << "1) Insert at front" << endl;
    cout << "5) Insert at specified place" << endl;
    cout << "9) Display" << endl << endl;
    while (choice != 99)
    {
        cout << "Your choice:";
        cin >> choice;

        switch (choice)
        {
        case 1:
            {
                insertFront();
                break;
            }
        case 5:
            {
                insertSpec();
                break;
            }
        case 9:
            {
                display();
                break;
            }
        case 99:
            {
                cout << "PROGRAM TERMINATED :)";
                break;
            }
        }
    }

    return 0;
}

答案1

得分: 1

在`insertSpec()`函数中,如果用户指定的节点编号超过了列表的末尾,第一个循环就会产生*未定义行为*,第二个循环由于`q`从未被赋值为指向有效的`node`,也会产生*未定义行为*

`display()`函数在循环结束后尝试访问`p->data`时会产生*未定义行为*,因为此时`p`为`nullptr`。

可以尝试使用以下方式改进:

```cpp
#include <iostream>
using namespace std;

struct node
{
    int data;
    node *next = nullptr;
};

node *list = nullptr;

void insertFront()
{
    int value;

    cout << "输入一个值=";
    cin >> value;

    list = new node{value, list};
}

void display()
{
    for (node *p = list; p != nullptr; p = p->next)
    {
        cout << p->data << " ";
    }
    cout << endl;
}

void insertSpec()
{
    int number, value;

    cout << "输入要在哪个节点后插入新节点=";
    cin >> number;
    
    if (number < 1)
    {
        cout << "无效的节点编号!" << endl;
        return;
    }

    node **p = &list;
    int n = number;

    while (*p != nullptr && n > 0)
    {
        p = &((*p)->next);
        --n;
    }

    if (n > 0)
    {
        cout << "无法在" << number << "后插入节点" << endl;
        return;
    }

    cout << "输入要添加的值=";
    cin >> value;

    *p = new node{value, *p};
}

int main()
{
    int choice;

    cout << "1) 在前面插入" << endl;
    cout << "5) 在指定位置插入" << endl;
    cout << "9) 显示" << endl;
    cout << "99) 退出" << endl << endl;

    do
    {
        cout << "您的选择:";
        cin >> choice;

        switch (choice)
        {
        case 1:
            insertFront();
            break;

        case 5:
            insertSpec();
            break;

        case 9:
            display();
            break;
		}
	}
    while (choice != 99);

    cout << "程序已结束 :)";
    return 0;
}

在线演示


<details>
<summary>英文:</summary>
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`.
`display()` has *undefined behavior* when it tries to access `p-&gt;data` after the loop is finished because `p` is `nullptr` by then.
Try something more like this instead:
```c++
#include &lt;iostream&gt;
using namespace std;
struct node
{
int data;
node *next = nullptr;
};
node *list = nullptr;
void insertFront()
{
int value;
cout &lt;&lt; &quot;ENTER A VALUE=&quot;;
cin &gt;&gt; value;
list = new node{value, list};
}
void display()
{
for(node *p = list; p != nullptr; p = p-&gt;next)
{
cout &lt;&lt; p-&gt;data &lt;&lt; &quot; &quot;;
}
cout &lt;&lt; endl;
}
void insertSpec()
{
int number, value;
cout &lt;&lt; &quot;Enter the node number after which you want to enter a new node=&quot;;
cin &gt;&gt; number;
if (number &lt; 1)
{
cout &lt;&lt; &quot;INVALID NODE NUMBER!&quot; &lt;&lt; endl;
return;
}
node **p = &amp;list;
int n = number;
while (*p != nullptr &amp;&amp; n &gt; 0)
{
p = &amp;((*p)-&gt;next);
--n;
}
if (n &gt; 0)
{
cout &lt;&lt; &quot;YOU CANNOT PUT A NODE AFTER &quot; &lt;&lt; number &lt;&lt; endl;
return;
}
cout &lt;&lt; &quot;ENTER THE VALUE YOU WANT TO ADD=&quot;;
cin &gt;&gt; value;
*p = new node{value, *p};
}
int main()
{
int choice;
cout &lt;&lt; &quot;1) Insert at front&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;5) Insert at specified place&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;9) Display&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;99) Exit&quot; &lt;&lt; endl &lt;&lt; endl;
do
{
cout &lt;&lt; &quot;Your choice:&quot;;
cin &gt;&gt; choice;
switch (choice)
{
case 1:
insertFront();
break;
case 5:
insertSpec();
break;
case 9:
display();
break;
}
}
while (choice != 99);
cout &lt;&lt; &quot;PROGRAM TERMINATED :)&quot;;
return 0;
}

Online Demo

答案2

得分: 0

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

    void display()
    {
    p = list; // 将指向1
    while (p != nullptr)
    {
    cout << p->data << " "; // 打印: 1:2:3
    p = p->next; // p 指向: 2:3:NULL
    }
    cout << p->data << endl;
    // 基本上这里的 p 指向 null,因此这个语句导致了问题,如果你删除了这个 cout,代码将正常运行。
    cout << endl; // 仅使用
    }
    
  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

    void display()
    {
    p = list; // will point to the 1
    while (p != nullptr)
    {
    cout &lt;&lt; p-&gt;data &lt;&lt; &quot; &quot;; //printing: 1:2:3
    p = p-&gt;next;// p point to: 2:3:NULL
    }
    cout &lt;&lt; p-&gt;data &lt;&lt; endl;
    // Basically p here is pointing to null so this statement is causing the problem if you removed this cout the code will run fine.
    cout&lt;&lt;endl; // Use only
    }
    
  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:

确定