Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.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
为什么在插入第一个元素后程序会终止? #包括 #包括 #包括“node2.h” void create(); int main(){ 创建(); 返回0; } void create(){ int n,num; printf(“要创建多少个节点?\n”); scanf(“%d”和“&n”); 而(n数据=num; head->link=NULL; 温度=水头; 对于(int i=0;i数据=num; 当前->链接=空; 温度->链接=当前; 温度=电流; } 返回; }_C_Linked List - Fatal编程技术网

为什么在插入第一个元素后程序会终止? #包括 #包括 #包括“node2.h” void create(); int main(){ 创建(); 返回0; } void create(){ int n,num; printf(“要创建多少个节点?\n”); scanf(“%d”和“&n”); 而(n数据=num; head->link=NULL; 温度=水头; 对于(int i=0;i数据=num; 当前->链接=空; 温度->链接=当前; 温度=电流; } 返回; }

为什么在插入第一个元素后程序会终止? #包括 #包括 #包括“node2.h” void create(); int main(){ 创建(); 返回0; } void create(){ int n,num; printf(“要创建多少个节点?\n”); scanf(“%d”和“&n”); 而(n数据=num; head->link=NULL; 温度=水头; 对于(int i=0;i数据=num; 当前->链接=空; 温度->链接=当前; 温度=电流; } 返回; },c,linked-list,C,Linked List,当我运行这段代码时,程序不会经过scanf(“%d”和&num);当我忘记scanf中的&symbol时,这种情况总是发生在我身上,但在本例中并非如此。那么,为什么它会被终止呢?放置在while循环中的条件,即..while(n=0) 代码甚至不会迭代。这是一种逻辑错误。if(head=NULL)=>if(head==NULL)…并打开并注意编译器警告。在C中,当您声明一个没有参数的函数时,需要指定void。声明void create();将create声明为一个函数,该函数接受未指定类型的未指

当我运行这段代码时,程序不会经过scanf(“%d”和&num);当我忘记scanf中的&symbol时,这种情况总是发生在我身上,但在本例中并非如此。那么,为什么它会被终止呢?

放置在while循环中的条件,即..while(n=0)
代码甚至不会迭代。这是一种逻辑错误。

if(head=NULL)
=>
if(head==NULL)
…并打开并注意编译器警告。在C中,当您声明一个没有参数的函数时,需要指定
void
。声明
void create();
create
声明为一个函数,该函数接受未指定类型的未指定数量的参数。
#include <stdio.h>

#include <stdlib.h>

#include "node2.h"

void create();

int main(){

    create();
    return 0;
}

void create(){

    int n, num;
    printf("How many nodes do you want to create?\n");
    scanf("%d", &n);
    while(n <= 0){
        printf("Please enter a number greater than 0 or '-1' to exit\n");
        scanf("%d", &n);
        if(n == -1) exit(1);
    }

    struct node *head = (struct node *)malloc(sizeof(struct node)), *temp;
    if(head = NULL) printf("Memory cannot be allocated");


    printf("Enter the elements you want to store\n");
    printf("Element at node 1: ");
    scanf("%d", &num);

    head -> data = num;
    head -> link = NULL;
    temp = head;

    for(int i = 0; i < n - 1; i++){
        struct node *current = (struct node *)malloc(sizeof(struct node));
        printf("Element at node %d: ", i + 2);
        scanf("%d", &num);

        current -> data = num;
        current -> link = NULL;
        temp -> link = current;
        temp = current;
    }

    return;
}