Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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++;节点分配错误:线程1:EXC\u错误\u访问(代码=1,地址=0x0) 我正在学习DSA,链表,C++新的。 我的链表是个例外_C++_Struct_Construct - Fatal编程技术网

C++;节点分配错误:线程1:EXC\u错误\u访问(代码=1,地址=0x0) 我正在学习DSA,链表,C++新的。 我的链表是个例外

C++;节点分配错误:线程1:EXC\u错误\u访问(代码=1,地址=0x0) 我正在学习DSA,链表,C++新的。 我的链表是个例外,c++,struct,construct,C++,Struct,Construct,错误信息: 它比应该的要长得多 这是我的密码: 定义结构ListNode // Definition for singly-linked list. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; 获取两个链表的交集 ListNode * Solution_three :: getIntersectionNode(ListNode *

错误信息:

它比应该的要长得多

这是我的密码:

定义结构ListNode

// Definition for singly-linked list.
struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
 };
获取两个链表的交集

 ListNode * Solution_three :: getIntersectionNode(ListNode *headA, ListNode *headB) {

    ListNode * p_one = headA;
    ListNode * p_two = headB;

    if (p_one == NULL || p_two == NULL) {
        return NULL;
    }

    while (p_one != NULL && p_two != NULL && p_one != p_two) {
        p_one = p_one -> next;
        p_two = p_two -> next;


        if( p_one == p_two ){
            return p_one;
        }

        if (p_one == NULL) {
            p_one = headB;
        }
        if (p_two == NULL) {
            p_two = headA;
        }
    }
    return p_one;
}
void Solution_three :: test_Intersection(){

    ListNode *node_one = new ListNode(1);
    ListNode *two = new ListNode(3);
    ListNode *three =  new ListNode(5);
    ListNode *four =  new ListNode(7);
    ListNode *five =  new ListNode(9);
    ListNode *six =  new ListNode(11);

    node_one->next = two;
    two->next = three;
    three->next = four;
    four->next = five;
    five->next = six;


    ListNode *node_a_one = new ListNode(2);
    ListNode *a_two = new ListNode(5);
    ListNode *a_three = new ListNode(9);
    ListNode *a_four = new ListNode(19);
    node_a_one->next = a_two;
    a_two->next = a_three;
    a_three->next = a_four;


    ListNode node_r = *getIntersectionNode(node_one, node_a_one);
    printf("%d", node_r.val);
    cout<<"\n"<<node_r.val<<endl;
}
构造样本并调用两个链表的交集

 ListNode * Solution_three :: getIntersectionNode(ListNode *headA, ListNode *headB) {

    ListNode * p_one = headA;
    ListNode * p_two = headB;

    if (p_one == NULL || p_two == NULL) {
        return NULL;
    }

    while (p_one != NULL && p_two != NULL && p_one != p_two) {
        p_one = p_one -> next;
        p_two = p_two -> next;


        if( p_one == p_two ){
            return p_one;
        }

        if (p_one == NULL) {
            p_one = headB;
        }
        if (p_two == NULL) {
            p_two = headA;
        }
    }
    return p_one;
}
void Solution_three :: test_Intersection(){

    ListNode *node_one = new ListNode(1);
    ListNode *two = new ListNode(3);
    ListNode *three =  new ListNode(5);
    ListNode *four =  new ListNode(7);
    ListNode *five =  new ListNode(9);
    ListNode *six =  new ListNode(11);

    node_one->next = two;
    two->next = three;
    three->next = four;
    four->next = five;
    five->next = six;


    ListNode *node_a_one = new ListNode(2);
    ListNode *a_two = new ListNode(5);
    ListNode *a_three = new ListNode(9);
    ListNode *a_four = new ListNode(19);
    node_a_one->next = a_two;
    a_two->next = a_three;
    a_three->next = a_four;


    ListNode node_r = *getIntersectionNode(node_one, node_a_one);
    printf("%d", node_r.val);
    cout<<"\n"<<node_r.val<<endl;
}
void Solution_three::test_Intersection(){
ListNode*node_one=新的ListNode(1);
ListNode*two=新的ListNode(3);
ListNode*3=新的ListNode(5);
ListNode*four=新的ListNode(7);
ListNode*5=新的ListNode(9);
ListNode*6=新的ListNode(11);
节点_one->next=2;
两个->下一个=三个;
三->下一个=四个;
四->下一个=五个;
五->下一个=六;
ListNode*node_a_one=新的ListNode(2);
ListNode*a_two=新的ListNode(5);
ListNode*a_three=新的ListNode(9);
ListNode*a_four=新的ListNode(19);
节点a_one->next=a_two;
a_two->next=a_two;
a_三->下一步=a_四;
ListNode node\u r=*getIntersectionNode(node\u one,node\u a\u one);
printf(“%d”,节点值);

怎么可能是真的呢


应重载运算符
!=
,以比较节点的值。

ListNode*node=(ListNode*)malloc(sizeof(ListNode))
--未定义的行为。不要使用
malloc
构造非POD类型。使用
new
,而不是
malloc
。其次,要获取相交节点,只需使用
无序集即可。只需将第一个列表的所有节点添加到集合中。然后,对于第二个列表中的每个节点,检查它是否在集合。如果那个节点在集合中,那就是相交的节点。集合是有力量的。谢谢你的建议。这是LeetCode 160。两个链表的相交你能给我一点提示吗?@PaulMcKenzie。我使用
new
,但它仍然…如果你转到链接,“解决方案”选项卡中有一个使用哈希表的描述。
unordered\u set
是哈希表。