【问题标题】:Why am i getting segmentation fault in deleting node?为什么在删除节点时出现分段错误?
【发布时间】:2018-08-20 02:00:41
【问题描述】:
    node * del(node * start,int loc)
{
    int l=len(start);
    if(loc<1 || loc>l)
    {
        printf("Deletion not possible. \n");
    }
    else
    {
        if(loc==1)
        {
            node *p;
            p=start;
            start=start->next;
            free(p);
        }
        else
        {
            node *p,*q;
            p=start;
            for(int i=1;i<=loc-1;i++)//check this one.
            {
                q=p;
                p=p->next;
            }
            q->next=p->next;
            free(p);
        }       
        printf("Deletion completed!\n");
    }
    return start;
}

这是函数,我从 main() 调用它:

case 5:
            printf("Enter the node you want to delete. \n");
            scanf("%d",temp);
            start=del(start,temp);
            break;

在删除任何节点时,我遇到了分段错误!我被困在这个问题上。 有人可以帮忙吗?

这是完整的程序:-

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

struct Node
{
    int data;
    struct Node * next;
};

typedef struct Node node;

int len(node * start)
{
    if(start==NULL)
        return 0;
    int c=1;
    while(start!=NULL)
    {
        start=start->next;
        c++;
    }
    return c;
}
node * insert(node* start,int loc,int d)
{
    int l=len(start);
    node * p=start;
    node * temp=(node*)malloc(sizeof(node));
    if(loc<1 || loc>l+1)
    {
        printf("Insertion not posible.!\n");
        return start;
    }       
    if(l==0 || loc==1)
    {
        temp->next=start;
        start=temp;
        temp->data=d;
        printf("Insertion completed.\n");
    }
    else
    {
        for(int i=1;i<=loc-2;i++)
        p=p->next;
        temp->next=p->next;
        p->next=temp;
        temp->data=d;
        printf("Insertion completed. \n");
    }   
    return start;
}
void show(node *start)
{
    int l=len(start);
    if(l==0)
    {
        printf("Nothing to print. \n");
    }
    else
    {
        for(int i=1;i<l-1;i++)
        {
            printf(" %d -> ",start->data);
            start=start->next;
        }
        printf(" %d \n",start->data);
    }   
}
node * destroy(node * start)
{
    node * p=start;
    while(start!=NULL)
    {
        start=start->next;
        free(p);
        p=start;
    }
    printf("Destroy of linked list completed! .\n");
    return NULL;
}
node * del(node * start,int loc)
{
    int l=len(start);
    if(loc<1 || loc>l)
    {
        printf("Deletion not possible. \n");
    }
    else
    {
        if(loc==1)
        {
            node *p;
            p=start;
            start=start->next;
            free(p);
        }
        else
        {
            node *p,*q;
            p=start;
            for(int i=1;i<=loc-1;i++)//check this one.
            {
                q=p;
                p=p->next;
            }
            q->next=p->next;
            free(p);
        }       
        printf("Deletion completed!\n");
    }
    return start;
}
int main()
{
    node * start=NULL;
    int ch,temp,d;
    ch=temp=d=0;
    while(ch != -1)
    {
        switch(ch)
        {
            case 0:
            printf("Enter 0 to show menu.\n");
            printf("Enter 1 to create linked list.\n");
            printf("Enter 2 to destroy linked list.\n");
            printf("Enter 3 to get the length of likned list.\n");
            printf("Enter 4 to insert element in the linked list.\n");
            printf("Enter 5 to delete element from the linked list.\n");
            printf("Enter 6 to view the linked list.\n");
            printf("Enter 7 to reverse a linked list.\n");
            break;

            case 1:
            printf("Enter the size of link list to begin with.\n");
            scanf("%d",&temp);
            for(int i=1;i<=temp;i++)
            {
                printf("Enter the data to bes inserted to node %d .\n",i);
                scanf("%d",&d);
                start=insert(start,i,d); 
            }
            break;

            case 2:
            start=destroy(start);
            break;

            case 3:
            printf("Size of linked list is: %d \n",len(start));
            break;

            case 4:
            printf("Enter the location where you want to insert the data.\n");
            scanf("%d",&temp);
            printf("Enter the data to be intered.\n");
            scanf("%d",&d);
            start=insert(start,temp,d);
            break;

            case 5:
            printf("Enter the node you want to delete. \n");
            scanf("%d",temp);
            start=del(start,temp);
            break;

            case 6:
            show(start);
            break;

            default:
            ch=0;
            break;
        }
        printf("\nEnter your choice? \n");
        scanf("%d",&ch);
    }
    destroy(start);


}

【问题讨论】:

  • if(loc&lt;1 || loc&gt;l) ... 你断言loc 只能是一个,但是之后你有一个 if-else 来检查loc 的值。
  • 我正在那里检查错误的索引!
  • 我正在检查输入的位置是否小于1并且大于链表的长度。
  • 您正在重新检查您已经测试过的相同条件,以决定是否完全走下该分支。修复那个和类似的代码,也许你会发现你的错误。
  • 您也可以通过在此站点中搜索“C 链表”而受益,这里有很多示例。你甚至可以点击“链表”标签,然后点击“最新”标签来查看最近的例子。

标签: c pointers data-structures linked-list segmentation-fault


【解决方案1】:

变化:

    case 5:
    printf("Enter the node you want to delete. \n");
    scanf("%d",temp);
    start=del(start,temp);
    break;

scanf("%d",temp); 转至scanf("%d",&amp;temp);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多