C语言典型例题(二级考试) - 图文 联系客服

发布时间 : 星期五 文章C语言典型例题(二级考试) - 图文更新完毕开始阅读1bdd5064a32d7375a517804a

例24编写通用的函数,可根据需要建立任意节点数的链表。structstudent*creat()/*无参有返回值*/{structstudent*head=NULL,*p1,*p2;intlen;longn;floats;len=sizeof(structstudent);while(1)/*循环次数不确定*/{printf(\number,score:\scanf(\&n,&s);if(n==0)break;/*输入0表示数据结束*/p1=(structstudent*)malloc(len);┇}return(head);}

while(1)

{printf(\number,score:\scanf(\&n,&s);if(n==0)break;/*以0表示数据结束*/p1=(structstudent*)malloc(len);

p1->num=n;p1->score=s;/*转存入节点*/if(head==NULL) head=p2=p1;else { p2->next=p1; p2=p1; }}

p2->next=NULL; /*末尾链节成员赋空*/return(head); /*返回链表头指针*/} /*creat*/

/*输出各节点*/

voidplink(structstudent*head){structstduent*p;inti;

for(i=1;i<=N;i++){if(i==1)p=head;elsep=p->next;

printf(\

i,p->num,p->score);

}

return;}

void plink(struct student *head) /*更具通用性*/{ struct student *p;p=head;

while(p!=NULL)

{ printf(\

p->num, p->score);p=p->next;}

return;}

注:本形式的链表输出函数具有通用性,可适应由于删除或插入节点引起的链表长度的变化。