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; }