双链表核心转储

时间:2016-04-22 04:23:09

标签: c++ linked-list coredump

提前致谢。

我正在制作双重链接列表。

一切都运行正常,但我意识到当我在中间某处添加一个新的类节点时,左指针仍然指向之前的节点(现在距离两个空格)。

所以我在第46行添加了一个新的节点指针。

然后在第51行,我告诉该节点现在指向新节点。

所以:

  • 首先我在空间中关闭了新的节点温度

  • 然后我通过列表

  • 制作指针temp2
  • 最后,我告诉temp3指向temp2节点之后的节点

功能运行后,订单应为temp2->temp->temp3

我的要点:在我添加第51行之后,我的程序核心转储(分段错误)并关闭。

我该如何解决这个问题?只有当我添加一些不能代替头部指针的东西时才会发生这种情况。

void add(node *&head, node *&tail, node *&current)
{
    node *temp = new node;  //creates a pointer pointing to a new class node
    cin >> temp->letter;    // user input

    current = head; // creates a pointer to point at the first node
    while (current != NULL) // while list isn't empty
    {
        if (current->letter == temp->letter)
        { // letter already exists
            cout << "DUPLICATE: " << temp->letter << endl << endl;
            return;
        }
        else
        { // loop through list moving tail pointer to the end while checking for duplicates
            tail = current;
            current = current->right_link;
        }
    }

    current = temp; // current = new added node

    if (isEmpty(head))
    { // if first node
        temp->left_link = NULL;
        temp->right_link = NULL;
        head = temp; // head and 
        tail = temp; // tail both point to first and only node.
    }
    else
    { // if new letter value is less than head value
        if(temp->letter < head->letter)
        {
            temp->right_link = head; // node points (right) to head
            temp->left_link = NULL; // left most node point to nothing.
            head->left_link = temp; // head (currently the second node) points (left) to first node
            head = temp; // head pointer moves to the first position
        }
        else
        { // if new node goes anywhere other than head
            node *temp2 = head; // new node to cycle through list
            while(temp2->right_link != NULL && temp2->right_link->letter < temp->letter)
            { // if temp2 points to a node and that node's value is less than temp node value
                temp2 = temp2->right_link;
            }
            node *temp3 = temp2->right_link;
            temp->right_link = temp2->right_link; // when temp2 stops looping, temp will point to
                                                  // the same node as temp2.
            temp2->right_link = temp; // temp2's current node will point to temp, causing temp
                                // to be added into the list (after temp2)
            temp3->left_link = temp; // point the node (after the newly inserted node) left to new node
            temp->left_link = temp2; // connects the left pointer between temp and temp2
            if(temp->right_link == NULL)
                tail = temp;
        }
    }
    cout << "ADDED : " << temp->letter << endl << endl;
}

1 个答案:

答案 0 :(得分:0)

如果temp2->right_link == NULL

 46      node *temp3 = temp2->right_link;

是一个NULL指针,所以你不能

 51      temp3->left_link = temp;

如果使用调试器,这应该是显而易见的。