程序无法运行,希望各位同学能帮忙看一下
#include<stdio.h>#include<malloc.h>
#include<iostream>
#include<string.h>
struct Student{
int sid;
char name;
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;//姓名
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");
}
你的判断分配内存失败的时候应该是两个等于号啊 其他没怎么看你看一下能不能运行吧
页:
[1]