蹲家宅宅 2023-08-17 10:58 采纳率: 52%
浏览 7

C语言:head插入新节点程序无法运行,以下情况如何解决

C语言:head插入新节点程序无法运行,以下情况如何解决

img


#include<stdio.h>
#include<stdlib.h>
struct Node
{
    int data;
    struct Node* next;
};
struct Node* head;
void insert(int x)
{
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    temp->data = x;
    temp->next = head;//涵盖head = NULL情况
    head = temp;
}
void print(){
    struct Node* temp = head;
    printf("list is:");
    while (temp != NULL)
    {
        printf(" %d",temp->data);
        temp = temp->next;
    }
    printf("\n");  
}
int main(){
    head = NULL;
    printf("how many numbers?");
    int n,i,x;
    scanf("%d",&n);
    for ( i = 0; i < n; i++)
    {
        printf("enter the number:\n");
        scanf("%d",&x);
        insert(x);
        print();
    }
    
}

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-08-17 11:13
    关注

    代码没有问题,你的编译的命令行有问题

    评论

报告相同问题?

问题事件

  • 创建了问题 8月17日

悬赏问题

  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型