鱼C论坛

 找回密码
 立即注册
查看: 2240|回复: 1

程序无法运行,希望各位同学能帮忙看一下

[复制链接]
发表于 2020-5-1 19:34:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<string.h>
struct Student{
        int sid;
        char name[100];
        int age;
        struct Student * next;
};
//初始化链表
struct Student * create_list();
//输出链表
void show_list(struct Student * phead);

int main(){
        struct Student * head;//定义头指针
        head=create_list();
        show_list(head);

}
struct Student *create_list(){
        struct Student *head;
        int sid;//学号
        char name[100];//姓名
        int age;//年龄
        int len;//链表长度
        head=(struct Student *)malloc(sizeof(struct Student));
        if(head=NULL){
                printf("分配内存失败");
                exit(-1);
        }
        struct Student *well;
        well=head;
        well->next=NULL;
        printf("请输入您要创建的链表的长度");
        scanf("%d",&len);
       
        for(int i=0;i<len;i++){
                printf("请输入学生的学号\n");
                scanf("%d",&sid);
                printf("请输入学生姓名\n");
                scanf("%s",name);
                printf("请输入学生年龄\n");
                scanf("%d",&age);
                struct Student *pNew;
                pNew =(struct Student *)malloc(sizeof(struct Student));
                if(pNew=NULL){
                        printf("分配内存失败");
                        exit(-1);
                }
                pNew->sid=sid;
                strcpy(pNew->name ,name);
                pNew->age=age;
                well->next=pNew;               
                pNew->next=NULL;
                well->next=NULL;
        }
        return head;
}
void show_list(struct Student *phead){
        struct Student *NEW = phead->next;
        while(NEW != NULL){
                printf("学生学号是%d\n",NEW->sid);
                printf("学生姓名是%s\n",NEW->name);
                printf("学生年龄是%d\n",NEW->age );
               
        }
        printf("\n");
}











想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-6 18:26:23 | 显示全部楼层
你的判断分配内存失败的时候应该是两个等于号啊 其他没怎么看你看一下能不能运行吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-22 13:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表