下面的代码崩溃了,我不确定为什么。试图计算在以下链接列表中找到整数的次数..但是 xcode 一直说 int count=0 来自 main 正在破坏线程?
#include <iostream>
using namespace std;
struct Node {
int val;
Node *next;
};
int countNum (Node *head, int key);
Node* cons (int x, Node* p);
int main()
{
Node *head = (1,cons(2,cons(2,(cons(4,(cons(5,nullptr)))))));
int counts=0;
counts= countNum(head,2);
cout<< counts<< head;
return 0;
}
Node* cons (int x, Node* p){
Node *q=new Node;
q->val=x;
q->next=p;
return p;
}
int countNum (Node *head, int key) {
int count=0;
if (head==nullptr)
return 0;
Node *follow=head;
while (follow!=nullptr) {
if(follow->val==key)
count++;
follow=follow->next;
}
cout<<count;
return count;
}