From b98d65fa23ecd5afb89fe1c76d12376efa3cfdf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=A1=E5=9D=82=E6=98=B4?= Date: Thu, 10 Oct 2024 13:30:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BE=93=E5=85=A5=E4=BF=AE=E5=A5=BD=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework2/test1.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/homework2/test1.cpp b/homework2/test1.cpp index fd30240..3ddc42c 100644 --- a/homework2/test1.cpp +++ b/homework2/test1.cpp @@ -8,7 +8,7 @@ typedef struct node { string name; double price; struct node *next; -} node, book_list*; +} node, *book_list; void init_list(book_list *books) { *books = new node(); @@ -16,19 +16,31 @@ void init_list(book_list *books) { } void create_from_end(book_list *books) { - node *r = reinterpret_cast(books); + node *r = *books; node *s = new node(); do { cin >> s->id >> s->name >> s->price; if (s->id == "0" && s->name == "0" && s->price == 0) { - r->next = s; - } else { r->next = NULL; + } else { + r->next = s; } - } while (r->next != NULL); + r = r->next; + } while (r != NULL); +} + +void display(book_list *books) { + while ((*books)->next != NULL) { + *books = (*books)->next; + cout << (*books)->id << " " << (*books)->name << " " << fixed << setprecision(2) << (*books)->price << endl; + } } int main() { + book_list *books; + init_list(books); + create_from_end(books); + display(books); return 0; }