線性表可以直接用malloc申請連續(xù)空間,按數(shù)組保存。但這樣不方便后期增刪。

創(chuàng)新互聯(lián)是專業(yè)的鐵山港網(wǎng)站建設(shè)公司,鐵山港接單;提供成都網(wǎng)站制作、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行鐵山港網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
所以,建議使用鏈表來實現(xiàn)。
下面代碼就是用鏈表實現(xiàn)線性表。
其中initList函數(shù)是生成了一個10節(jié)點的單向鏈表作為線性表。
ListLength就是題目要的函數(shù)。(函數(shù)中順帶打印了鏈表內(nèi)容,你不想要顯示鏈表內(nèi)容咐汪,就刪掉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線性表中元素個數(shù)為:%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語言線性函數(shù) c語言線性方程
分享地址:http://chinadenli.net/article17/dshoddj.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)頁設(shè)計公司、電子商務(wù)、軟件開發(fā)、移動網(wǎng)站建設(shè)、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)