c语言线性函数 c语言线性方程
C语言 统计线性表中元素的个数,int ListLength(struct Sqlist MyLi
线性表可以直接用malloc申请连续空间,按数组保存。但这样不方便后期增删。
所以,建议使用链表来实现。
下面代码就是用链表实现线性表。
其中initList函数是生成了一个10节点的单向链表作为线性表。
ListLength就是题目要的函数。(函数中顺带打印了链表内容,你不想要显示链表内容咐汪,就删掉printf语句)。
#includestdio.h
#includemalloc.h
struct Sqlist
{
int num;
struct Sqlist *next;
} ;
struct Sqlist *initList(); //初始化一个线性链表
int ListLength(struct Sqlist MyList);
int main()
{
struct Sqlist *mylist;
mylist=initList();
printf("\n线性表中元素个数为:%d\n",ListLength(*mylist));
return 0;
}
int ListLength(struct Sqlist MyList)
{
int cnt=0;
struct Sqlist *headList=MyList;
printf("生成的线性表各元素值为:");
while(headList)
{
printf("%d ",headList-num);
cnt++;
headList=headList-next;
}
return cnt;
}
struct Sqlist 槐桥*initList()
{
int i;
struct Sqlist *newList=NULL,*firstList=NULL,*lastList=NULL;
for(i=1; i=10; i++)
{
newList=(struct Sqlist *)malloc(sizeof(struct Sqlist));
if(!newList)
return NULL;
newList-num=i;
newList-next=NULL;
衡明仔 if(!firstList)
firstList=newList;
else
lastList-next=newList;
lastList=newList;
}
return firstList;
} ;
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: c语言线性函数 c语言线性方程
本文地址: https://pptw.com/jishu/297019.html