热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

链接列表数组在C中无法正常工作-ArrayoflinkedListnotworkingproperlyinC

Imtryingtomakeanarrayofnodes,andentervaluesfromS[]arraytoit.ButIkeepgettingaseg

I'm trying to make an array of nodes,and enter values from S[] array to it. But I keep getting a segmentation fault. My struct looks like this:

我正在尝试创建一个节点数组,并从S []数组中输入值。但我一直遇到分段错误。我的结构看起来像这样:

typedef struct Node {
int num;
struct Node *next;
}ListNode;

Initialized array of S[10] with random numbers:

带有随机数的S [10]的初始化数组:

    printf("list is:");

    for(i=0;i<10; i++)
    {
            RN= (random()+1);
            S[i]=RN;
            printf("%d  ",S[i]);
    }
    printf("\n");

Here is how I initialized my array of nodes:

以下是我初始化节点数组的方法:

    ListNode *bucket[Radix];
    for(j=0; jnext=NULL;
      }

And this is the function I use to read array numbers from S[] into the array of linked list, bucket[]:

这是我用来将S []中的数组编号读入链表,bucket []数组的函数:

     for(y=0;y<(sizeof(S)/sizeof(int));y++) // S is size of a normal array
        {
            digit=bucketNumber(S[y],1);// returns the first digit values
            pointer= bucket[digit];
            number= S[y];
            insert_tail(pointer, number);
        }

my insert_tail function look like this:

我的insert_tail函数看起来像这样:

    ListNode *insert_tail(ListNode *tail, int data)
    // insert and element at tail

    {

    if (tail==NULL)
    { tail=(ListNode *)malloc(sizeof(ListNode));}

    else
    {
        while(tail->next !=NULL)

        tail->next = (ListNode *)malloc(sizeof(ListNode));
        tail= tail->next;
    }

    tail->num= data;
    tail->next=NULL;
    }

this is the bucketNumber function:

这是bucketNumber函数:

    int bucketNumber(int num,int digit)
    {
    int x, y;
    y= 10*digit;
    x= num%y;

    if(digit>=2)
    {
      num= num%y;
      x= num/(y/10);
    }
    return (x);

}

}

I think the reason for segmentation fault is that my function is not creating the links in the array properly. I'm not sure thou, there could be something else wrong!

我认为分段错误的原因是我的功能是没有正确地在数组中创建链接。我不确定你,可能还有别的错!

1 个解决方案

#1


2  

I think the problem is in this section of insert_tail():

我认为问题出在insert_tail()的这一部分:

else
    {
        while(tail->next !=NULL)

        tail->next = (ListNode *)malloc(sizeof(ListNode));
        tail= tail->next;
    }

    tail->num= data;
    tail->next=NULL;

Since the while() loop doesn't have curly braces following it, the below line gets executed if and only if tail->next != NULL:

由于while()循环后面没有花括号,当且仅当tail-> next!= NULL时,才会执行以下行:

tail->next = (ListNode *)malloc(sizeof(ListNode));

...which is sort of the opposite of what you want; you want to allocate a new node for next if next is NULL. As is, next is likely NULL, and so tail gets moved forward to next, but next is not allocated--it's NULL. The bottom two lines above would each cause a segmentation fault in that case, since you can't dereference a NULL pointer, which is what -> is doing.

......这与你想要的相反;如果next为NULL,则要为next分配新节点。原样,next可能是NULL,因此tail会向前移动到next,但是next不会被分配 - 它是NULL。在这种情况下,上面的两行会导致分段错误,因为你不能取消引用NULL指针,这就是 - >正在做的事情。


推荐阅读
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 本文介绍了在go语言中利用(*interface{})(nil)传递参数类型的原理及应用。通过分析Martini框架中的injector类型的声明,解释了values映射表的作用以及parent Injector的含义。同时,讨论了该技术在实际开发中的应用场景。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 摘要: 在测试数据中,生成中文姓名是一个常见的需求。本文介绍了使用C#编写的随机生成中文姓名的方法,并分享了相关代码。作者欢迎读者提出意见和建议。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • Givenasinglylinkedlist,returnarandomnode'svaluefromthelinkedlist.Eachnodemusthavethe s ... [详细]
  • 上图是InnoDB存储引擎的结构。1、缓冲池InnoDB存储引擎是基于磁盘存储的,并将其中的记录按照页的方式进行管理。因此可以看作是基于磁盘的数据库系统。在数据库系统中,由于CPU速度 ... [详细]
  • 开源Keras Faster RCNN模型介绍及代码结构解析
    本文介绍了开源Keras Faster RCNN模型的环境需求和代码结构,包括FasterRCNN源码解析、RPN与classifier定义、data_generators.py文件的功能以及损失计算。同时提供了该模型的开源地址和安装所需的库。 ... [详细]
  • OpenMap教程4 – 图层概述
    本文介绍了OpenMap教程4中关于地图图层的内容,包括将ShapeLayer添加到MapBean中的方法,OpenMap支持的图层类型以及使用BufferedLayer创建图像的MapBean。此外,还介绍了Layer背景标志的作用和OMGraphicHandlerLayer的基础层类。 ... [详细]
  • #define_CRT_SECURE_NO_WARNINGS#includelist.h#includevoidSListInit(PNode*pHead ... [详细]
  • Python教学练习二Python1-12练习二一、判断季节用户输入月份,判断这个月是哪个季节?3,4,5月----春 ... [详细]
author-avatar
手机用户2502913853
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有