From 0dd0c19bd14fcfd7e6e34aa6718ffd6300a2d897 Mon Sep 17 00:00:00 2001 From: msksbr Date: Sat, 19 Oct 2024 00:53:12 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E9=A2=98=E5=86=99=E5=AE=8C?= =?UTF-8?q?=E8=BE=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework2/LinkList.h | 1 + homework2/LinkLits.cpp | 13 +++++++++++++ homework2/test1.cpp | 1 + 3 files changed, 15 insertions(+) diff --git a/homework2/LinkList.h b/homework2/LinkList.h index b957b01..1595ec4 100644 --- a/homework2/LinkList.h +++ b/homework2/LinkList.h @@ -16,4 +16,5 @@ void create(bookList &l); void display(bookList *l); +int getLength(bookList *l); #endif //LINKLIST_H diff --git a/homework2/LinkLits.cpp b/homework2/LinkLits.cpp index 1a85296..7c556d7 100644 --- a/homework2/LinkLits.cpp +++ b/homework2/LinkLits.cpp @@ -47,3 +47,16 @@ void display(bookList *l) { } } } + +int getLength(bookList *l) { + if (isEmpty(l)) { + return 0; + } + bookNode *temp = (*l)->next; + int length = 0; + while (temp != NULL) { + length++; + temp = temp->next; + } + return length; +} diff --git a/homework2/test1.cpp b/homework2/test1.cpp index b4c79c9..0437146 100644 --- a/homework2/test1.cpp +++ b/homework2/test1.cpp @@ -7,6 +7,7 @@ int main() { bookList books = new bookNode; books->next = NULL; create(books); + cout << getLength(&books) << endl; display(&books); return 0; }