unable to delete a node in double linked list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jeevan83
    New Member
    • Aug 2014
    • 9

    unable to delete a node in double linked list

    I am trying this without a head/start pointer which would generally hold the address of first node. I have 3 nodes here from which I am trying to delete last node, but its not happening. I might be wrong in my logic and this is my first linked list program, so please help me!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct dll{
                struct dll *prev;
                int data;
                struct dll *next;
              };
    
    int main()
    {
        struct dll *p1,*p2,*p3, *temp;
        p1=malloc(sizeof(struct dll));
        p2=malloc(sizeof(struct dll));
        p3=malloc(sizeof(struct dll));
        temp=malloc(sizeof(struct dll));
        p1->prev=NULL;
        p1->data=1;
        p1->next=p2;
        p2->prev=p1;
        p2->data=2;
        p2->next=p3;
        p3->prev=p2;
        p3->data=3;
        p3->next=NULL;  
        struct dll *add=NULL;
        int count=0;
        printf("add of p1::%p add of p2::%p add of p3::%p add of p1->prev::%p add of p1->next::%p add of p2->prev::%p add of p2->next::%p add of p3->prev::%p add of p3->next::%p\n",p1,p2,p3,p1->prev,p1->next,p2->prev,p2->next,p3->prev,p3->next);
        while(p1->next!=NULL)
        {
            count++;
            p1=p1->next;
        }
        printf("no of nodes %d\n",count+1);
        puts("enter the addresss of node to delete it");
        scanf("%p",&add);
        while(p1->next!=NULL)
        {
            if(p1->next==add)
            {
                temp = p1->next;
                p1->next=NULL;
                free(temp);
                temp=NULL;
            }
            else
            p1=p1->next;
        }
        puts("after deletion attempted");
    printf("add of p1::%p add of p2::%p add of p3::%p add of p1->prev::%p add of p1->next::%p add of p2->prev::%p add of p2->next::%p add of p3->prev::%p add of p3->next::%p\n",p1,p2,p3,p1->prev,p1->next,p2->prev,p2->next,p3->prev,p3->next);
    
        while(p1->next!=NULL)
        {
            count++;
            p1=p1->next;
        }
        printf("no of nodes %d\n",count+1);
        free(p1);
        p1=NULL;
        free(p2);
        p2=NULL;
        free(p3);
        p3=NULL;
    return 0;
    }
    output::::

    add of p1::0x9605008 add of p2::0x9605018 add of p3::0x9605028 add of p1->prev::(nil) add of p1->next::0x960501 8 add of p2->prev::0x960500 8 add of p2->next::0x960502 8 add of p3->prev::0x960501 8 add of p3->next::(nil)

    no of nodes 3

    enter the addresss of node to delete it
    0x9605028

    after deletion attempted

    add of p1::0x9605028 add of p2::0x9605018 add of p3::0x9605028 add of p1->prev::0x960501 8 add of p1->next::(nil) add of p2->prev::0x960500 8 add of p2->next::0x960502 8 add of p3->prev::0x960501 8 add of p3->next::(nil)

    no of nodes 3

    In this example i am trying to delete the node 3 which is p3, by deleting its address 0x9605028. But after deletion the node count is still 3 and addresses are like unexpected!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Using a double linked list, one with next/prev pointers, should allow you to delete any node.

    Assume p1 is a pointer to the last node. That is, p1->next is 0.

    To delete this node, set p1->prev->next to p1->next, free the p1 members and then free p1.

    To delete a node in the middle of the list, let's call it cur for the current node, you must:
    1) set cur->prev->next to cur->next
    2) set cur->next->prev to cur->prev->next

    The cur node is now out of the list. free the members ansd then free the node.

    Comment

    Working...