C++ 节点分配错误:线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0) [英] C++ node assign error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

查看:53
本文介绍了C++ 节点分配错误:线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 C++ 新手 DSA、链表.我的链表是个例外.

I am learning DSA , Linked List , new to C++. My linked list is of exception.

错误信息:

它比它应该的要长得多.

It is much longer than it should be.

这是我的代码:

定义结构ListNode

define struct 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;
}

构造样本并调用两个链表的交集

construct samples and call Intersection of Two Linked Lists

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;
}

通过断点,我看到链表要长得多.

By take a break point, I see the linked list is much longer.

我不知道怎么取出来.

推荐答案

p_one != p_two 怎么可能是真的.

应该重载运算符!=,比较节点的值.

Should overload the operator !=, to compare the value of the node.

这篇关于C++ 节点分配错误:线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆