Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Guys I am trying to delete node 2 but program is not entering the if loop for comparison. The first print f doesn’t print even! Please help!
C#
#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));
    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 \n",  p1, p2, p3);
for ( temp = p1; temp != NULL; temp = temp->next ){
        count++;
    }
    printf("no of nodes %d\n", count);
   //puts("enter the addresss of node to delete it");
    add=&p2;
printf("address of node which i wanna delete::%p\n",*add);
 temp = p1;
 printf("temp:%p\n",temp);
while(temp!=NULL){
        if (temp->next == add) {
        printf("temp in for loop:%p\n",temp);
            temp->next=temp->next->next;
            temp->next->next->prev=temp;
            printf("%p %p\n",temp->next,temp->next->next->prev);
            free(temp);
            temp = NULL;
        }
        temp = temp->next;
break;
    }
    puts("after deletion attempted");

count =0;
for ( p1; p1!= NULL; p1 = p1->next ){
        count++;
    }
    printf("no of nodes %d\n", count);
    free(p1);
    p1 = NULL;
    free(p2);
    p2 = NULL;
    free(p3);
    p3 = NULL;
    return 0;
}

Output:

add of p1::0x8512008 add of p2::0x8512018 add of p3::0x8512028<br />
no of nodes 3<br />
address of node which i wanna delete::0x8512018<br />
temp:0x8512008<br />
after deletion attempted<br />
no of nodes 3
Posted
Updated 23-Aug-14 9:34am
v2
Comments
Sergey Alexandrovich Kryukov 23-Aug-14 23:20pm    
Please, use the debugger, to start with.
—SA
Jeevan83 24-Aug-14 14:56pm    
[removed... — SA]

1 solution

Hi Jeevan83

Problem is with this statement
add=&p2; //this will assign the address of pointer p2 but you want to assign address to which pointer p2 points to so use add=p2;

Also printf("address of node which i wanna delete::%p\n",*add);
This is syntax error You want to print the address so use only add instead of *add

For your information
Your code will only run properly when 2 node is deleted if u try to delete some other it will fail with segmentation fault. Try for that cases too if required post here . Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900