【问题标题】:Getting a segmentation fault when trying to delete a node from the list?尝试从列表中删除节点时出现分段错误?
【发布时间】:2019-12-05 17:18:40
【问题描述】:

我正在研究一个双向链表的删除方法,我试图在将一些节点添加到我的列表后删除它们。下面我提供了一些最低限度可重现的代码。

当我运行此代码时,我收到错误 Segmentation Fault: 11。我知道我试图删除同一个节点两次,但这只会导致我的代码出现错误,并显示Invalid command\n 的消息,但是当这种情况发生时,我的整个程序会停止并且我会收到Segmentation Fault: 11。我相信这与下面我的delete 方法中的(*current)->next->prev = (*current)->prev; 有关。

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

typedef struct NodeStruct Node;

//struct for each office item
struct NodeStruct {
    int id;
    struct NodeStruct *next;
    struct NodeStruct *prev; //Create doubly linked list node
};


/** Structure for the whole list, including head and tail pointers. */
typedef struct {
  /** Pointer to the first node on the list (or NULL ). */
  Node *head;
  Node *last;
} List;

List *list;

List *makeList();
void insert(int idInsert, List *list);
static void *addRecord(List *list, int newID);
static void printReverse(List *list);
void delete(int key, List *list);
bool search(List *list, int x);

int main(void) {

  //Create an empty list for you to start.
  list = (List *)makeList();

  addRecord(list, 1);
  addRecord(list, 2);
  addRecord(list, 3);
  addRecord(list, 4);
  addRecord(list, 4);
  addRecord(list, 7);
  insert(15, list);
  delete(7, list);
  delete(1, list);
  delete(4, list);
  delete(2, list);
  delete(5, list);
  delete(2, list);
  printReverse(list);

}

List *makeList()
{
  List *list = (List *) malloc( sizeof( List ) );
  list->head = NULL;
  list->last = NULL;
  return list;
}

void insert(int idInsert, List *list)
{
  //Insert the record based on the ID if no duplicates exist
  //Special case: insert at front if idInsert is less than the ID of the current head
  if (idInsert < list->head->id) {
    //Make the node with idInsert the first node
    Node *new = malloc(sizeof(Node)); //Allocate memory for the new node

    list->head->prev = new;
    new->prev = NULL;
    //Add in data
    new->id = idInsert;

    new->next = list->head;

    list->head = new;
    // if the new node is the last one
  } else if (idInsert > list->last->id) {
    addRecord(list, idInsert);
    printf("RECORD INSERTED: %d\n", idInsert);
    return;
  } else { //Locate the node before the point of insertion
    //Allocate memory for the node
    Node *new = malloc(sizeof(Node));

    //Add in data
    new->id = idInsert;

    Node *current = list->head;
    while (current->next != NULL && current->next->id < new->id) {
      current = current->next;
    }
    new->next = current->next;
    if (current->next != NULL) {
      new->next->prev = new;
    }
    current->next = new;
    new->prev = current;
  }

  //Print this message if successful
  printf("RECORD INSERTED: %d\n", idInsert);
}

static void *addRecord(List *list, int newID) 
{
  //Allocate memory for the node
  Node *new = malloc(sizeof(Node)); 

  //Add in data
  new->id = newID; 

  new->prev = list->last;
  new->next = NULL;

  list->last = new;

  // if list is empty
  if(!list->head)
  {
    list->head = new;
    return EXIT_SUCCESS;
  }

  Node **next_p = &list->head;
  while (*next_p) {
    next_p = &(*next_p)->next;
  }
  *next_p = new;

  return EXIT_SUCCESS;
}

static void printReverse(List *list) {
    Node **tail = &list->last;
    printf("LIST IN REVERSE ORDER:\n");

    //Traversing until tail end of linked list
    while (*tail) {
        printf("Item ID: %d\n", (*tail)->id);
        tail = &(*tail)->prev;
    }
}

void delete(int key, List *list)
{
  //Check if ID exists in list
  if (search(list, key) == false) {
    printf("Invalid command\n");
  } else {
    printf("RECORD DELETED: %d\n", key);
  }

  //If linked list is empty (base case)
  Node **current = &list->head;

  //current points to the pointer pointing towards the current node
  //For the first iteration, it is the address of the head pointer
  while (*current && key != (*current)->id) {
    current = &(*current)->next;
  }

  (*current)->next->prev = (*current)->prev;

  if (*current) {
    Node *temp = *current;
    *current = (*current)->next;
    free(temp);
  }

}

bool search(List *list, int x)
{
  Node *current = list->head; //Initialize current node
  while (current != NULL) {
    if (current->id == x) {
      return true;
    }
    current = current->next;
  }
  return false;
}

我想要的结果是:

RECORD INSERTED: 15
RECORD DELETED: 7
RECORD DELETED: 1
RECORD DELETED: 4
RECORD DELETED: 2
RECORD DELETED: 5
Invalid command
LIST IN REVERSE ORDER: 
Item ID: 15
Item ID: 3

而不是分段错误。如何改进我的删除方法中的代码,以免收到此错误,为什么目前存在?

【问题讨论】:

  • 如果尝试删除最后一个节点,那么node->next->prev无效崩溃-->next为NULL,不能做NULL->prev。

标签: c pointers segmentation-fault doubly-linked-list


【解决方案1】:

打印出Invalid Command 后,您的代码会做什么?它继续在函数内执行,最终到达(*current)-&gt;next-&gt;prev = (*current)-&gt;prev; 行,这将导致段错误,因为*current 将为NULL。

这暴露了三个问题。第一个是您有效地搜索key 两次:一次是查看它是否在列表中,第二次是在列表中找到该节点所在的位置,以便可以将其删除。这些应该结合起来,所以只需要进行一次搜索。一种方法是让find 返回一个适当的指针,以便您可以找到并删除该节点。另一种方法是手动搜索(不调用find),如果找不到key,则报告错误。

第二个问题是打印Invalid Command后缺少return,虽然执行前面的建议可以避免这个问题。

第三个问题是(*current)-&gt;next-&gt;prev 的赋值应该在下面的if (*current) 内,以避免取消引用空指针。

【讨论】:

  • 非常感谢。我最终使用了return 语句,现在我的程序运行良好。
猜你喜欢
  • 2018-03-10
  • 1970-01-01
  • 2017-10-28
  • 2019-03-03
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多