跪坐小人可爱表情包:编写函数,与链表有关

来源:百度文库 编辑:神马品牌网 时间:2024/05/06 07:32:56
编写一个函数,其功能是求出1到m之内(包括m)能被7或11整除,但不能同时被7和11整除的所有整数放在一个链表list中(函数的返回值为链表的头指针).(函数说明自定)
能在tc上运行更好

#include <stdio.h>
struct list /*定义结构体list,存储一个符合条件的整数*/
{
int num;
struct list *next;
};
struct list *calc(int m) /*所求函数*/
{
int i;
struct list *head=NULL,*p1,*p2=NULL;
for(i=1;i<=m;i++)
{
if((i%7==0||i%11==0)&&i%77!=0)
{
p1=(struct list *)malloc(sizeof(struct list));
p1->next=NULL;
p1->num=i;
if(p2==NULL)head=p1;
else p2->next=p1;
p2=p1;
}
}
return head;
}
/*主函数例子*/
main()
{
int m;
struct list *p;
printf("Enter m:");
scanf("%d",&m);
p=calc(m);
while(p!=NULL)
{
printf("%d\n",p->num);
p=p->next;
}
getch();
}