数据结构中像GetElem(L,i,&e)操作在C语言怎么实现实现?

给个具体的函数或者编码!!!谢谢了

/*GetElem(L,i,&e)用E返回L中第i个数据的值*/
#define NULL 0
#include<stdlib.h>
struct student
{
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<j)
{
p1=p1->next;
i++;
}
return(p1->num);
}
温馨提示:答案为网友推荐,仅供参考
相似回答