【问题标题】:Segmentation fault when deleting and adding a node in double linked list在双链表中删除和添加节点时出现分段错误
【发布时间】:2013-06-09 14:24:06
【问题描述】:
#include<stdio.h>
#include<stdlib.h>

//double linked list
struct node {
    int data;
    struct node *rnext;
    struct node *lnext;
}*first=NULL,*last=NULL;
//double linked list

void insertion() {
    struct node *nn=malloc(sizeof(*nn));
    printf("enter data to be inserted\n");
    scanf("%d",&nn->data);
    nn->rnext=NULL;
    nn->lnext=last;
    if(first == NULL) {
        first = nn;
        last = nn;
    }
    else{
        last->rnext=nn;
    } 
    last=nn;
}

void display() {
    struct node *temp;
    if(first==NULL) {
        printf("list is empty\n");
        return;
    }
    temp=first;
    while(temp!=NULL) {
        printf("%d \n",temp->data);
        temp=temp->rnext;
    }
}

void deletion() {
    struct  node  *temp;
    if(first==NULL) {
        printf("list is empty\n");
        return;
    }
    temp=first;
    first=first->rnext;
    first->lnext=NULL;
    free(temp);
}

int main() {
    int  option;
    do {
        printf("enter option 1.insert\n  2.display\n  3.delete\n  4.exit\n");
        scanf("%d",&option);
        switch(option) {
        case 1:
            insertion();
            break;
        case 2:
            display();
            break;
        case 3:
            deletion();
            break;
        }
    } while(option!=4);
}

这是一个为删除和插入双链表中的节点而编写的程序。该程序编译没有错误,但在运行时失败,并在列表中只有一个节点时删除节点时出现分段错误错误。谁能帮忙解决这个分段错误?

这是程序的一些示例输出:

./out
enter option 1.insertion
  2.display
  3.deletion
  4.exit
1
enter data to be inserted
11
enter option 1.insertion
  2.display
  3.deletion
  4.exit
2
11 
enter option 1.insertion
  2.display
  3.deletion
  4.exit
3
Segmentation fault

【问题讨论】:

  • 请格式化您的输出,使其更具可读性。
  • @user2458408 您应该真正致力于在您的程序中实现一致的缩进和通用代码样式。一致的缩进让任何愿意回答你问题的人更容易理解你的代码,而且你自己也会更快地发现小错误。

标签: c doubly-linked-list


【解决方案1】:

解决这个问题最简单的方法是在调试器中运行它。您可能甚至不需要学习如何单步执行您的代码或任何东西 - 只需启动、运行并阅读该行。

如果您在 *nix 上,如您的标签所示:

  1. 使用-g 标志编译您的代码。
  2. 加载为,例如gdb a.out。
  3. 现在运行它已加载 - (gdb) run。
  4. 尽一切可能重现段错误。
  5. bt 或哪里应该给你一个堆栈跟踪 - 以及导致你的问题的确切行。

我确信您可以从那里解决它并将其发布为答案;但如果没有,知道确切的路线将使研究和解决变得非常容易。

【讨论】:

    【解决方案2】:

    至少有两个错误:

    一方面,在插入函数中,内存分配不正确:

    struct node *nn=malloc(sizeof(*nn));
    

    应该是:

    struct node *nn= (struct node *) malloc(sizeof(struct node));
    

    另一方面,在删除功能中。如果只有一个节点。声明后

    first=first->rnext;
    

    指针首先变为NULL。然后您尝试将其用作:

    first->lnext=NULL;  // first is NULL
    

    然后分段失败。

    【讨论】:

      【解决方案3】:
      temp=first;
      first=first->rnext;//when only one, first is NULL(first->rnext)
      first->lnext=NULL;//(NULL)->lnext , Segmentation fault!!
      free(temp);
      

      可能会修复

      temp=first;
      if(first == last){//if only one
          first = last = NULL;
      } else {
          first=first->rnext;
          first->lnext=NULL;
      }
      free(temp);
      

      【讨论】:

        猜你喜欢
        • 2017-10-28
        • 2012-09-22
        • 2018-03-10
        • 1970-01-01
        • 1970-01-01
        • 2020-04-19
        • 2019-03-03
        • 1970-01-01
        相关资源
        最近更新 更多