diff --git a/homework2/LinkList.h b/homework2/LinkList.h index 7ebec96..b957b01 100644 --- a/homework2/LinkList.h +++ b/homework2/LinkList.h @@ -12,7 +12,7 @@ typedef struct bookNode { bool isEmpty(bookList); -void insert(bookList &l); +void create(bookList &l); void display(bookList *l); diff --git a/homework2/LinkLits.cpp b/homework2/LinkLits.cpp index 1c9c3ef..1a85296 100644 --- a/homework2/LinkLits.cpp +++ b/homework2/LinkLits.cpp @@ -11,6 +11,30 @@ bool isEmpty(bookList *l) { return false; } +void create(bookList &l) { + bookNode *input = new bookNode; + input->next = NULL; + bookNode *temp = l; // 从头节点开始 + bool flag = true; + + while (flag) { + cin >> input->id >> input->name >> input->price; + + if (input->id == "0" && input->name == "0" && input->price == 0) { + flag = false; + delete input; + } else { + if (temp == l) { + l->next = input; + } else { + temp->next = input; + } + temp = input; + input = new bookNode; + input->next = NULL; + } + } +} void display(bookList *l) { if (isEmpty(l)) { diff --git a/homework2/test1.cpp b/homework2/test1.cpp index 4b858d7..b4c79c9 100644 --- a/homework2/test1.cpp +++ b/homework2/test1.cpp @@ -2,22 +2,11 @@ #include "LinkList.h" using namespace std; -void insert(bookList &l) { - bookNode *temp = l; - while (temp != NULL) { - cin >> (*temp).id >> (*temp).name >> (*temp).price; - if (!(((*temp).id == "0" && (*temp).name == "0" && (*temp).price == 0))) { - temp = (*temp).next = new bookNode; - } else { - temp = NULL; - } - } -} int main() { bookList books = new bookNode; books->next = NULL; - insert(books); + create(books); display(&books); return 0; }