当前位置: 代码迷 >> C语言 >> 这个自定义函数怎么理解 有什么作用
  详细解决方案

这个自定义函数怎么理解 有什么作用

热度:156   发布时间:2008-05-17 20:32:12.0
这个自定义函数怎么理解 有什么作用
#define NULL 0
#define TYPE struct stu
#define LEN sizeof (struct stu)
struct stu
{
int num;
int age;
struct stu *next;
};
TYPE *creat(int n)
{
struct stu *head,*pf,*pb;
int i;
for(i=0;i<n;i++)
{
pb=(TYPE*) malloc(LEN);
printf("input Number and Age\n");
scanf("%d%d",&pb->num,&pb->age);
if(i==0)
pf=head=pb;
else pf->next=pb;
pb->next=NULL;
pf=pb;
}
return(head);
搜索更多相关的解决方案: 函数  定义  

----------------解决方案--------------------------------------------------------
/*尾插法建立单链表*/
#define NULL 0
#define TYPE struct stu
#define LEN sizeof (struct stu)
struct stu
{
int num;
int age;
struct stu *next;
};
TYPE *creat(int n)
{
struct stu *head,*pf,*pb;
int i;
for(i=0;i<n;i++)                   /*一共建立n组元素*/
{
pb=(TYPE*) malloc(LEN);             /*malloc动态分配元素开辟新的存储单元,pb总是去开辟新的结点*/
printf("input Number and Age\n");
scanf("%d%d",&pb->num,&pb->age);
if(i==0)
pf=head=pb;                         /*如果i=0,链表就为空链表*/
else pf->next=pb;
pb->next=NULL;                      /*链表的最后一个结点设为尾结点*/
pf=pb;                               /*pf总是记住最后一个结点*/
}
return(head);
----------------解决方案--------------------------------------------------------
  相关解决方案