1个回答
展开全部
/*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);
}
#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);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |