From 7e63e4a1c9e44577daf4d3c443511dda49f37d81 Mon Sep 17 00:00:00 2001 From: ViolentAyang <76544389+ViolentAyang@users.noreply.github.com> Date: Wed, 9 Mar 2022 11:35:36 +0800 Subject: [PATCH] =?UTF-8?q?Create=20=E6=B1=82=E4=B8=8D=E5=B8=A6=E5=A4=B4?= =?UTF-8?q?=E7=BB=93=E7=82=B9=E7=9A=84=E5=8D=95=E9=93=BE=E8=A1=A8=E9=95=BF?= =?UTF-8?q?=E5=BA=A6.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 链表/求不带头结点的单链表长度.c | 67 +++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 链表/求不带头结点的单链表长度.c diff --git a/链表/求不带头结点的单链表长度.c b/链表/求不带头结点的单链表长度.c new file mode 100644 index 0000000..d678efa --- /dev/null +++ b/链表/求不带头结点的单链表长度.c @@ -0,0 +1,67 @@ +#include +#include +#include + +typedef struct LNode{ + int data; + struct LNode *next; +}LNode,*LinkList; + +bool InitList(LinkList *L){ + (*L) = NULL; + return true; +} +void PrintList(LinkList L){ + LinkList p = L; + while(p){ + printf("%d\n",p->data); + p = p->next; + } +} +int Length(LinkList L){ + LNode *p = L; + int j = 0; + while(p){ + p = p->next; + j++; + } + return j; +} +bool InsertList(LinkList *L,int i,int e){ + if(i<1){ + return false; + } + if(i==1){ + LNode *s = (LNode*)malloc(sizeof(LNode)); + s->data = e; + s->next = *L; + *L = s; + return true; + } + LNode *p; + int j = 1; + p = *L; + while(p&&jnext; + j++; + } + if(p==NULL){ + return false; + } + LNode *s = (LNode*)malloc(sizeof(LNode)); + s->data = e; + s->next = p->next; + p->next = s; + return true; +} +int main(){ + LinkList L; + InitList(&L); + for(int i = 0;i < 10;i ++){ + InsertList(&L,i+1,i); + } + PrintList(L); + printf("----测试----\n"); + printf("该链表的长度为%d",Length(L)); + return 0; +}