//////////////////////////////
/// 建立动态链表 ///
//////////////////////////////
#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct student) //"sizeof"求字节数运算符
struct student
{
long num;
float score;
struct student *next;
};
/* 使用函数建立动态链表,带回一个头指针 */
struct student *Creat(void)
{
struct student *head=NULL;
struct student *p1,*p2;
p1 = p2 = (struct student *)malloc(LEN); //开辟一个新单元
scanf("%ld%f", &(p1->num), &(p1->score));
int first=1;
while(p1->num != 0) //输入作为"0 0"结束标志
{
if(first) {head=p1; first=0;}
else p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf("%ld%f", &p1->num, &p1->score);
} /*p2指向链表中最后一个结点,p1指向新开辟的结点,
用"p2->next=p1"连接两个结点,通过这种方式延长链表*/
p2->next = NULL;
return head;
}
int main()
{
struct student *pt;
pt = Creat();
return 0;
}
C语言建立动态链表
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。