From 39819cdf5e438bc7392b0cc42c4225a5bf1cd28b Mon Sep 17 00:00:00 2001 From: msksbr Date: Sat, 19 Oct 2024 00:04:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BA=AB=E9=A6=96=E5=BC=82=E5=A4=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework2/CMakeLists.txt | 5 +++-- homework2/LinkList.h | 19 +++++++++++++++++++ homework2/LinkLits.cpp | 12 ++++-------- homework2/{Test_LinkList.cpp => test1.cpp} | 12 ++++++------ 4 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 homework2/LinkList.h rename homework2/{Test_LinkList.cpp => test1.cpp} (57%) diff --git a/homework2/CMakeLists.txt b/homework2/CMakeLists.txt index 5d741ae..a367765 100644 --- a/homework2/CMakeLists.txt +++ b/homework2/CMakeLists.txt @@ -3,5 +3,6 @@ project(homework2) set(CMAKE_CXX_STANDARD 20) -add_executable(Test_LinkList LinkLits.cpp - Test_LinkList.cpp) +add_executable(test1 LinkLits.cpp + test1.cpp + LinkList.h) diff --git a/homework2/LinkList.h b/homework2/LinkList.h new file mode 100644 index 0000000..7ebec96 --- /dev/null +++ b/homework2/LinkList.h @@ -0,0 +1,19 @@ +#ifndef LINKLIST_H +#define LINKLIST_H +#include +using namespace std; + +typedef struct bookNode { + string id; + string name; + double price; + bookNode *next; +} book_node, *bookList; + +bool isEmpty(bookList); + +void insert(bookList &l); + +void display(bookList *l); + +#endif //LINKLIST_H diff --git a/homework2/LinkLits.cpp b/homework2/LinkLits.cpp index 2b55ce0..1c9c3ef 100644 --- a/homework2/LinkLits.cpp +++ b/homework2/LinkLits.cpp @@ -1,23 +1,19 @@ #include #include +#include"LinkList.h" using namespace std; -typedef struct bookNode { - string id; - string name; - double price; - bookNode *next; -} book_node, *bookList; -bool is_list_empty(bookList *l) { +bool isEmpty(bookList *l) { if ((*l)->next == NULL) { return true; } return false; } + void display(bookList *l) { - if (is_list_empty(l)) { + if (isEmpty(l)) { printf("List is empty\n"); } else { bookNode *temp = (*l)->next; diff --git a/homework2/Test_LinkList.cpp b/homework2/test1.cpp similarity index 57% rename from homework2/Test_LinkList.cpp rename to homework2/test1.cpp index 95e567b..4b858d7 100644 --- a/homework2/Test_LinkList.cpp +++ b/homework2/test1.cpp @@ -1,13 +1,13 @@ #include -#include "LinkLits.cpp" +#include "LinkList.h" using namespace std; -void insert(bookList *l) { - bookNode *temp = (*l)->next; +void insert(bookList &l) { + bookNode *temp = l; while (temp != NULL) { cin >> (*temp).id >> (*temp).name >> (*temp).price; - if (!(((*temp).id == "" && (*temp).name == "" && (*temp).price == 0))) { - temp = (*temp).next; + if (!(((*temp).id == "0" && (*temp).name == "0" && (*temp).price == 0))) { + temp = (*temp).next = new bookNode; } else { temp = NULL; } @@ -17,7 +17,7 @@ void insert(bookList *l) { int main() { bookList books = new bookNode; books->next = NULL; - insert(&books); + insert(books); display(&books); return 0; }