weixin_46000478 2021-08-17 21:40 采纳率: 100%
浏览 20
已结题

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

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2021-08-18 10:26
    关注

    代码没有大问题,只是不严谨造成的,头结点的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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月26日
  • 已采纳回答 8月18日
  • 修改了问题 8月17日
  • 创建了问题 8月17日

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”