[英]How to delete a specific value from a doubly linked list?
I was given a task with a DOUBLY linked list to delete a specific number from the list.我被分配了一个带有双重链接列表的任务,以从列表中删除特定数字。 My code is giving an Access Violation error.
我的代码给出了访问冲突错误。 Even after multiple dry runs, I can't figure out what is wrong.
即使经过多次试运行,我也无法弄清楚出了什么问题。 The task basically is to create a search function which finds a specific number in the linked list, and a deletion function which deletes that specific link.
该任务基本上是创建一个搜索 function 以在链表中找到特定数字,以及删除 function 删除该特定链接。
node* search(int val){
node* cur=head;
while(cur!=NULL){
if(cur->data==val){
cout<<"value found "<<val<<endl;
return cur;
}
cur=cur->next;
}
cout<<"value not exist"<<endl;
return NULL;
}
bool delspval(int val){
node*temp=0;
if(search(val)==NULL){
return 0;
}
else{
temp=search(val);
temp->prev->next=temp->next;
delete temp;
temp=0;
cout<<"specific value "<<val<<" deleted"<<endl;
return 1;
}
}
In the above given code, the line temp->prev->next=temp->next;
在上面给出的代码中,行
temp->prev->next=temp->next;
is giving the error.给出错误。 I'm pretty much a beginner at linked lists, so any help would be appreciated.
我几乎是链表的初学者,所以任何帮助将不胜感激。
minimal working code:最小的工作代码:
#include<iostream>
using namespace std;
class dll{
struct node{
int data;
node *next,*prev;
};
node *head;
public:
dll(){
head=NULL;
}
void inatst(int val){
node *temp=new node;
temp->data=val;
temp->next=head;
head=temp;
}
node* search(int val){
node* cur=head;
while(cur!=NULL){
if(cur->data==val){
cout<<"value found "<<val<<endl;
return cur;
}
cur=cur->next;
}
cout<<"value not exist"<<endl;
return NULL;
}
bool delspval(int val){
node*temp=0;
if(search(val)==NULL){
return 0;
}
else{
temp=search(val);
temp->prev->next=temp->next;
delete temp;
temp=0;
cout<<"specific value "<<val<<" deleted"<<endl;
return 1;
}
}
void display(){
node*cur=head;
while(cur!=NULL){
cout<<cur->data<<" ";
cur=cur->next;
}
cout<<endl;
}
~dll(){
while(head!=NULL){
node*cur=head;
head=cur->next;
delete cur;
cur=head;
}
}
};
void main(){
dll l1;
l1.inatst(1);
l1.inatst(2);
l1.inatst(3);
l1.inatst(4);
l1.inatst(5);
l1.inatst(6);
l1.display();
l1.delspval(3);
system("pause");
}
For starters, the search()
function is being called twice within the delspval()
function:对于初学者,
search()
function 在delspval()
function 中被调用两次:
if(search(val)==NULL){
and和
temp=search(val);
that makes the delspval()
function less efficient.这使得
delspval()
function 效率降低。
This statement:这个说法:
temp->next->prev=temp->next;
does not make sense.没有意义。
The delspval()
function can be defined in the following way. delspval()
function 可以通过以下方式定义。 I suppose that the class contains only one pointer to the head
node.我想 class 只包含一个指向
head
节点的指针。 If the class contains also a pointer to the tail
node, then the function below must be modified.如果 class 还包含指向
tail
节点的指针,则必须修改下面的 function。
bool delspval( int val )
{
node *temp = search( val );
bool success = temp != nullptr;
if ( success )
{
if ( temp->next != nullptr )
{
temp->next->prev = temp->prev;
}
// If the class has a pointer to the tail node
// then uncomment the else part
/*
else
{
tail = temp->prev;
}
*/
if ( temp->prev != nullptr )
{
temp->prev->next = temp->next;
}
else
{
head = temp->next;
}
delete temp;
}
return success;
}
You should absolutely not call search twice.你绝对不应该调用 search 两次。 You basically double the runtime.
您基本上将运行时间加倍。
But the solution if the value was found: If tmp->next is not null then it points to tmp, but it now needs to the item before tmp.但是如果找到该值的解决方案:如果tmp->next不是null那么它指向tmp,但它现在需要tmp之前的项目。 And if tmp->prev is not then it points to tmp, but it needs to point to the item after tmp.
如果 tmp->prev 不是,则它指向 tmp,但它需要指向 tmp 之后的项目。
And finally, if tmp = head then head must be changed to the item after tmp.最后,如果 tmp = head 那么 head 必须更改为 tmp 之后的项目。
Tmp = search()
If tmp == null then return
Prev = tmp->prev
Next = tmp->next
If prev ≠null then prev->next = next
If next ≠ null then next->prev = prev
If head = tmp then head = next.
Delete tmp.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.