小编Tej*_*svi的帖子

在链接列表中的第 n 个位置插入显示分段错误

void Insert(int data, int n){
    Node* temp1 = new Node();
    temp1 -> data = data;
    temp1 -> next = NULL;
    if(n == 1){
        temp1 -> next = head;
        head = temp1;
        return;
    }
    else{
        Node* temp2 = head;
        for(int i; i< n-2; i++){
            temp2 = temp2 -> next;
        }
        temp1 -> next = temp2 -> next;
        temp2 -> next = temp1;
    }
}
Run Code Online (Sandbox Code Playgroud)

我遇到了分段错误,但无法找出问题所在。

c++ linked-list

2
推荐指数
1
解决办法
78
查看次数

标签 统计

c++ ×1

linked-list ×1