黑马程序员技术交流社区

标题: 动态链表的建立 [打印本页]

作者: 墓笙    时间: 2014-12-13 11:21
标题: 动态链表的建立
#include "stdio.h"


struct student
{
char name[12];
int score ;
char sex[6];
struct student * next;
};
main()
{
struct student * head ,* p1, * p2; struct student *p;
int n=0;
p1=p2=(struct student *)malloc(sizeof(struct student));
head=NULL;
printf("intput date name,score and sex:\n");
scanf("%s%d%s",&p1->name,&p1->score ,&p1->sex);
while (p1->score>=0)
{
n++;
if (n==1) head=p1;
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
printf("intput date name,score and sex:\n");
scanf("%s%d%s",&p1->name,&p1->score ,&p1->sex);
}
p2->next=NULL;
p=head;
while(p!=NULL)
{
printf("name:%s\nscore:%d\nsex:%s\n\n",p->name,p->score,p->sex);
p=p->next;
}
getch();
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2