C语⾔中的GetElem函数,数据结构中像GetElem(L,i,e)操作
在C语⾔怎么实现实现?
满意答案
lsdiddemj60
推荐于 2017.09.28
/*GetElem(L,i,&e)⽤E返回L中第i个数据的值*/
#define NULL 0
#include
struct student
c语言中struct{
int num;
struct student *next;
}
main()
{struct student *InitList(void);
int GetElem(struct student *head,int j);
int i,j;
struct student *head,*p1,*p2;
head=InitList();
p1=head;
printf("输⼊");
p2=(struct student *)malloc(sizeof(struct student));
scanf("%d",&p2->num);
p1->next=p2;
p1=p1->next;
while(p2->num!=NULL)
{ p2=(struct student *)malloc(sizeof(struct student));
scanf("%d",&p2->num);
p1->next=p2;
p1=p1->next;
}
printf("\n");
p1->next=NULL;
p1=head;
p1=p1->next;
while(p1->num!=NULL)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("要返回第⼏个结点的数据?");
scanf("%d",&j);
i=GetElem(head,j);
printf("\n%d ",i);
}
struct student *InitList(void)/*函数部分*/
{
struct student *L;
L=(struct student *)malloc(sizeof(struct student)); return(L);
}
int GetElem(struct student *head,int j)/*函数部分*/ {
int i=0;
struct student *p1;
p1=head;
while(i
{
p1=p1->next;
i++;
}
return(p1->num);
}
10分享举报

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。