1.11M Members

Deleting nodes in a doubly linked list

 
0
 

I have the following function to delete nodes in a doubly linked list at a specified position

void Delete(struct dnode **ptr,int pos)            /*pos refers to position at which element is to be deleted and ptr is the pointer to the first node in the list */
{
    struct dnode *temp;             
    temp=*ptr;
    if(pos==1)
    {
        *ptr=(*ptr)->next;
        (*ptr)->prev=NULL;
    }
    else
    {
        while(pos-1)
        {
            temp=temp->next;
            pos--;
        }
        if(temp->next==NULL)
        {
            temp->prev->next=NULL;
        }
        else
        {
            temp->next->prev=temp->prev;
            temp->prev->next=temp->next;
        }
    }
    free(temp);
}

Here dnode refers to the structure node and prev and next are pointers to the previous and next elements respectively. I am not getting the desired output.

My linked list contained: 55 96 33 11 66 2 17 14 99

I called Delete two times in main()

int main()
{
Delete(&p,3);                //where p points to the first element
Delete(&p,4);
}

Output was: 55 11 66 17 14 99

 
0
 

please someone reply!!

 
0
 

Where you insert your node, at the beginning or at the end?

You problem is that when delete a node your pointer is move forward.
and pointing next element of deleted node and you did not able to get desired result so first store the address of your pointer in other pointer before calling Delete function.

if you insert at the end and your head is pointing at the first element.
for example, your list is: 55 96 33 11 66 2 17 14 99
after executing this code

int main()
{
Delete(&p,3);                //where p points to the first element
Delete(&p,4);
}

your output
55 96 11 66 2 14 99
not this
55 11 66 17 14 99

what happen in your code........
when execute your 1st Delete
delete the 3rd element that is 33
2nd time when execute your Delete
->now your pointer is pointing to 11 which you 3rd element now and try to count after that position and 17 is the 4th element according to that.
I think you miss place some data element

use some thing like this

int main()
{
struct dnode *temp;
temp=p;
Delete(&p,3);                //where p points to the first element
p=temp;
Delete(&p,4);
}

Best Of Luck.

 
0
 

I tried your suggestion but got the same output.. I am not able to get your point. What I think is that the pointer p is updated only when position is 1 and in all other cases the temporary pointer temp is updated which gets set to the location of the first node as Delete is called again and again. I have checked the pointers and they point to the first element after Delete is called so that is not a problem. What really annoys me is that I called Delete two times and three elements get deleted.

 
0
 

If my suggestion not working for u please send ur enough code so that I can help u .........coz I don't how you inserting node into link list how u print may be some logical error in ur inserting function.............

 
0
 
void addafter(struct dnode *ptr,int pos,int value)
{
    struct dnode *temp;
    temp=(struct dnode *)malloc(sizeof(struct dnode));
    temp->data=value;

    if(pos==2)
    {
        temp->next=ptr->next;
        ptr->next=temp;
        temp->prev=ptr;
//error 
    }
    else
    {
        while(pos-2)
       {
        ptr=ptr->next;
        pos--;
       }

        temp->next=ptr->next;
        ptr->next=temp;
        temp->prev=ptr;
        void addafter(struct dnode *ptr,int pos,int value)
{
    struct dnode *temp;
    temp=(struct dnode *)malloc(sizeof(struct dnode));
    temp->data=value;

    if(pos==2)
    {
        temp->next=ptr->next;
        ptr->next=temp;
        temp->prev=ptr;
    }
    else
    {
        while(pos-2)
       {
        ptr=ptr->next;
        pos--;
       }

        temp->next=ptr->next;
        ptr->next=temp;
        temp->prev=ptr;
        temp->next->prev=temp;
       }

}


       }

}

You just miss a single line at the place of error

temp->next->prev=temp;// insert this line at the place of error

I hope u understand the problem.
Actually I lost my previous a/c because my pc is formatted (prvnkmr449)

Best Of Luck

 
0
 

And please left the concept of sending problem in PM

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article