1

在这里,我的代码显示了函数中的分段错误,该函数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;
}
4

2 回答 2

0

指针值可能为 NULL。您应该在该案例的代码中添加检查。

if(pointer != NULL)
{
    // execute the code
}
于 2013-06-09T13:20:07.223 回答
0

另外请说明为什么我取消注释时功能 printlist 中的注释命令显示分段错误。

对于列表的第一个和最后一个元素,prev 和 next 元素将为 null,
这会引发段错误,因为您尝试访问无效的内存。

由于您有一个双链表,您不需要维护两个指针,
您可以像这样更简单地删除:

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;
}
于 2013-06-09T13:25:38.007 回答