我正在制作一个在链表中特定位置插入节点的函数。
一切正常,除了选项5)插入在特定位置和9)显示。
如果我选择5),程序要求输入一个数字。在提供一个数字后,它将自动终止。
同样,当我选择9)时,程序显示节点中的数据,然后终止,但我已经放入了一个while
循环用于停止。
主文件:
#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;
}
在insertSpec()
中,如果用户指定的节点号超过列表的末尾,则第一个循环具有未定义的行为,并且第二个循环具有未定义的行为,因为q
从未分配给指向有效node
的点。
display()
在循环结束后试图访问p->data
时具有未定义行为,因为那时p
已经是nullptr
了。
试试这样写:
#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;
}
在线演示对于这两个问题:
- 显示功能:如果我们跟踪代码,我们会注意到这里的问题:假设列表包含:123
void 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 }
insertSpec
函数:
首先你需要声明p
和q
并设置它们的值:
p = list; q = list;
更多的事情要考虑,我更喜欢如果你有一个变量列表的长度....主要原因是为了验证索引长度。如果你需要任何帮助或遇到任何问题,你可以随时问。希望对大家有所帮助。