简体   繁体   English

C++ 中链表实现中的分段错误

[英]Segmentation fault in linked list implementation in C++

I am writing a function in C++ to add a "data" of type 'int' to the end of a linked list.我正在 C++ 中编写 function 以将“int”类型的“数据”添加到链表的末尾。

void insert_back()
{
 int no;
 node *temp;
 cout<<"\nEnter the number"<<"\n";
 cin>>no;
 temp = head;
 if(temp != NULL)
 {
         while(temp != NULL)
                    temp = temp->next;
 }

 temp->next = (node*)malloc(sizeof(node));
 temp = temp->next;
 temp->data = no;
 temp->next = NULL;

} }

However, at the line, temp->next = (node*)malloc(sizeof(node)), I get an access violation error(segmentation fault).但是,在 temp->next = (node*)malloc(sizeof(node)) 行,我收到访问冲突错误(分段错误)。 I do not find anything fundamentally wrong.我没有发现任何根本上的错误。 Can you please enlighten me on the issue?你能就这个问题告诉我吗?

If you want to get the last node of the list, just check if the next member is null or not as the next member of the last node is null. 如果要获取列表的最后一个节点,只需检查下一个成员是否为null,因为最后一个节点的下一个成员为null。

In your code, you check temp is null or not instead of temp->next . 在代码中,检查temp是否为null而不是temp-> next

while(temp != NULL)
    temp = temp->next;

will get the temp be null when the loop is over. 循环结束时,温度将为null。

Besides, you should also consider the condition where the head is null. 此外,还应考虑head为null的条件。

void insert_back()
{
    int no;
    node *temp;
    cout<<"\nEnter the number"<<"\n";
    cin>>no;
    temp = head;
    if(temp != NULL)
    {
        while(temp->next != NULL)
            temp = temp->next;
        temp->next = (node*)malloc(sizeof(node));
        temp = temp->next;
        temp->data = no;
        temp->next = NULL;
    }else{
        head = (node*)malloc(sizeof(node));
        head->data = no;
        head->next = NULL;
    }

}

Just before that line executes, temp will be null. 在该行执行之前, temp将为null。 You then dereference it. 然后,您取消引用它。

The temp you are referring doesn't exist, temp is NULL. 您所引用的温度不存在,温度为NULL。 To correct this, instead of using temp!=NULL in the while loop condition, use temp->next!= NULL. 若要更正此问题,请使用temp-> next!= NULL代替在while循环条件下使用temp!= NULL。

 while(temp->next!=NULL)
{
   temp = temp->next;
}

Node* new_node = (Node*)malloc(sizeof(Node));

new_node->data = no;
new_node->next = NULL;
temp->next = new_node;

If anyone has this issue, just check if you have created a Default constructor for your List class or not.如果有人遇到此问题,只需检查您是否为 List 类创建了默认构造函数。 In that default constructor " Make your head = NULL ";在那个默认构造函数中“ Make your head = NULL ”;

Code:代码:

class List
{
public:
Node *head;
List()
 {
    head = NULL;
 }
}

Just try this:- while(temp->next =NULL) Instead of while(temp =NULL)试试这个:- while(temp->next =NULL) 而不是 while(temp =NULL)

It's such a silly mistake I was facing it and beileve me it took me months to see such a silly fcking error.这是一个如此愚蠢的错误,我正面临着它,并且相信我花了几个月的时间才看到如此愚蠢的错误。

while(temp != NULL)
    temp = temp->next;

The above code gets you to the last node in the list. 上面的代码将您带到列表中的最后一个节点。 So, you are suppose to add the node to temp itself rather than temp->next . 因此,假设您将节点添加到temp本身,而不是temp->next

temp = (node*)malloc(sizeof(node));

And now the last node's child should be NULL. 现在,最后一个节点的子节点应该为NULL。

temp->next = NULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM