Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从数组中读取数字然后将其插入列表的程序不会';行不通_C_Arrays_List - Fatal编程技术网

C 从数组中读取数字然后将其插入列表的程序不会';行不通

C 从数组中读取数字然后将其插入列表的程序不会';行不通,c,arrays,list,C,Arrays,List,我必须编写一个程序,从数组中读取整数,然后在大于极限数时逐个插入。我写的是: #include <stdio.h> #include <stdlib.h> #define SIZE 10 struct number_list{ int high_number; struct number_list *next; }; typedef struct number_list number_list; void insert_in_list(i

我必须编写一个程序,从数组中读取整数,然后在大于极限数时逐个插入。我写的是:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 10

struct number_list{

    int high_number;
    struct number_list *next;
    };

typedef struct number_list number_list;

void insert_in_list(int *array, int lim);

int main(void){

    int number_seq[SIZE]={1, 2, 3, 4, 5, 6, 7, 8, 9};
    int limit;
    scanf("%d\n", limit);
    insert_in_list(number_seq, limit);
    }

void insert_in_list(int *array, int lim){

    int i;
    number_list *start_node;
    for(i=0; i<SIZE; i++){

        if(array[i]>lim){

            start_node=malloc(sizeof(number_list));
            start_node->high_number=array[i];
            printf("%d\n", start_node->high_number);
            start_node->next=start_node;
            }

        }

    }
#包括
#包括
#定义尺寸10
结构编号\u列表{
int高_数;
结构编号\u列表*下一步;
};
类型定义结构编号\u列表编号\u列表;
在_列表中无效插入_(int*数组,int lim);
内部主(空){
int number_seq[SIZE]={1,2,3,4,5,6,7,8,9};
整数极限;
scanf(“%d\n”,限制);
在清单中插入(编号顺序、限制);
}
void insert_in_list(int*数组,int lim){
int i;
编号列表*开始节点;
对于(i=0;ilim){
start_node=malloc(sizeof(number_list));
开始_节点->高_数=数组[i];
printf(“%d\n”,开始\u节点->高\u编号);
启动\u节点->下一步=启动\u节点;
}
}
}
发生的情况是,程序没有任何编译错误(除了一个警告,即
%d
需要
*int
类型的参数,而不是
int
,我也不明白,所以如果有人能帮我解决这个问题,那就太好了),但是在运行时,在插入限制号之后,显示分段错误(已创建核心转储)


我想问题出在insert_in_list函数中,但我不明白它是什么。

这里有几个地方出了问题:

(a) insert_in_list函数需要一个额外的参数,告诉into要插入的列表

(b) 为什么您将数字列表结构的成员命名为“高数字”。它不应该只包含一个
数字,因此被简单地称为“数字”吗

(c) 始终将下一个指针设置为结构本身。如果要形成一个列表,它应该指向下一个节点

(d) 大体上,您需要定义指向列表第一个节点的指针,只要列表为空,该节点就应该包含null。这个“锚”代表您的列表

(e) 提示:将指向该锚点的指针传递给insert函数,因为一旦insert函数创建第一个节点,它就必须将第一个节点的地址存放到锚点中。因此,插入函数应如下所示:

void insert_in_list (number_list** pAnchor, int number)

(f) 将输入数组的大小定义为size并在insert_in_list函数中使用该符号不是一个好主意。而是将数组的路径及其长度作为函数的两个参数,或者——正如我在上面的代码行中所做的那样——每次调用只向insert函数传递一个数字,并为您要插入的每个数字调用它。

这将解决您的问题,但是我不理解为什么insert\u in\u list什么也不返回,而您根本不在主函数中使用该列表 如果不是,则让它返回一个指向列表的指针,或者在main中创建列表,并将列表指针传递给函数insert_in_list

#include <stdio.h>
#include <stdlib.h>

#define SIZE 10

typedef struct node *node_pointer;
typedef struct node{
    int high_number;
    node_pointer next;
}number_list;
void insert_in_list(int *array, int lim);
int main(void){
    int number_seq[SIZE]={1, 2, 3, 4, 5, 6, 7, 8, 9};
    int limit;
    scanf("%d", &limit);
    insert_in_list(number_seq, limit);
}
void insert_in_list(int *array, int lim){
    extern void print_list(node_pointer list);
    int i;
    node_pointer list = NULL;
    for(i=0; i<10; i++){
        if(array[i]>lim){
            node_pointer newnode, last = list;

            newnode = (node_pointer)malloc(sizeof(number_list));
            newnode->high_number = array[i];
            newnode->next = NULL;
            if (last == NULL){
                list = newnode;
            }//first node
            else{
                while (1) {
                    if (last->next == NULL) {
                        last->next = newnode;
                        break;
                    }
                    last = last->next;
                }
            }
        }
    }
    print_list(list);
}
void print_list(node_pointer list) {
    node_pointer which;
    which = list;
    printf("\nList 4 :");
    if (which == NULL){ printf("Empty\n"); }
    while (which != NULL) {
        printf("%d", which->high_number);
        which = which->next;
    }
}
#包括
#包括
#定义尺寸10
类型定义结构节点*节点\指针;
类型定义结构节点{
int高_数;
节点\下一个指针;
}数字列表;
在_列表中无效插入_(int*数组,int lim);
内部主(空){
int number_seq[SIZE]={1,2,3,4,5,6,7,8,9};
整数极限;
scanf(“%d”、&limit);
在清单中插入(编号顺序、限制);
}
void insert_in_list(int*数组,int lim){
外部无效打印列表(节点指针列表);
int i;
节点\指针列表=空;
对于(i=0;ilim){
节点\指针newnode,last=列表;
newnode=(node_指针)malloc(sizeof(number_列表));
newnode->high_number=数组[i];
newnode->next=NULL;
if(last==NULL){
列表=新节点;
}//第一节点
否则{
而(1){
如果(上一次->下一次==NULL){
last->next=newnode;
打破
}
最后一个=最后一个->下一个;
}
}
}
}
打印列表(列表);
}
无效打印列表(节点指针列表){
节点\指针,该指针;
其中=列表;
printf(“\n列表4:”);
if(which==NULL){printf(“Empty\n”);}
while(which!=NULL){
printf(“%d”,其中->高\u编号);
哪个=哪个->下一个;
}
}

a)谢谢,我不知道。作业的内容诱使我认为只需要两个论点。b) 没有真正的动机,只是因为它高于极限。c) 也许这正是我不明白的。初始化第一个节点后,我不知道如何创建下一个节点。d) thx,现在也这么做了。f) 好的,我明白了。虽然我不能只传递一个数字,但我的任务要求我传递数组、限制和函数所需的所有其他参数。您还可以为
scanf
添加错误,即它将整数的地址作为
%d
的参数。因此它将是
scanf(“%d\n”,&limit)
limit应该是传递给函数的数组中的元素数吗?然后您的循环应该是:
for(int i=0;i thx!我太笨了,忘了添加&运算符。总之,limit是一个数字,在它之前,其他数字会被忽略。例如,如果我将4设置为limit,那么列表中只应插入5、6、7、8、9。这主要是C mate,而不是C++对它的警告%d希望参数类型为
*int
而不是
int
,这是因为
scanf
将变量的地址作为参数。因此它应该是
scanf(“%d\n”,&limit)
好的,谢谢。你能给我解释一下while语句的参数吗?我不确定你指的是哪个while语句,但如果它是insert_in_列表中的一个,它只会导航到链表的最后一个节点,并将它的下一个节点指定给新条目,而不是NULL。而print_列表中的一个只会通过每个节点:)我希望我回答了你的问题