|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 yiwan 于 2016-5-12 09:54 编辑
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student) //结构大小
struct student
{
int num;
float score;
struct student *next;
};
struct student *creat(); //创建链表
void print(struct student *head); //打印链表
int n; //全局变量
void main()
{
struct student *stu;
stu=creat();
print(stu);
printf("\n\n");
}
struct student *creat()
{
struct student *p1,*p2;
struct student *head;
p1=p2=(struct student *)malloc(LEN);//开辟结构大小;
printf("please enter the num:");
scanf("%d",&p1->num);
printf("please enter the score:");
scanf("%f",&p1->score);
head=NULL;
n=0;
while(p1->num)
{
n++;
if(1==n)
{
head=p1;
}
else
{
p2->next=p1;
}
p1 =(struct student *)malloc(LEN); //p1=p2=(struct student *)malloc(LEN);
//p1指向新节点,p2->next还要保存指向p1新节点!!
printf("please enter the num:");
scanf("%d",&p1->num);
printf("please enter the score:");
scanf("%f",&p1->score);
}
p2->next=NULL;
return head;
}
void print(struct Student * head)
{
struct student * p;
printf("Now,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
|
|