链表之初识

#include <stdio.h>
#include <malloc.h>
#define  N  sizeof(struct student)
struct student
{
	int num;
	char name[20];
	int score;
	struct student *next;
};
struct student* creat_list(void)
{	struct student* Head,*p0,*p1;//结构体指针变量
	Head=p0=(struct student*)malloc(N);//指向头结点
	p1=(struct student*)malloc(N);//指向第一个结点

	scanf("%d %s %d",&p1->num,p1->name,&p1->score);
	
	while(p1->num!=0)//输入学号为0那么退出循环
	{	p0->next=p1;
		p0=p1;//指向下一个节结
		p1=(struct student*)malloc(N);
		
	scanf("%d %s %d",&p1->num,p1->name,&p1->score);
	}
	p0->next=NULL;
	free(p1);//释放存学号为0的空间
	return Head;
}
void print_list(struct student* pH)
{
	pH=pH->next;
	while(pH!=NULL)
	{
		printf("%d %s %d\n",pH->num,pH->name,pH->score);
		pH=pH->next;
	}
}
int main (void)
{
	struct student* head;
	head=creat_list();//返回头结点的地址
	print_list(head);//发送头结点的地址

}

猜你喜欢

转载自blog.csdn.net/L_Z_jay/article/details/103675005