我一直在用双指点工作,以巩固我对它们是什么以及如何使用它们的了解,最近我遇到了一个非常奇怪的问题。我试图编写代码,使用双指针将节点插入双向链表,以避免使用if语句启动代码。我的代码如下:
Node* SortedInsert(Node *head,int data)
{
Node** pp = &head;
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
while(*pp){
cout<<"prev: "<<(*pp)->prev<<" *pp: "<<*pp<<" next: "<<(*pp)->next<<" data inserted: "<<(*pp)->data<<endl;
if((*pp)->data < newNode->data){
if(!(*pp)->next){
(*pp)->next = newNode;
newNode->prev = *pp;
cout<<" Node added: "<<newNode<<endl;
return head;
}
pp = &(*pp)->next;
} else{
newNode->next = *pp;
cout<<" new node next stored address: "<<newNode->next<<endl;
if(!(*pp)->prev){
(*pp)->prev = newNode;
cout<<" Node added: "<<newNode<<endl;
return newNode;
}
newNode->prev = (*pp)->prev;
cout<<" new node prev stored address: "<<newNode->prev<<endl;
newNode->prev->next = newNode;
cout<<" new node prev stored address: "<<newNode->prev<<endl;
(*pp)->prev = newNode;
cout<<" new node prev stored address: "<<newNode->prev<<endl;
cout<<" Node added: "<<newNode<<endl;
return head;
}
}
cout<<endl;
return newNode;
}
我主要在这句话中遇到问题:
else{
newNode->next = *pp;
cout<<" new node next stored address: "<<newNode->next<<endl;
if(!(*pp)->prev){
(*pp)->prev = newNode;
cout<<" Node added: "<<newNode<<endl;
return newNode;
}
newNode->prev = (*pp)->prev;
cout<<" new node prev stored address: "<<newNode->prev<<endl;
newNode->prev->next = newNode;
cout<<" new node prev stored address: "<<newNode->prev<<endl;
(*pp)->prev = newNode;
cout<<" new node prev stored address: "<<newNode->prev<<endl;
cout<<" Node added: "<<newNode<<endl;
return head;
}
我很确定我已经安排了关于prev和next正确排序的链接,以便没有问题,但出于某种原因,在这一行:
(*pp)->prev = newNode;
newNode prev也会切换到自己,我不相信它应该。我可能忽略了指针的一些功能,这些功能对我来说并不明显。
以下是该计划的输出:
prev: 0 *pp: 0x158adb0 next: 0 data inserted: 1
Node added: 0x158adf0
prev: 0 *pp: 0x158adb0 next: 0x158adf0 data inserted: 1
prev: 0x158adb0 *pp: 0x158adf0 next: 0 data inserted: 4
new node next stored address: 0x158adf0
new node prev stored address: 0x158adb0
new node prev stored address: 0x158adb0
new node prev stored address: 0x158ae30
Node added: 0x158ae30
prev: 0 *pp: 0x158adb0 next: 0x158ae30 data inserted: 1
prev: 0x158ae30 *pp: 0x158ae30 next: 0x158adf0 data inserted: 2
prev: 0x158adb0 *pp: 0x158adf0 next: 0 data inserted: 4
new node next stored address: 0x158adf0
new node prev stored address: 0x158adb0
new node prev stored address: 0x158adb0
new node prev stored address: 0x158adb0
Node added: 0x158ae70
prev: 0 *pp: 0x158adb0 next: 0x158ae70 data inserted: 1
prev: 0x158adb0 *pp: 0x158ae70 next: 0x158adf0 data inserted: 3
prev: 0x158ae70 *pp: 0x158adf0 next: 0 data inserted: 4
Node added: 0x158aeb0
prev: 0 *pp: 0x158adb0 next: 0x158ae70 data inserted: 1
prev: 0x158adb0 *pp: 0x158ae70 next: 0x158adf0 data inserted: 3
prev: 0x158ae70 *pp: 0x158adf0 next: 0x158aeb0 data inserted: 4
prev: 0x158adf0 *pp: 0x158aeb0 next: 0 data inserted: 7
new node next stored address: 0x158aeb0
new node prev stored address: 0x158adf0
new node prev stored address: 0x158adf0
new node prev stored address: 0x158aef0
Node added: 0x158aef0
prev: 0 *pp: 0x158adb0 next: 0x158ae70 data inserted: 1
prev: 0x158adb0 *pp: 0x158ae70 next: 0x158adf0 data inserted: 3
prev: 0x158ae70 *pp: 0x158adf0 next: 0x158aef0 data inserted: 4
prev: 0x158aef0 *pp: 0x158aef0 next: 0x158aeb0 data inserted: 6
prev: 0x158adf0 *pp: 0x158aeb0 next: 0 data inserted: 7
Node added: 0x158af30
prev: 0 *pp: 0x158adb0 next: 0x158ae70 data inserted: 1
prev: 0x158adb0 *pp: 0x158ae70 next: 0x158adf0 data inserted: 3
prev: 0x158ae70 *pp: 0x158adf0 next: 0x158aef0 data inserted: 4
prev: 0x158aef0 *pp: 0x158aef0 next: 0x158aeb0 data inserted: 6
prev: 0x158adf0 *pp: 0x158aeb0 next: 0x158af30 data inserted: 7
prev: 0x158aeb0 *pp: 0x158af30 next: 0 data inserted: 9
Node added: 0x158af70
Wrong Answer!
输入一次放一个数字,因此列表会不断添加到。
如果有人需要更多澄清,这里有关于hackerrank问题的链接:
https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list
我是一名学生,只是想在编程方面做得更好:)欢迎任何帮助!!