0

I've been working a lot with double pointers to solidify my knowledge of what they are and how I can use them, and I recently ran into a very weird problem. I'm trying to write code to insert a node into a doubly linked list using a double pointer to avoid starting the code with if statements. My code is as follows:

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;
}

I'm mainly having problems with this else statement:

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;
    }

I'm pretty sure I have the arrangement of links to prev and next correctly ordered so that there are no problems, but for some reason, on this line:

(*pp)->prev = newNode;

the newNode prev also gets switched to itself, which I don't believe it should. I probably overlooked some function of pointers that hasn't become apparent to me.

Here is output from the program:

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!  

The input is put one number at a time so the list is continuously added to.

Here is a link to the problem on hackerrank if anyone needs more clarifying:

https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list

I'm a student just trying to get better at programming :) any help is welcome!!

A Zhang
  • 23
  • 4
  • *I've been working a lot with double pointers to solidify my knowledge of what they are and how I can use them,* -- You've chosen a very bad example of using them. That question you linked to needs no "double pointer" to solve, and to try and shoehorn their usage into that question just obfuscates the code. – PaulMcKenzie Jun 03 '16 at 17:06
  • I know it could have been done without it and much simpler too! But I just wanted to see if I could do it, and now I want to know why this problem is happening haha. Thank you for the input though. – A Zhang Jun 03 '16 at 17:13

0 Answers0