![](/img/trans.png)
[英]C++: Attempting to add new node to linked list yields “Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)” error
[英]C++ node assign error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
我正在学习C ++的DSA(链接列表)。 我的链表例外。
错误信息:
它比应有的更长。
这是我的代码:
定义结构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;
}
稍作休息,我看到链接列表会更长。
我不知道如何取出它。
p_one != p_two
怎么可能是正确的。
应该重载运算符!=
,以比较节点的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.