建立一个动态列表(结点的结构自拟)
来源:百度知道 编辑:UC知道 时间:2024/06/23 07:59:05
写出程序代码,输入是数据和输出结果,谢谢!!
楼上的也太复杂了,我给个简单的:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct student
{
char name[20];
char sex[7];
int score ;
struct student * next;
};
main()
{
struct student *head=NULL;
struct student *p,*p1;
char name[20],sex[7];
int score;
//建立链表
while(1)
{
printf("Input name, sex and score:\n");
scanf("%s%s%d",name,sex,&score );
if(score<0)
{
break;
}
p1=(struct student *)malloc(sizeof(struct student));
p1->next=NULL;
strcpy(p1->name,name);
strcpy(p1->sex,sex);
p1->score=score;
if(head==NULL)
{
head=p1;
p=head;
}
else
{
p->next=p1;
p=p1;
}
}
//打印链表
p=head;
while(p!=NULL)
{
printf