|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student
{
char *name;
int num;
struct student *next;
};
void main()
{
struct student * pt;
struct student * create();//声明动态链表建立函数
pt=create();
while(pt!=NULL)//当pt不为NULL时,输出其结构体的各项
{
printf("%s %d\n",pt->name,pt->num);
pt=pt->next;
}
}
int n=0;//作为判断是否为第一个结构的依据
struct student * create()
{
struct student * head,*p1,*p2;
printf("please input your log:\n");
p1=p2=(struct student *)malloc(LEN);//开辟一个新单元
scanf("%s,%d",p1->name,&p1->num);//输入第一个学生的名字和序号
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);//开辟新单元,并且将其起始地址赋给p1
scanf("%s,%d",p1->name,&p1->num);
}
p2->next=NULL;
free(p1);
return head;
}
:sad: |
|