1

Ι 在尝试从 c 中的双链表中删除元素时遇到问题。

Nodes_t *remove_Nodes (Nodes_t *a, Nodes_t b){
    Nodes_t *head;
    head=a;
    if ((a->i_pos==b.i_pos) && (a->j_pos==b.j_pos)){
        if (a->next=NULL){
            return NULL;
            }
        else {
            head=a->next;
            head->previous=NULL;
            return head;
            }
        }
    else if ((a->i_pos!=b.i_pos) || (a->j_pos!=b.j_pos)){
        while ((a->next->i_pos!=b.i_pos)||(a->next->j_pos!=b.j_pos)){
            a=a->next;
            if (a->next=NULL){
                return head;
                }
            }
        a=a->next;
        a->previous->next=a->next;
        if (a->next=NULL){
            return head;
            }
        else if (a->next!=NULL){
            a->next->previous=a->previous;
            return head;
            }               
        }
        return head;        
    }

它获取一个双链表,找到一个 Nodes_t 类型的元素,然后将其删除。虽然,当我检查了列表并且它的指针工作正常时,当我尝试调用该函数来删除我的第一个元素时,我得到了一个段错误。

更具体地说,正如我所检查的那样,该函数运行良好,直到达到这一点

else {
            head=a->next;
            head->previous=NULL;// HERE 
            return head;
            }

我使用的结构是这个

typedef struct Nodes {
char    position;
int     i_pos, j_pos;
int     g_distance;
int     h_distance;
int     F_estim;
struct Nodes    *parent;
struct Nodes    *next;
struct Nodes    *previous;

}Nodes_t;
4

1 回答 1

3

您在这里使用了分配= 而不是比较==

if (a->next=NULL){

哪个将评估为NULL,这是错误的,因此将转到您的else子句,您在哪里

head=a->next;
head->previous=NULL;

所以head变成NULL了,然后你试图取消引用一个NULL指针来获取它的previous成员。

  • 快速修复:将缺少的内容添加=到我引用的第一行。
  • 更好的解决方法:重构你的代码。它太长并且有不必要的位。并且不要忘记检查您的 equals 操作。
于 2013-04-07T23:30:05.823 回答