当前位置: 首页 > 知识库问答 >
问题:

双链表删除函数中的分割错误与奇异输出

王云
2023-03-14

在这里,我的代码显示函数delete_node中的分段错误,该函数从双向链表中删除给定编号的节点。如果我删除第一个元素或最后一个元素,它显示分段错误。如果我试图删除中间的元素,它只是用“0”替换它的值。还请告诉我为什么当我取消注释时,函数printlist中的注释命令显示分段错误。

#include<stdio.h>
#include<stdlib.h>
struct node 
{
int data;
struct node *prev;
struct node *next;

};

struct node *first=NULL;

struct node *insertatbeg(struct node* list,int number)
{
struct node *new_node=malloc(sizeof(struct node));
new_node->data=number;
new_node->next=list;
new_node->prev=NULL;

return new_node;
}

struct node *insertatend(struct node* list,int number)
{
    struct node* new_node;
    struct node *curr=list;
    new_node=malloc(sizeof(struct node));
    new_node->data=number;

    if(list==NULL)
    {
    new_node->next=list;
    new_node->prev=NULL;
    return new_node ;    
    }
    else
    {
    while(curr->next!=NULL)
    {
        curr=curr->next;
    }
    curr->next=new_node;
    new_node->next=NULL;
    new_node->prev=curr;
return list;
    }

}
///Till here no error for sure
////////////////////////////////////////////////////////
void printlist(struct node *list)
{
int i=0;
struct node *p;
for(p=list;p!=NULL;p=p->next)
{
//printf("\nCurr=%d Prev=%d Next=%d\n",p->data,(p->prev)->data,(p->next)->data);//uncomment this & Error(Segmentation Fault comes)
printf("%d->",p->data);

++i;
}
printf("NULL\n");
printf("size=%d\n",i);
}
////////////////////////////////////////////
struct node *delete_node(struct node *list, int number)
{
struct node *curr,*previous;
for(curr=list,previous=NULL;curr!=NULL && curr->data!=number; previous=curr,curr=curr->next, (curr->next)->prev=previous)
;

if(curr==NULL)
{
printf("Sorry..Could not find the element..!!!\n");
return list;    
}

if(curr->prev==NULL)
{
(curr->next)->prev=NULL;
free(curr);
return list;
}

if(curr->next==NULL)
{
(curr->prev)->next=NULL;
free(curr);
return list;
}

else
{
(curr->prev)->next=curr->next;
(curr->next)->prev=curr->prev;
free(curr);
return list;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////   
}

int main()
{

first=insertatbeg(first,3);
first=insertatend(first,2);
first=insertatbeg(first,1);
//Till here output is fine and is 1->3->2

printlist(first);

//first=delete_node(first,1);

//first=delete_node(first,2);
first=delete_node(first,3);
printlist(first);

return 0;
}

共有2个答案

慎峻
2023-03-14

另外,当我取消注释时,请告诉我为什么函数printlist中的注释命令显示分段错误。

对于列表的第一个和最后一个元素,prev和next元素将为null,
这会抛出segfults,因为您试图访问无效内存。

因为你有一个双链表,你不需要维护两个指针,
你可以像这样简单地删除:

struct node *delete_node(struct node *list, int number) 
{
    struct node *c;
    for (c = list; c != NULL && c->data != number; c = c->next)
        ;
    if (c == NULL) return list;             //if node not found

    if (c->next == NULL && c->prev == NULL) //if the only element in
            return NULL;                    //list has to be deleted

    if (c->prev != NULL)                    
        c->prev->next = c->next;
    else list = c->next;                    //if first element is to be deleted

    if (c->next != NULL)
        c->next->prev = c->prev;
    return list;
}
冯和硕
2023-03-14

指针值可能为NULL。您应该在代码中为这种情况下添加检查。

if(pointer != NULL)
{
    // execute the code
}
 类似资料:
  • 双链表节点是在main函数中创建的。Ender和header已定义。在删除节点函数处中断-ender为空。 释放最后一个和第一个输入的内存的最佳方法是什么,即:删除:233,A和888,F?

  • 我正在实现一个类似于队列的双链接列表。因此,当我向列表中添加节点(例如5个节点)并清空列表并尝试向列表中添加新节点时,会出现分段错误(核心转储)。我不知道它为什么这么做。你能解释一下吗?

  • 几个月前我学习了递归,现在这一切都很混乱。有一个人可以解释我整个功能是如何正常工作的,但我有点明白它是如何工作的,但我认为有些步骤在我的脑海中并不是很清楚。病人的Thx提前。

  • 这是我的remove函数,用于删除具有元素的节点。我得到了一个seg错误,我很确定这是因为temp->prev是前面的哨兵,所以从技术上来说,它不在双链表中。如果这是正确的,我实际上如何防止这种情况?如有任何帮助,不胜感激。 编辑:刚刚更新了代码,但仍然出现了Seg错误

  • 我有两个循环双链表,与head和integer元素(无序)相连。要删除的第一个列表中包含第二个列表中的值。你的工作怎么样?如何排除这种情况?需要搜索第一个列表中的值才能删除第二个列表?我怎样才能和他们合作?你能解释一下解决这个问题的算法吗? 例如: 我有两个循环的双链表,带有head。 L1:40100902003266 L2:60146308090 我想删除第一个列表中第二个列表中的值。第一个列

  • 我是C语言的新手,正在尝试使用双链表创建一个电话簿应用程序。但是,我无法确定如何删除联系人,即此人的名字、姓氏和号码,并将上一个节点链接到下一个节点。我已经附上下面的代码。 任何帮助都将不胜感激。谢谢