英文:
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'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->data` after the loop is finished because `p` is `nullptr` by then.
Try something more like this instead:
```c++
#include <iostream>
using namespace std;
struct node
{
int data;
node *next = nullptr;
};
node *list = nullptr;
void insertFront()
{
int value;
cout << "ENTER A VALUE=";
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 << "Enter the node number after which you want to enter a new node=";
cin >> number;
if (number < 1)
{
cout << "INVALID NODE NUMBER!" << endl;
return;
}
node **p = &list;
int n = number;
while (*p != nullptr && n > 0)
{
p = &((*p)->next);
--n;
}
if (n > 0)
{
cout << "YOU CANNOT PUT A NODE AFTER " << number << endl;
return;
}
cout << "ENTER THE VALUE YOU WANT TO ADD=";
cin >> value;
*p = new node{value, *p};
}
int main()
{
int choice;
cout << "1) Insert at front" << endl;
cout << "5) Insert at specified place" << endl;
cout << "9) Display" << endl;
cout << "99) Exit" << endl << endl;
do
{
cout << "Your choice:";
cin >> choice;
switch (choice)
{
case 1:
insertFront();
break;
case 5:
insertSpec();
break;
case 9:
display();
break;
}
}
while (choice != 99);
cout << "PROGRAM TERMINATED :)";
return 0;
}
答案2
得分: 0
- 显示函数:如果我们追踪代码,我们会注意到这里的问题:
假设列表包含:123void 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; // 仅使用 }
- 对于
insertSpec
函数:
首先,你需要声明 p
和 q
并设置它们的值:
> p = list;
> q = list;
> 还有一件事需要考虑,我建议你添加一个变量来保存列表的长度... 主要原因是为了验证索引与长度是否匹配。
如果你需要任何帮助或遇到任何问题,随时可以问。希望这对你有所帮助。
英文:
Well for the two problems:
- Display function: If we track the code, we will notice the problem here:
Assume the list contained: 123void display() { p = list; // will point to the 1 while (p != nullptr) { cout << p->data << " "; //printing: 1:2:3 p = p->next;// p point to: 2:3:NULL } cout << p->data << 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<<endl; // Use only }
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论