节点未被删除 [英] Node not being deleted

查看:106
本文介绍了节点未被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计我试图删除节点2,但程序没有进入if循环进行比较。第一个打印f甚至不打印!请帮忙!

 #include< stdio.h> 
#include< stdlib.h>

struct dll {
struct dll * prev;
int 数据;
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( 添加p1 ::%p添加p2 ::%p添加p3 :: %p \ n,p1,p2,p3);
for (temp = p1; temp!= NULL; temp = temp-> next){
count ++;
}
printf( no节点%d \ n,计数);
// puts(输入节点的地址以删除它);
add =& p2;
printf( 我要删除的节点的地址::%p \ n ,*加);
temp = p1;
printf( temp:%p \ n,temp);
while (temp!= NULL){
if (temp-> next == add ){
printf( temp for for循环:%p \ n,temp);
temp-> next = temp-> next-> next;
temp-> next-> next-> prev = temp;
printf( %p%p \ n,temp-> next, TEMP->下一步 - >下一步 - >分组);
free(temp);
temp = NULL;
}
temp = temp-> next;
break ;
}
puts( 删除后尝试);

count = 0 ;
for (p1; p1!= NULL; p1 = p1-> next){
count ++;
}
printf( no节点%d \ n,计数);
免费(p1);
p1 = NULL;
免费(p2);
p2 = NULL;
免费(p3);
p3 = NULL;
return 0 ;
}



输出:



添加p1 :: 0x8512008添加p2 :: 0x8512018添加p3 :: 0x8512028< br /> 
没有节点3< br />
节点的地址我要删除:: 0x8512018< br />
temp:0x8512008< br />
删除后尝试< br />
no of nodes 3

解决方案

您好Jeevan83



问题与此声明有关

add =& p2; //这将指定指针p2的地址,但你想指定指针p2指向的地址,所以使用 add = p2 ;



另外printf(地址我要删除的节点::%p \ n, * add );

这是语法错误你想要打印地址所以只使用添加而不是 *添加



供您参考

您的代码只有在删除2节点时才能正常运行如果您尝试删除其他节点,它将因分段错误而失败。尝试对于那些情况,如果需要在这里发布。感谢

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!

#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

解决方案

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


这篇关于节点未被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆