|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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");
}
|
|