重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

c语言线性函数 c语言线性方程

C语言 统计线性表中元素的个数,int ListLength(struct Sqlist MyLi

线性表可以直接用malloc申请连续空间,按数组保存。但这样不方便后期增删。

创新互联是专业的铁山港网站建设公司,铁山港接单;提供成都网站制作、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行铁山港网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

所以,建议使用链表来实现。

下面代码就是用链表实现线性表。

其中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;

};


分享名称:c语言线性函数 c语言线性方程
文章分享:http://cqcxhl.cn/article/dshoddj.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP