Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.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+中的挂起+;链表数据结构_C++_Singly Linked List - Fatal编程技术网

C++ 删除C+中的挂起+;链表数据结构

C++ 删除C+中的挂起+;链表数据结构,c++,singly-linked-list,C++,Singly Linked List,我对以下代码有问题。我需要先搜索记录,然后删除它: 它总是删除最后一条记录。即使我想删除中间的第一个rcord string NameForSearch1; cout<<"Enter Your Friend's Name: "<<endl; cin>>NameForSearch1; tempRec->namePers

我对以下代码有问题。我需要先搜索记录,然后删除它:

它总是删除最后一条记录。即使我想删除中间的第一个rcord

string NameForSearch1;

                     cout<<"Enter Your Friend's Name:      "<<endl;
                     cin>>NameForSearch1;

                     tempRec->namePerson=NameForSearch1;

                     if (tempRec==firstRec){//delete the head

                                      tempRec->next=firstRec;
                                      firstRec=tempRec->next;
                                      delete tempRec;
                                      DisplayRec();
                                      }

                     else if (tempRec->next==NULL ){//delete the last record

                                      tempRec2=firstRec->next;
                                      while(tempRec2->next!=tempRec){
                                            tempRec2 = tempRec2->next;
                                      }
                                      tempRec2->next=NULL;                                         
                                      delete tempRec;


                                      }
                     else {
                          //delete anyrecord
                                          tempRec2=firstRec->next;
                                          while(tempRec2->next!=tempRec){
                                                                         tempRec2 = tempRec2->next;
                                                                         }             
                                          tempRec2->next=tempRec->next;
                                          delete tempRec;

                                          }
搜索1的字符串名称;
coutnext=firstRec;
firstRec=tempRec->next;
删除tempRec;
DisplayRec();
}
else如果(tempRec->next==NULL){//删除最后一条记录
tempRec2=firstRec->next;
while(tempRec2->next!=tempRec){
tempRec2=tempRec2->next;
}
tempRec2->next=NULL;
删除tempRec;
}
否则{
//删除任何记录
tempRec2=firstRec->next;
while(tempRec2->next!=tempRec){
tempRec2=tempRec2->next;
}             
tempRec2->next=tempRec->next;
删除tempRec;
}
问题在于:

tempRec2=firstRec->next;
while(tempRec2->next!=tempRec){
  tempRec2 = tempRec2->next;
}
要查找要删除的节点,请比较节点的地址。由于
tempRec
是一个不同的对象,因此您永远不会在链接列表中找到它

相反,您应该将搜索1的
名称与每个节点中的数据进行比较


此外,while循环条件的构造也很糟糕,因为它允许您跑过列表的末尾。您需要确保尚未到达列表的末尾。

经过数小时的调试,我终于能够获得正确的代码:

string NameForSearch1;

                     cout<<"Enter Your Friend's Name:      "<<endl;
                     cin>>NameForSearch1;

                     tempRec=firstRec;

                     if (tempRec->namePerson==NameForSearch1){//delete the head

                                      firstRec=tempRec->next;
                                      delete tempRec;
                                      DisplayRec();
                                      }

                     else 
                     {
                          while (tempRec!=NULL && tempRec->namePerson!=NameForSearch1){
                                               tempRec2=tempRec;
                                               tempRec=tempRec->next;
                                       }

                                       if (tempRec==NULL)
                                       {
                                                         cout<<"NO RECORD FOUND";
                                       }
                                       else if (tempRec->namePerson==NameForSearch1)
                                       {
                                            tempRec2->next=tempRec->next;
                                            delete tempRec;     
                                        }
                       }               
搜索1的字符串名称;
coutnext;
删除tempRec;
DisplayRec();
}
其他的
{
while(tempRec!=NULL&&tempRec->namePerson!=NameForSearch1){
tempRec2=tempRec;
tempRec=tempRec->next;
}
if(tempRec==NULL)
{
coutnext=tempRec->next;
删除tempRec;
}
}               

如果删除挂起,则可能是其中一条记录的析构函数挂起了。如果不确定是否是删除器,则可能是链接列表中有一个循环使循环挂起。首先,不应以这种方式命名变量。tempRec2、tempRec等等。接下来,您的删除迭代不应与“待删除”项isI尝试使用else if(tempRec->next==NULL)删除最后一个节点的位置有所不同。{//删除最后一条记录,它正常工作我尝试使用else if(tempRec->next==NULL)删除最后一个节点{//删除最后一条记录,它就正常工作了..它总是删除最后一条记录