#includestdio.h
目前創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機、網(wǎng)站托管維護、企業(yè)網(wǎng)站設(shè)計、嫩江網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
#includestdlib.h
#define OK 1
#define ERROR 0
typedef char ElemType ;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
int InitList(LinkList *L )
{
(*L)=(struct LNode *)malloc(sizeof(LNode));
if(!(*L))
return ERROR;
(*L)-next=NULL;
return OK;
}
int InsertList(LinkList *L,int i,ElemType e)
{
LinkList p,s;
int j=0;
p=*L;
while(pji-1)
{
p=p-next ;
++j;
}
if(!p||ji-1)
return ERROR;
s=(struct LNode *)malloc(sizeof(LNode));
s-data=e;
s-next=p-next ;
p-next=s;
return OK;
}
void print(ElemType e)
{
printf("%d-",e);
}
void TraverseList(LinkList L,void(*visit)())
{
LinkList p;
p=L-next ;
while(p )
{
visit(p-data);
p=p-next ;
}
return OK;
}
int DeleteList(LinkList *L,int i,ElemType e)
{
LinkList p;
int j=0;
p=(*L)-next ;
while(pji-1)
{
p=p-next ;
++j;
}
if(!p||ji-1)
return ERROR;
e=p-data ;
return e;
}
int GetElem(LinkList L,ElemType e)
{
LinkList p;
int i=0;
p=L-next ;
while(p)
{
i++;
if(p-data==e)
{
printf("the number you want to find exist! its posotion is %d\n\n",i);
return OK;
}
p=p-next ;
}
printf("the number you want to find not exist!!\n\n");
return OK;
}
int Length(LinkList L)
{
LinkList p;
int i=0;
p=L-next ;
while(p)
{
i++;
p=p-next ;
}
return i;
}
main()
{
LinkList L;
ElemType i;
ElemType e;
InitList(L);
for(i=0;i10;i++)
InsertList(L,1,i);
printf("鏈表為:");
TraverseList(L,print);
printf("鏈表長度為:%d\n\n",Length(L));
printf("input the position of the number you want to detele :");
scanf("%d",i);
printf("the number you want to detele is %d\n",DeleteList(L,i,e));
printf("please input the data you want to find is :");
scanf("%d",e);
GetElem(L,e,i);
}
int CreateList(LinkList *head)代碼的while循環(huán)中,加入了一句話:
if (getchar() == '\n') break; //?輸入換行時可跳出循環(huán)
新寫的int getLength(LinkList head)代碼如下:
完整代碼為:
#includestdio.h
#includemalloc.h
#define ERROR 0
#define OK 1
typedef int ElemType; /*定義表元素的類型*/
typedef struct LNode /*線性表的單鏈表存儲*/
{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
/*創(chuàng)建單鏈表*/
int CreateList(LinkList *head);
/*函數(shù)定義*/
int getLength(LinkList head);
/*你的代碼將寫在此處*/
int CreateList(LinkList *head)
{
LNode *p, *q;
int e;
*head = (LinkList)malloc(sizeof(LNode));
if (!*head)
? return ERROR;
(*head)-next = NULL;
p = *head;
while (scanf("%d", e) == 1)
{
? if (getchar() == '\n')
? ? ? break; // 回車時跳出循環(huán)
? q = (LNode *)malloc(sizeof(LNode));
? if (!q)
? ? ? return ERROR;
? q-data = e;
? q-next = NULL;
? p-next = q;
? p = q;
}
return OK;
}/*CreateList*/
int getLength(LinkList head)
{
LNode *p = head;
int len = 0;
while (p != NULL)
{
? ++len;
? p = p-next;
}
return len;
}
int main()
{
LinkList L = NULL;
CreateList(L);
printf("%d\n", getLength(L));
return 0;
}
gcc編譯通過,望采納~
如果不是循環(huán)鏈表要做以下規(guī)定的:
表頭的prev指針必須初始化為NULL
表尾的next指針必須初始化為NULL
于是計算鏈表長度可以從表頭開始迭代,每次迭代計數(shù)加一,當(dāng)遇到next為NULL時結(jié)束迭代,結(jié)束之后鏈表的長度就計算出來了。
新建節(jié)點時要養(yǎng)成習(xí)慣,prev和next未鏈接的要賦值為NULL
int Length(PLNode head)/*求長度*/
{
int n=0;
PLNode p;
p=head-next;
while(p)
{
n++;
p=p-next;
}
從頭節(jié)點開始,遍歷鏈表,如果未到鏈表末尾,則長度加一,如此反復(fù),知道鏈表結(jié)尾
#include?stdio.h
#include?conio.h
#include?stdlib.h
#define?elemType?long?/*元素類型*/
#define?elemPrintType?"%ld\t"?/*元素打印類型*/
#define?status?int
#define?OVERFLOW?-1
#define?ERROR?0
#define?OK?1
/*?單鏈表數(shù)據(jù)結(jié)構(gòu)?*/
typedef?struct?lNode?{
elemType?data;
struct?lNode?*next;
}?lNode,?*linkList;
/********************************?以下為函數(shù)聲明?********************************/
void?initList?(linkList?*L); /*?初始化?*/
void?destroyList?(linkList?L); /*?銷毀?*/
status?listIsEmpty?(linkList?L); /*?判斷單鏈表是否為空?*/
int?listLength?(linkList?L); /*?獲取單鏈表長度?*/
status?listInsertNode?(linkList?L,?int?i,?elemType?e); /*?單鏈表指定位置插入新元素?*/
status?listPrint?(linkList?L); /*?輸出鏈表?*/
status?listReverse?(linkList?L); /*?逆置鏈表?*/
/********************************?以上為函數(shù)聲明?********************************/
/*?初始化?*/
/*?操作結(jié)果:構(gòu)造一個空的單鏈表L?*/
void?initList?(linkList?*L)?{
*L?=?(linkList)?malloc?(sizeof?(struct?lNode));?/*?產(chǎn)生頭節(jié)點,并使L指向此頭節(jié)點?*/
if(!*L)?/*?內(nèi)存分配失敗?*/
exit?(OVERFLOW);
(*L)-next?=?NULL;?/*?指針域為空?*/
}
/*?銷毀?*/
/*?初始條件:單鏈表L已存在。操作結(jié)果:銷毀單鏈表L?*/
void?destroyList?(linkList?L)?{
linkList?p,q;
p?=?L-next;?/*?p指向第一個結(jié)點?*/
while?(p)?{?/*?沒到表尾?*/
q?=?p-next;
free?(p);
p?=?q;
}
free?(L);
}
/*?判斷單鏈表是否為空?*/
/*?初始條件:單鏈表L已存在。操作結(jié)果:若L為空表,則返回TRUE,否則返回FALSE?*/
status?listIsEmpty?(linkList?L)?{
return?L-next?==?NULL;
}
/*?獲取單鏈表長度?*/
/*?初始條件:單鏈表L已存在。操作結(jié)果:返回L中數(shù)據(jù)元素個數(shù)?*/
int?listLength?(linkList?L)?{
int?i?=?0;
linkList?p?=?L-next;?/*?p指向第一個結(jié)點?*/
while?(p)?{?/*?沒到表尾?*/
i++;
p=p-next;
}
return?i;
}
/*?單鏈表指定位置插入新元素?*/
/*?操作結(jié)果:在帶頭結(jié)點的單鏈表L中第i個位置之前插入元素e?*/
status?listInsertNode?(linkList?L,?int?i,?elemType?e)?{
int?j=0;
linkList?p=L,s;
while?(p??ji-1)?{?/*?尋找第i-1個結(jié)點?*/
p?=?p-next;
j++;
}
if?(!p?||?ji-1)?/*?插入位置不合理:i小于1或者大于表長?*/
return?ERROR;
/*?生成新結(jié)點,并插入L中?*/
s?=?(linkList)?malloc?(sizeof?(struct?lNode));
s-data?=?e;
s-next?=?p-next;
p-next?=?s;
return?OK;
}
/*?輸出鏈表?*/
status?listPrint?(linkList?L)?{
if?(listIsEmpty?(L))?{
puts?("鏈表為空!");
return?OK;
}
linkList?p?=?L-next;?/*?p指向第一個結(jié)點?*/
while?(p!=NULL)?{
printf?(elemPrintType,p-data);
p?=?p-next;
}
putchar?('\n');
return?OK;
}
/*?逆置鏈表?*/
/*?初始條件:單鏈表L已存在。操作結(jié)果:鏈表元素逆置?*/
status?listReverse?(linkList?L)?{
linkList?p,?q;
if?(listIsEmpty?(L)||listLength?(L)==1)?/*?若L為空表或只有一個元素?*/?
return?ERROR;??
p?=?L-next-next;?/*?p指向第2個結(jié)點?*/
L-next-next?=?NULL;?/*?首結(jié)點置為尾結(jié)點?*/
/*?自第2個結(jié)點起遍歷鏈表,循環(huán)將當(dāng)前結(jié)點插入到頭結(jié)點之后以逆置鏈表?*/
while?(p)?{
q?=?p-next;?/*?q指向p的后繼結(jié)點?*/?
/*?將p插入到頭結(jié)點之后?*/
p-next?=?L-next;
L-next=p;
/*?訪問下一結(jié)點?*/
p?=?q;
}
return?OK;
}
int?main?(void)?{
linkList?head;
elemType?a=1,b=2,c=3,d=4;
/*?初始化鏈表?*/
initList?(head);
/*?插入若干元素?*/
listInsertNode?(head,?1,?a);
listInsertNode?(head,?1,?b);
listInsertNode?(head,?1,?c);
listInsertNode?(head,?1,?d);
puts?("原鏈表內(nèi)容:");
listPrint?(head);?/*?逆置鏈表?*/
listReverse?(head);?/*?逆置鏈表?*/
puts?("逆置后鏈表內(nèi)容:");
listPrint?(head);
destroyList?(head);?/*?銷毀?*/
getch?();
return?0?;
}
運行結(jié)果
struct node {
int data;
struct node *next;
} ;
創(chuàng)建單鏈表后,最后一個結(jié)點的next是NULL,據(jù)此來遍歷表,獲得長度。
void get_len( struct node *head )
{
struct node *p=head-next ;
int len=0;
while ( p )
{
len++;
p=p-next;
}
head-data=len ; //存儲長度到頭結(jié)點
}
當(dāng)前名稱:go語言獲取鏈表長度 go實現(xiàn)單鏈表
網(wǎng)站鏈接:http://jinyejixie.com/article46/doschhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、用戶體驗、網(wǎng)站制作、標(biāo)簽優(yōu)化、微信公眾號、動態(tài)網(wǎng)站
聲明:本網(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)