是什么导致双向链表代码中的分段错误

时间:2018-12-05 13:56:13

标签: c++ data-structures linked-list doubly-linked-list

调用printList时,以下代码会导致分段错误。为什么会这样?

失败的有效示例在https://ide.geeksforgeeks.org/ZeqrQf9esb

#include <iostream>

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

void addNode(struct Node **head_ref,int pos,int data)
{
    struct Node*nn=new Node;
    int k=0;
    nn->data=data;
    if(*head_ref==nullptr)
        *head_ref=nn;
    else
    {
        struct Node*temp=*head_ref;

        while(k<pos)
        {
            temp=temp->next;
        }

        if(temp->next!=nullptr)
        {

            nn->prev=temp;
            nn->next=temp->next;
            temp->next=nn;
            nn->next->prev=nn;


        }
        else
        {
            nn->next=nullptr;
            nn->prev=temp;
            temp->next=nn;
        }
    }
}
 void printList(struct Node *Node)
    {
      struct Node *temp=Node;
      //goto end
      while(temp->next!=NULL)
      {
        temp=temp->next;
      }
      //goto start
      while(temp->prev!=NULL)
      {
       temp = temp->prev;
      }
      //now print
      while(temp!=NULL)
      {
          printf("%d ",temp->data);
          temp=temp->next;
      }

    }

int main()
{
    Node * head; 
    addNode(&head,0,10);
    addNode(&head,0,11);
    addNode(&head,0,12);


    std::cerr << head->data << std::endl;
    std::cerr << head->next->data << std::endl;
    std::cerr << head->next->next-> data << std::endl;  
      printList(head);
}

2 个答案:

答案 0 :(得分:1)

1)请勿在同一代码中混合使用malloc和new。您将失去对哪个节点来自哪个分配器的跟踪,如果您释放了新分配的节点,或者删除了malloc的节点,则会遇到严重的错误。

2)在前进“ k”次时……您忘记增加k,所以永远不要停止前进,而走出清单。这是崩溃的原因:

    while(k<pos)
    {
        temp=temp->next;
    }

可能还有更多,但看到#2后我不再看了。

答案 1 :(得分:0)

解决方法是将next和prev初始化为null。如果您不这样做,那么它们将采用随机值。重要的行是

struct Node {
    int data;
    Node * next=nullptr;
    Node * prev=nullptr;
}; 

有关有效的示例,请参见https://wandbox.org/permlink/qooehizoRifrvOVX。完整代码如下

#include <iostream>

struct Node {
    int data;
    Node * next=nullptr;
    Node * prev=nullptr;
};

void addNode(struct Node **head_ref,int pos,int data)
{
    struct Node*nn=new Node;
    int k=0;
    nn->data=data;
    if(*head_ref==nullptr)
        *head_ref=nn;
    else
    {
        struct Node*temp=*head_ref;

        while(k<pos)
        {
            temp=temp->next;
        }

        if(temp->next!=nullptr)
        {

            nn->prev=temp;
            nn->next=temp->next;
            temp->next=nn;
            nn->next->prev=nn;


        }
        else
        {
            nn->next=nullptr;
            nn->prev=temp;
            temp->next=nn;
        }
    }
}
 void printList(struct Node *Node)
    {
      struct Node *temp=Node;


      //goto end
      while(temp->next!=nullptr)
      {
        temp=temp->next;
      }


      //goto start
      while(temp->prev!=nullptr)
      {
       temp = temp->prev;
      }



      //now print
      while(temp!=nullptr)
      {
          printf("%d ",temp->data);
          temp=temp->next;
      }


    }

int main()
{
    Node * head; 
    addNode(&head,0,10);
    addNode(&head,0,11);
    addNode(&head,0,12);

    std::cerr << head->data << std::endl;
    std::cerr << head->next->data << std::endl;
    std::cerr << head->next->next-> data << std::endl;  

    printList(head);
}