Home > Back-end >  Excuse me this double linked list to delete, is where the pointer refers to the uninitialized, how t
Excuse me this double linked list to delete, is where the pointer refers to the uninitialized, how t

Time:10-11

The following is my double linked list respectively established, removing elements and debugging problems

CodePudding user response:

You of the main function of the code is posted, see how the head when you delete functions have parameters initialization
Second, delete function phead and p pointer without new time, it is good to use the head assignment directly, namely DPigNode * phead=head; DPigNode * p=head; Otherwise you new without don't delete, causing a memory leak

CodePudding user response:

The head has come in, pHead=p=head;
Two new
there is no need for operation
D Node * pHead, * p, * temp);
PHead=p=head;
While (p!=NULL) {
If (p - & gt; Weight!=weight) p=p - & gt; PNext .//not to delete node

Temp=p;//p is to delete the node
If (NULL==p - & gt; PPre ) {//p is head node will not lead node
PHead=p - & gt; PNext .
PHead - & gt; PPre =NULL;
delete p;
Return pHead;
}

If (NULL==p - & gt; PNext  ) {//p is tail node
P - & gt; PPre - & gt; PNext =NULL;
delete p;
Return pHead;
}

//p not head, nor
tailP - & gt; PPre - & gt; PNext =p - & gt; PNext .
P - & gt; PNext - & gt; PPre =p - & gt; PPre .
delete p;
Return pHead;
}
Return pHead;//in the end also didn't find want to delete the weight will be executed this statement

CodePudding user response:


The head has come in, pHead=p=head;
Two new
there is no need for operation
D Node * pHead, * p, * temp;
PHead=p=head;
While (p!=NULL) {
If (p - & gt; Weight!=weight) p=p - & gt; PNext .//not to delete node

Temp=p;//p is to delete the node
If (NULL==p - & gt; PPre ) {//p is head node will not lead node
PHead=p - & gt; PNext .
if (pHead)//queue only nodes inside, likely pHead will be NULL
pHead - & gt; PPre =NULL;
delete p;
Return pHead;
}

If (NULL==p - & gt; PNext  ) {//p is tail node
//here don't have to test p - & gt; PPre , program here has guaranteed the p not head node, front nodes will exist
P - & gt; PPre - & gt; PNext =NULL;
delete p;
Return pHead;
}

//p not head, nor
tailP - & gt; PPre - & gt; PNext =p - & gt; PNext .
P - & gt; PNext - & gt; PPre =p - & gt; PPre .
delete p;
Return pHead;
}
Return pHead;//in the end also didn't find want to delete the weight will be executed this statement
  • Related