Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在链接列表中插入之前?_C++_Linked List_Swap_Singly Linked List - Fatal编程技术网

C++ 在链接列表中插入之前?

C++ 在链接列表中插入之前?,c++,linked-list,swap,singly-linked-list,C++,Linked List,Swap,Singly Linked List,我试图分析插入到前面给出的链表中的代码,但我相信代码中有一些多余的东西: // new_n->next = p->next; // p->next = new_n; 我相信没有必要指定p->next=new\n;既然我们已经把新的东西移到了p->next所指的东西上了? / 模板 bool insertBeforeS(SNode*节点、对象前、对象新值){ SNode*new_n=新SNode(newValue); 对

我试图分析插入到前面给出的链表中的代码,但我相信代码中有一些多余的东西:

             //  new_n->next = p->next;
               // p->next = new_n;
我相信没有必要指定p->next=new\n;既然我们已经把新的东西移到了p->next所指的东西上了? /

模板
bool insertBeforeS(SNode*节点、对象前、对象新值){
SNode*new_n=新SNode(newValue);
对于(SNode*p=node;p!=nullptr&&p->next!=nullptr;p=p->next){
如果(p->data==之前){
新建->数据=p->数据;
p->data=newValue;
新建->下一步=p->下一步;
p->next=new\n;
返回true;
}
}
返回false;
}

在列表中找到before值后,新节点将其值与“before”节点的值交换

然后,该“before”节点应指向新节点,新节点应指向“before”节点早期指向的节点

所以这些声明

new_n->next = p->next;
p->next = new_n;
通过设置节点旁边的数据成员来执行所需任务

最初

| before-value| pointer to the next node|
新节点

| new-value | nullptr |
然后交换值

| new-value| pointer to the next node|
| before-value | nullptr |
然后在这个声明之后

new_n->next = p->next;
p->next = new_n;
我们有

| new-value| pointer to the next node|
| before-value | pointer to the next node |
| new-value| pointer to the new node |
| before-value | pointer to the next node |
然后在这个声明之后

new_n->next = p->next;
p->next = new_n;
我们有

| new-value| pointer to the next node|
| before-value | pointer to the next node |
| new-value| pointer to the new node |
| before-value | pointer to the next node |

请注意,如果未找到具有“before”值的节点,则函数存在内存泄漏。

在列表中找到“before”值后,新节点将其值与“before”节点的值交换

然后,该“before”节点应指向新节点,新节点应指向“before”节点早期指向的节点

所以这些声明

new_n->next = p->next;
p->next = new_n;
通过设置节点旁边的数据成员来执行所需任务

最初

| before-value| pointer to the next node|
新节点

| new-value | nullptr |
然后交换值

| new-value| pointer to the next node|
| before-value | nullptr |
然后在这个声明之后

new_n->next = p->next;
p->next = new_n;
我们有

| new-value| pointer to the next node|
| before-value | pointer to the next node |
| new-value| pointer to the new node |
| before-value | pointer to the next node |
然后在这个声明之后

new_n->next = p->next;
p->next = new_n;
我们有

| new-value| pointer to the next node|
| before-value | pointer to the next node |
| new-value| pointer to the new node |
| before-value | pointer to the next node |

请注意,如果未找到具有before值的节点,则函数会出现内存泄漏。

相关的,您可能需要重新考虑(a)
before
从一开始就匹配列表中的第一项,或者(b)before之前没有任何匹配时会发生什么(即,新分配的
新节点会发生什么情况)…相关的,您可能需要重新考虑(a)
之前的
从一开始就匹配列表中的第一项,或者(b)之前的
都不匹配(即,新分配的
新节点会发生什么情况)。。