xcode 求表长度代码无错输入结束报错

img

删除howlong函数后可以做到插入数据 查找数据功能但是带入howlong函数可以输入数据 输入数据结束后出现上图所示报错
代码我觉得没问题 是不是编译器问题希望有兄弟可以站出来指点我一波 问题很简单 但是弟弟我蚌埠住了折磨我要一天了


#include <iostream>
using namespace::std;
typedef struct Lnode *list;
struct Lnode
{
    int data;
    list next=NULL;
};
int howlong(list x)
{
    int j=1;
    list p=x;
    while(p!=NULL)
    {
        p=p->next;
        j++;
    }
    return j;
}
list find_it_by_position(list x,int position)
{
    int i=0;
    list p=x;
    while(p!=NULL&&i<position){
        p=p->next;
        i++;
    }
    if(i==position)
        return p;
    else
        return NULL;
}
list find_it_by_target(list x,int target)
{
    list p=x;
    while(p&&p->data!=target)
    {
        if(p->data==target)
            return p;
        p=p->next;
    }
    return  NULL;
}
list insert(list x,int target,int position)
{
    list s,p;
    if(position==0)
    {
        s=(list)malloc(sizeof(struct Lnode));
        s->data=target;
        s->next=x;
        return s;
    }
    s=find_it_by_position(x, position-1);
    if(s==NULL)
    {cout <<"wrong!";
        return NULL;}
    else
    {
        p=(list)malloc(sizeof(struct Lnode));
        p->data=target;
        p->next=s->next;
        s->next=p;
    }
    return x;
}
int main()
{
    int x;
    cin >>x;
    list l=(list)malloc(sizeof(struct Lnode));
    for(int i=1;i<=x;i++)
    {
        int q;
        cin >>q;
        insert(l,q,i);
    }
    cout <<howlong(l);
    cout <<find_it_by_position(l, 3)->data;
}

代码没有大问题,只是不严谨造成的,头结点的next指针没定义指向NULL,和编译器无关,修改如下,供参考:

#include <iostream>
using namespace  std;//这里做了修改
typedef struct Lnode* list;
struct Lnode
{
    int data;
    list next = NULL;
};
int howlong(list x)
{
    int j = 1;
    list p = x;
    while (p != NULL)
    {
        p = p->next;
        j++;
    }
    return j;
}
list find_it_by_position(list x, int position)
{
    int i = 0;
    list p = x;
    while (p != NULL && i < position) {
        p = p->next;
        i++;
    }
    if (i == position)
        return p;
    else
        return NULL;
}
list find_it_by_target(list x, int target)
{
    list p = x;
    while (p && p->data != target)
    {
        if (p->data == target) 
            return p;
        p = p->next;
    }
    return  NULL;
}
list insert(list x, int target, int position)
{
    list s, p;
    if (position == 0)
    {
        s = (list)malloc(sizeof(struct Lnode));
        s->data = target;
        s->next = x;
        return s;
    }
    s = find_it_by_position(x, position - 1);
    if (s == NULL)
    {
        cout << "wrong!";
        return NULL;
    }
    else
    {
        p = (list)malloc(sizeof(struct Lnode));
        p->data = target;
        p->next = s->next;
        s->next = p;
    }
    return x;
}
int main()
{
    int x;
    cin >> x;
    list l = (list)malloc(sizeof(struct Lnode));
    l->next = NULL;   //这里做了修改
    for (int i = 1; i <= x; i++)
    {
        int q;
        cin >> q;
        insert(l, q, i);
    }
    cout << howlong(l)<<endl;
    cout << find_it_by_position(l, 3)->data;
}