Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将指针传递到链表时出现分段错误_C++_Segmentation Fault - Fatal编程技术网

C++ 将指针传递到链表时出现分段错误

C++ 将指针传递到链表时出现分段错误,c++,segmentation-fault,C++,Segmentation Fault,各位程序员好,下面的代码给出了分段错误。此代码旨在在链表的末尾插入一个元素。我试着用print语句来调试它。我认为错误在于将链表指针传递给insert()函数。请告诉我怎样才能更正它。提前谢谢 代码如下: #include <iostream> using namespace std; class node { public: int data; node *next; node(int data) { this->data = dat

各位程序员好,下面的代码给出了分段错误。此代码旨在在链表的末尾插入一个元素。我试着用print语句来调试它。我认为错误在于将链表指针传递给insert()函数。请告诉我怎样才能更正它。提前谢谢

代码如下:

#include <iostream>
using namespace std;

class node {
public:
    int data;
    node *next;
    node(int data) {
        this->data = data;
        this->next = NULL;
    } 
};

class linked_list {
public: 
    node *head;
    linked_list() {
        this->head = NULL;
    }
};

void insert(node **head, int data);
void print(linked_list *L);

int main() {
    int N;
    linked_list *A = new linked_list();
    
    cout << "N: ";
    cin >> N;
    for(int i=0; i<=N-1; i++) {
        int t;
        cin >> t;
        insert(&(A->head), t);
    }
    print(A);
    return 0;
}

void insert(node **head, int data) {
    node *temp = new node(data);
    if(*head == NULL) {
        *head = temp;
        return;
    } else {
        node *t = *head;
        while(t->next != NULL) {
            t=t->next;
        }
        t->next = temp;
        return;
    }
}

void print(linked_list *L) {
    node * t = L->head;
    while(t!=NULL) {
        cout << t->data << " ";
        t = t->next;
    }
    return;
}
#包括
使用名称空间std;
类节点{
公众:
int数据;
节点*下一步;
节点(int数据){
这->数据=数据;
此->下一步=空;
} 
};
类链表{
公众:
节点*头;
链表(){
此->头=空;
}
};
无效插入(节点**头部,整数数据);
作废打印(链接列表*L);
int main(){
int N;
链表*A=新链表();
cout>N;
对于(int i=0;i>t;
插入(&(A->头部),t);
}
印刷品(A);
返回0;
}
无效插入(节点**头部,整数数据){
节点*临时=新节点(数据);
如果(*head==NULL){
*压头=温度;
返回;
}否则{
节点*t=*头部;
while(t->next!=NULL){
t=t->next;
}
t->next=温度;
返回;
}
}
无效打印(链接列表*L){
节点*t=L->head;
while(t!=NULL){
下一步是收集数据;
}
返回;
}

您使用的是要进行比较的赋值。

我强烈建议使用链表的插入和打印功能。然后您只需调用a->insert(t);和insert可以访问此->head。当我编译代码时,我注意到您不能启用编译器警告。我强烈建议您启用编译器警告。大多数编译器可以对常见的编码错误进行静态分析,就像以前使用的旧lint工具一样(但现在已内置到编译器中)。我要问你为什么要这样做
linked_list*A=new linked_list();
而不是更简单、更高效、更明显的等
linked_list A;
指针不是必须的。另外
插入
(可能还有
打印
)应该是你的
链表类的方法,而不是自由函数。也许你只是想解决这个问题。这就是为什么我总是这样检查的原因:
if(NULL=*head)
(先是常量值,然后是非常量值)
main.cpp:42:14: error: using the result of an assignment as a
      condition without parentheses [-Werror,-Wparentheses]
    if(*head = NULL) {
       ~~~~~~^~~~~~
main.cpp:42:14: note: place parentheses around the assignment to
      silence this warning
    if(*head = NULL) {
             ^
       (           )
main.cpp:42:14: note: use '==' to turn this assignment into an
      equality comparison
    if(*head = NULL) {
             ^
             ==
1 error generated.