diff --git a/.gitignore b/.gitignore index fa9a9ea..d5d1976 100644 --- a/.gitignore +++ b/.gitignore @@ -1,43 +1,43 @@ -# Prerequisites -*.d - -# Compiled Object files -*.slo -*.lo -*.o -*.obj - -# Precompiled Headers -*.gch -*.pch - -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - -# Fortran module files -*.mod -*.smod - -# Compiled Static libraries -*.lai -*.la -*.a -*.lib - -# Executables -*.exe -*.out -*.app -# .vscode -.vscode/ -*/.vscode/ - -# Clion and jetbrains -.idea/ -*/.idea - -# cmake log -cmake*/ -*/cmake*/ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app +# .vscode +.vscode/ +*/.vscode/ + +# Clion and jetbrains +.idea/ +*/.idea + +# cmake log +cmake*/ +*/cmake*/ diff --git a/LICENSE b/LICENSE index cd4b02e..b186090 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,21 @@ -MIT License - -Copyright (c) 2024 御坂昴 - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +MIT License + +Copyright (c) 2024 御坂昴 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/homework1/CMakeLists.txt b/homework1/CMakeLists.txt index 2aa2370..10a6d0a 100644 --- a/homework1/CMakeLists.txt +++ b/homework1/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 3.29) -project(homework1) - -set(CMAKE_CXX_STANDARD 20) - -add_executable(test1 - test1.cpp) -add_executable(test2 test2.cpp) -add_executable(test3 test3.cpp) -add_executable(test4 test4.cpp) +cmake_minimum_required(VERSION 3.29) +project(homework1) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(test1 + test1.cpp) +add_executable(test2 test2.cpp) +add_executable(test3 test3.cpp) +add_executable(test4 test4.cpp) diff --git a/homework1/test1.cpp b/homework1/test1.cpp index f41f3ae..da2d632 100644 --- a/homework1/test1.cpp +++ b/homework1/test1.cpp @@ -1,34 +1,34 @@ -#include -#include - -using namespace std; - -typedef struct book { - string id; - string name; - double price; -} book; - -int input(book *books) { - int length = 0; - for (int length = 0; true; length++) { - cin >> books[length].id >> books[length].name >> books[length].price; - if (books[length].id == "0" && books[length].name == "0" && books[length].price == 0) { - return length; - } - } -} - -void output(book *books, int length) { - for (int i = 0; i < length; i++) { - cout << books[i].id << " " << books[i].name << " " << fixed << setprecision(2) << books[i].price << endl; - } -} - -int main() { - book books[100]; - int length = input(books); - cout << length << endl; - output(books, length); - return 0; -} +#include +#include + +using namespace std; + +typedef struct book { + string id; + string name; + double price; +} book; + +int input(book *books) { + int length = 0; + for (int length = 0; true; length++) { + cin >> books[length].id >> books[length].name >> books[length].price; + if (books[length].id == "0" && books[length].name == "0" && books[length].price == 0) { + return length; + } + } +} + +void output(book *books, int length) { + for (int i = 0; i < length; i++) { + cout << books[i].id << " " << books[i].name << " " << fixed << setprecision(2) << books[i].price << endl; + } +} + +int main() { + book books[100]; + int length = input(books); + cout << length << endl; + output(books, length); + return 0; +} diff --git a/homework1/test2.cpp b/homework1/test2.cpp index c56cd33..2efd216 100644 --- a/homework1/test2.cpp +++ b/homework1/test2.cpp @@ -1,50 +1,50 @@ -#include -#include - -using namespace std; - -typedef struct { - string id; - string name; - double price; -} book; - -void swap(book &a, book &b) { - book temp = a; - a = b; - b = temp; -} - -void sort(book *arr, int length) { - for (int i = 0; i < length - 1; i++) { - for (int j = 0; j < length - 1 - i; j++) { - if (arr[j].price < arr[j + 1].price) { - swap(arr[j], arr[j + 1]); - } - } - } -} - -int input(book *books) { - int length = 0; - for (int length = 0; true; length++) { - cin >> books[length].id >> books[length].name >> books[length].price; - if (books[length].id == "0" && books[length].name == "0" && books[length].price == 0) { - return length; - } - } -} - -void output(book *books, int length) { - for (int i = 0; i < length; i++) { - cout << books[i].id << " " << books[i].name << " " << fixed << setprecision(2) << books[i].price << endl; - } -} - -int main() { - book books[100]; - int length = input(books); - sort(books, length); - output(books, length); - return 0; -} +#include +#include + +using namespace std; + +typedef struct { + string id; + string name; + double price; +} book; + +void swap(book &a, book &b) { + book temp = a; + a = b; + b = temp; +} + +void sort(book *arr, int length) { + for (int i = 0; i < length - 1; i++) { + for (int j = 0; j < length - 1 - i; j++) { + if (arr[j].price < arr[j + 1].price) { + swap(arr[j], arr[j + 1]); + } + } + } +} + +int input(book *books) { + int length = 0; + for (int length = 0; true; length++) { + cin >> books[length].id >> books[length].name >> books[length].price; + if (books[length].id == "0" && books[length].name == "0" && books[length].price == 0) { + return length; + } + } +} + +void output(book *books, int length) { + for (int i = 0; i < length; i++) { + cout << books[i].id << " " << books[i].name << " " << fixed << setprecision(2) << books[i].price << endl; + } +} + +int main() { + book books[100]; + int length = input(books); + sort(books, length); + output(books, length); + return 0; +} diff --git a/homework1/test3.cpp b/homework1/test3.cpp index 2effad1..2e243db 100644 --- a/homework1/test3.cpp +++ b/homework1/test3.cpp @@ -1,40 +1,40 @@ -#include -#include - -using namespace std; - -typedef struct { - string id; - string name; - double price; -} book; - -void alter_to_110_percent(book *books, int length) { - for (int i = 0; i < length; i++) { - books[i].price = books[i].price * 1.1; - } -} - -int input(book *books) { - int length = 0; - for (int length = 0; true; length++) { - cin >> books[length].id >> books[length].name >> books[length].price; - if (books[length].id == "0" && books[length].name == "0" && books[length].price == 0) { - return length; - } - } -} - -void output(book *books, int length) { - for (int i = 0; i < length; i++) { - cout << books[i].id << " " << books[i].name << " " << fixed << setprecision(2) << books[i].price << endl; - } -} - -int main() { - book books[100]; - int length = input(books); - alter_to_110_percent(books, length); - output(books, length); - return 0; -} +#include +#include + +using namespace std; + +typedef struct { + string id; + string name; + double price; +} book; + +void alter_to_110_percent(book *books, int length) { + for (int i = 0; i < length; i++) { + books[i].price = books[i].price * 1.1; + } +} + +int input(book *books) { + int length = 0; + for (int length = 0; true; length++) { + cin >> books[length].id >> books[length].name >> books[length].price; + if (books[length].id == "0" && books[length].name == "0" && books[length].price == 0) { + return length; + } + } +} + +void output(book *books, int length) { + for (int i = 0; i < length; i++) { + cout << books[i].id << " " << books[i].name << " " << fixed << setprecision(2) << books[i].price << endl; + } +} + +int main() { + book books[100]; + int length = input(books); + alter_to_110_percent(books, length); + output(books, length); + return 0; +} diff --git a/homework1/test4.cpp b/homework1/test4.cpp index 94bc83e..283d0b2 100644 --- a/homework1/test4.cpp +++ b/homework1/test4.cpp @@ -1,72 +1,72 @@ -#include -#include - -using namespace std; - -typedef struct { - string id; - string name; - double price; -} book; - -typedef struct { - book *books; - int number_of_books; -} book_and_number; - -double find_max_price(book *books, int n) { - double max_price = 0; - for (int i = 0; i < n; i++) { - if (books[i].price > max_price) { - max_price = books[i].price; - } - } - return max_price; -} - -book_and_number max_price_books(book *books, int n) { - double max_price = find_max_price(books, n); - book_and_number books_and_number; - books_and_number.books = new book[100]; - books_and_number.number_of_books = 0; - - for (int i = 0; i < n; i++) { - if (books[i].price == max_price) { - books_and_number.books[i].id = books[i].id; - books_and_number.books[i].name = books[i].name; - books_and_number.books[i].price = books[i].price; - books_and_number.number_of_books++; - } - } - return books_and_number; -} - -void input(book *books, int n) { - for (int i = 0; i < n; i++) { - cin >> books[i].id >> books[i].name >> books[i].price; - } -} - -void output(book_and_number *books_and_number) { - cout << books_and_number->number_of_books << endl; - for (int i = 0; true; i++) { - if (books_and_number->books[i].price != 0) { - for (int j = 1; j < i + books_and_number->number_of_books; j++) { - cout << books_and_number->books[i].id << " " << books_and_number->books[i].name << " " << fixed << - setprecision(2) << books_and_number->books[i].price << endl; - } - break; - } - } -} - -int main() { - book books[100]; - int n; - cin >> n; - input(books, n); - find_max_price(books, n); - book_and_number books_and_number = max_price_books(books, n); - output(&books_and_number); - return 0; -} +#include +#include + +using namespace std; + +typedef struct { + string id; + string name; + double price; +} book; + +typedef struct { + book *books; + int number_of_books; +} book_and_number; + +double find_max_price(book *books, int n) { + double max_price = 0; + for (int i = 0; i < n; i++) { + if (books[i].price > max_price) { + max_price = books[i].price; + } + } + return max_price; +} + +book_and_number max_price_books(book *books, int n) { + double max_price = find_max_price(books, n); + book_and_number books_and_number; + books_and_number.books = new book[100]; + books_and_number.number_of_books = 0; + + for (int i = 0; i < n; i++) { + if (books[i].price == max_price) { + books_and_number.books[i].id = books[i].id; + books_and_number.books[i].name = books[i].name; + books_and_number.books[i].price = books[i].price; + books_and_number.number_of_books++; + } + } + return books_and_number; +} + +void input(book *books, int n) { + for (int i = 0; i < n; i++) { + cin >> books[i].id >> books[i].name >> books[i].price; + } +} + +void output(book_and_number *books_and_number) { + cout << books_and_number->number_of_books << endl; + for (int i = 0; true; i++) { + if (books_and_number->books[i].price != 0) { + for (int j = 1; j < i + books_and_number->number_of_books; j++) { + cout << books_and_number->books[i].id << " " << books_and_number->books[i].name << " " << fixed << + setprecision(2) << books_and_number->books[i].price << endl; + } + break; + } + } +} + +int main() { + book books[100]; + int n; + cin >> n; + input(books, n); + find_max_price(books, n); + book_and_number books_and_number = max_price_books(books, n); + output(&books_and_number); + return 0; +} diff --git a/homework2/LinkList.cpp b/homework2/LinkList.cpp index 466158e..3220ea1 100644 --- a/homework2/LinkList.cpp +++ b/homework2/LinkList.cpp @@ -1,63 +1,63 @@ -//LinkList.cpp -#include -#include -#include"LinkList.h" -using namespace std; - - -bool isEmpty(bookList *l) { - if ((*l)->next == NULL) { - return true; - } - 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)) { - printf("List is empty\n"); - } else { - bookNode *temp = (*l)->next; - while (temp != NULL) { - cout << temp->id << " " << temp->name << " " << fixed << setprecision(2) << temp->price << endl; - temp = temp->next; - } - } -} - -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; -} +//LinkList.cpp +#include +#include +#include"LinkList.h" +using namespace std; + + +bool isEmpty(bookList *l) { + if ((*l)->next == NULL) { + return true; + } + 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)) { + printf("List is empty\n"); + } else { + bookNode *temp = (*l)->next; + while (temp != NULL) { + cout << temp->id << " " << temp->name << " " << fixed << setprecision(2) << temp->price << endl; + temp = temp->next; + } + } +} + +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/LinkList.h b/homework2/LinkList.h index e0ab7a1..f2d0c8a 100644 --- a/homework2/LinkList.h +++ b/homework2/LinkList.h @@ -1,21 +1,21 @@ -//LinkList.h -#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 create(bookList &l); - -void display(bookList *l); - -int getLength(bookList *l); -#endif //LINKLIST_H +//LinkList.h +#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 create(bookList &l); + +void display(bookList *l); + +int getLength(bookList *l); +#endif //LINKLIST_H diff --git a/homework2/test1.cpp b/homework2/test1.cpp index 5e819e5..1e0a158 100644 --- a/homework2/test1.cpp +++ b/homework2/test1.cpp @@ -1,14 +1,14 @@ -//test1.cpp -#include -#include "LinkList.h" -using namespace std; - - -int main() { - bookList books = new bookNode; - books->next = NULL; - create(books); - cout << getLength(&books) << endl; - display(&books); - return 0; -} +//test1.cpp +#include +#include "LinkList.h" +using namespace std; + + +int main() { + bookList books = new bookNode; + books->next = NULL; + create(books); + cout << getLength(&books) << endl; + display(&books); + return 0; +} diff --git a/homework2/test2.cpp b/homework2/test2.cpp index dada520..9946109 100644 --- a/homework2/test2.cpp +++ b/homework2/test2.cpp @@ -1,47 +1,47 @@ -//test2.cpp -#include -#include"LinkList.h" //LinkList类见第一题 -using namespace std; - -void swapNode(bookNode &n1, bookNode &n2) { - string tempId = n1.id; - n1.id = n2.id; - n2.id = tempId; - - string tempName = n1.name; - n1.name = n2.name; - n2.name = tempName; - - double tempPrice = n1.price; - n1.price = n2.price; - n2.price = tempPrice; -} - -void bubble_sort(bookList &l) { - int n = getLength(&l); - bool swapped; - for (int i = 0; i < n - 1; i++) { - swapped = false; - bookNode *temp = l->next; - for (int j = 0; j < n - i - 1; j++) { - bookNode *temp1 = temp->next; - if (temp != NULL && temp1 != NULL && temp->price < temp1->price) { - swapNode(*temp, *temp1); - swapped = true; - } - temp = temp1; - } - if (!swapped) { - break; - } - } -} - -int main() { - bookList books = new bookNode; - books->next = NULL; - create(books); - bubble_sort(books); - display(&books); - return 0; -} +//test2.cpp +#include +#include"LinkList.h" //LinkList类见第一题 +using namespace std; + +void swapNode(bookNode &n1, bookNode &n2) { + string tempId = n1.id; + n1.id = n2.id; + n2.id = tempId; + + string tempName = n1.name; + n1.name = n2.name; + n2.name = tempName; + + double tempPrice = n1.price; + n1.price = n2.price; + n2.price = tempPrice; +} + +void bubble_sort(bookList &l) { + int n = getLength(&l); + bool swapped; + for (int i = 0; i < n - 1; i++) { + swapped = false; + bookNode *temp = l->next; + for (int j = 0; j < n - i - 1; j++) { + bookNode *temp1 = temp->next; + if (temp != NULL && temp1 != NULL && temp->price < temp1->price) { + swapNode(*temp, *temp1); + swapped = true; + } + temp = temp1; + } + if (!swapped) { + break; + } + } +} + +int main() { + bookList books = new bookNode; + books->next = NULL; + create(books); + bubble_sort(books); + display(&books); + return 0; +} diff --git a/homework2/test3.cpp b/homework2/test3.cpp index 357f3f3..a35bd6a 100644 --- a/homework2/test3.cpp +++ b/homework2/test3.cpp @@ -1,21 +1,21 @@ -//test3.cpp -#include -#include"LinkList.h" //LinkList类见实验一 -using namespace std; - -void alter_to_110_percent(bookList &l) { - bookNode *temp = l->next; - while (temp != NULL) { - temp->price *= 1.1; - temp = temp->next; - } -} - -int main() { - bookList books = new bookNode; - books->next = NULL; - create(books); - alter_to_110_percent(books); - display(&books); - return 0; -} +//test3.cpp +#include +#include"LinkList.h" //LinkList类见实验一 +using namespace std; + +void alter_to_110_percent(bookList &l) { + bookNode *temp = l->next; + while (temp != NULL) { + temp->price *= 1.1; + temp = temp->next; + } +} + +int main() { + bookList books = new bookNode; + books->next = NULL; + create(books); + alter_to_110_percent(books); + display(&books); + return 0; +} diff --git a/homework2/test4.cpp b/homework2/test4.cpp index 9c15836..b3c1062 100644 --- a/homework2/test4.cpp +++ b/homework2/test4.cpp @@ -1,57 +1,57 @@ -//test4.cpp -#include -#include -#include"LinkList.h" //LinkList类见实验一 -using namespace std; - -void create_with_count(bookList &l, int count) { - bookNode *input = new bookNode; - input->next = NULL; - bookNode *temp = l; - for (int i = 0; i < count; i++) { - cin >> input->id >> input->name >> input->price; - if (temp == l) { - l->next = input; - } else { - temp->next = input; - } - temp = input; - input = new bookNode; - input->next = NULL; - } -} - -double find_max_price(bookList *l) { - bookNode *temp = (*l)->next; - double max = 0; - while (temp != NULL) { - if (temp->price > max) { - max = temp->price; - } - temp = temp->next; - } - return max; -} - -void display_max_prices(bookList *l, double max_price) { - bookNode *temp = *l; - while (temp != NULL) { - if (temp->price == max_price) { - cout << temp->id << " " << temp->name << " " << fixed << setprecision(2) << temp->price << endl; - } - temp = temp->next; - } -} - -int main() { - int count; - bookList books = new bookNode; - double max_price; - books->next = NULL; - cin >> count; - create_with_count(books, count); - max_price = find_max_price(&books); - cout << max_price << endl; - display_max_prices(&books, max_price); - return 0; -} +//test4.cpp +#include +#include +#include"LinkList.h" //LinkList类见实验一 +using namespace std; + +void create_with_count(bookList &l, int count) { + bookNode *input = new bookNode; + input->next = NULL; + bookNode *temp = l; + for (int i = 0; i < count; i++) { + cin >> input->id >> input->name >> input->price; + if (temp == l) { + l->next = input; + } else { + temp->next = input; + } + temp = input; + input = new bookNode; + input->next = NULL; + } +} + +double find_max_price(bookList *l) { + bookNode *temp = (*l)->next; + double max = 0; + while (temp != NULL) { + if (temp->price > max) { + max = temp->price; + } + temp = temp->next; + } + return max; +} + +void display_max_prices(bookList *l, double max_price) { + bookNode *temp = *l; + while (temp != NULL) { + if (temp->price == max_price) { + cout << temp->id << " " << temp->name << " " << fixed << setprecision(2) << temp->price << endl; + } + temp = temp->next; + } +} + +int main() { + int count; + bookList books = new bookNode; + double max_price; + books->next = NULL; + cin >> count; + create_with_count(books, count); + max_price = find_max_price(&books); + cout << max_price << endl; + display_max_prices(&books, max_price); + return 0; +} diff --git a/homework4/BiTree.h b/homework4/BiTree.h index 3abc165..1a0ed42 100644 --- a/homework4/BiTree.h +++ b/homework4/BiTree.h @@ -1,176 +1,176 @@ -#ifndef BITREE_H -#define BITREE_H - -#include -#include -#include -#include - -template -class BiTree -{ -public: - T data; - BiTree* left; - BiTree* right; - - BiTree() : left(nullptr), right(nullptr) - { - } - - BiTree(char val): data(val), left(nullptr), right(nullptr) - { - } - - static bool is_empty(BiTree* bt) - { - if (bt == nullptr) - { - return true; - } - return false; - } - - static bool is_leaf(BiTree* bt) - { - if (bt->left == nullptr && bt->right == nullptr) - { - return true; - } - return false; - } - - static T sum_leaf(BiTree* bt) - { - if (is_empty(bt)) - { - return 0; - } - if (is_leaf(bt)) - { - return bt->data; - } - return sum_leaf(bt->left) + sum_leaf(bt->right); - } - - static BiTree* createNode(const T& value) - { - BiTree* node = new BiTree(); - node->data = value; - node->left = nullptr; - node->right = nullptr; - return node; - } - - static void insertNode(BiTree*& bt, T value) - { - if (bt == nullptr) - { - bt = createNode(value); - } - else - { - if (value < bt->data) - { - insertNode(bt->left, value); - } - if (value > bt->data) - { - insertNode(bt->right, value); - } - } - } - - static BiTree* buildFromPreorderString(const std::string& preorder, size_t& index) - { - if (index >= preorder.size() || preorder[index] == '#') - { - index++; - return nullptr; - } - BiTree* node = createNode(preorder[index++]); - node->left = buildFromPreorderString(preorder, index); - node->right = buildFromPreorderString(preorder, index); - return node; - } - - void inorderTraversal(std::ostringstream& oss) const - { - if (left) left->inorderTraversal(oss); - oss << data; - if (right) right->inorderTraversal(oss); - } - - static BiTree* buildFromPreorderInorderString(const std::string& preorder, const std::string& inorder, int preStart, - int preEnd, - int inStart, int inEnd, std::unordered_map& inMap) - { - if (preStart > preEnd || inStart > inEnd) - { - return nullptr; - } - - char rootVal = preorder[preStart]; - BiTree* root = new BiTree(rootVal); - - int inRoot = inMap[rootVal]; - int leftSubtreeSize = inRoot - inStart; - - root->left = buildFromPreorderInorderString(preorder, inorder, preStart + 1, preStart + leftSubtreeSize, - inStart, inRoot - 1, inMap); - root->right = buildFromPreorderInorderString(preorder, inorder, preStart + leftSubtreeSize + 1, preEnd, - inRoot + 1, inEnd, inMap); - - - return root; - } - - static int getHeight(BiTree* root) - { - if (root == nullptr) - { - return 0; - } - int leftHeight = getHeight(root->left); - int rightHeight = getHeight(root->right); - return 1 + std::max(leftHeight, rightHeight); - } - - void toStringHelper(std::ostringstream& oss, const std::string& prefix, bool isLeft, bool hasSibling) const - { - if (this != nullptr) - { - oss << prefix; - oss << (isLeft ? (hasSibling ? "├─" : "└─") : (hasSibling ? "├─" : "└─")) << data << "\n"; - - std::string newPrefix = prefix + (isLeft ? (hasSibling ? "│ " : " ") : " "); - bool hasLeftSibling = (left != nullptr && right != nullptr); - - if (left) left->toStringHelper(oss, newPrefix, true, hasLeftSibling); - if (right) right->toStringHelper(oss, newPrefix, false, false); - } - } - - std::string toString() const - { - std::ostringstream oss; - this->toStringHelper(oss, "", false, false); - return oss.str(); - } - - void destroy() - { - if (left) - { - left->destroy(); - delete left; - } - if (right) - { - right->destroy(); - delete right; - } - } -}; - -#endif // BITREE_H +#ifndef BITREE_H +#define BITREE_H + +#include +#include +#include +#include + +template +class BiTree +{ +public: + T data; + BiTree* left; + BiTree* right; + + BiTree() : left(nullptr), right(nullptr) + { + } + + BiTree(char val): data(val), left(nullptr), right(nullptr) + { + } + + static bool is_empty(BiTree* bt) + { + if (bt == nullptr) + { + return true; + } + return false; + } + + static bool is_leaf(BiTree* bt) + { + if (bt->left == nullptr && bt->right == nullptr) + { + return true; + } + return false; + } + + static T sum_leaf(BiTree* bt) + { + if (is_empty(bt)) + { + return 0; + } + if (is_leaf(bt)) + { + return bt->data; + } + return sum_leaf(bt->left) + sum_leaf(bt->right); + } + + static BiTree* createNode(const T& value) + { + BiTree* node = new BiTree(); + node->data = value; + node->left = nullptr; + node->right = nullptr; + return node; + } + + static void insertNode(BiTree*& bt, T value) + { + if (bt == nullptr) + { + bt = createNode(value); + } + else + { + if (value < bt->data) + { + insertNode(bt->left, value); + } + if (value > bt->data) + { + insertNode(bt->right, value); + } + } + } + + static BiTree* buildFromPreorderString(const std::string& preorder, size_t& index) + { + if (index >= preorder.size() || preorder[index] == '#') + { + index++; + return nullptr; + } + BiTree* node = createNode(preorder[index++]); + node->left = buildFromPreorderString(preorder, index); + node->right = buildFromPreorderString(preorder, index); + return node; + } + + void inorderTraversal(std::ostringstream& oss) const + { + if (left) left->inorderTraversal(oss); + oss << data; + if (right) right->inorderTraversal(oss); + } + + static BiTree* buildFromPreorderInorderString(const std::string& preorder, const std::string& inorder, int preStart, + int preEnd, + int inStart, int inEnd, std::unordered_map& inMap) + { + if (preStart > preEnd || inStart > inEnd) + { + return nullptr; + } + + char rootVal = preorder[preStart]; + BiTree* root = new BiTree(rootVal); + + int inRoot = inMap[rootVal]; + int leftSubtreeSize = inRoot - inStart; + + root->left = buildFromPreorderInorderString(preorder, inorder, preStart + 1, preStart + leftSubtreeSize, + inStart, inRoot - 1, inMap); + root->right = buildFromPreorderInorderString(preorder, inorder, preStart + leftSubtreeSize + 1, preEnd, + inRoot + 1, inEnd, inMap); + + + return root; + } + + static int getHeight(BiTree* root) + { + if (root == nullptr) + { + return 0; + } + int leftHeight = getHeight(root->left); + int rightHeight = getHeight(root->right); + return 1 + std::max(leftHeight, rightHeight); + } + + void toStringHelper(std::ostringstream& oss, const std::string& prefix, bool isLeft, bool hasSibling) const + { + if (this != nullptr) + { + oss << prefix; + oss << (isLeft ? (hasSibling ? "├─" : "└─") : (hasSibling ? "├─" : "└─")) << data << "\n"; + + std::string newPrefix = prefix + (isLeft ? (hasSibling ? "│ " : " ") : " "); + bool hasLeftSibling = (left != nullptr && right != nullptr); + + if (left) left->toStringHelper(oss, newPrefix, true, hasLeftSibling); + if (right) right->toStringHelper(oss, newPrefix, false, false); + } + } + + std::string toString() const + { + std::ostringstream oss; + this->toStringHelper(oss, "", false, false); + return oss.str(); + } + + void destroy() + { + if (left) + { + left->destroy(); + delete left; + } + if (right) + { + right->destroy(); + delete right; + } + } +}; + +#endif // BITREE_H diff --git a/homework4/CMakeLists.txt b/homework4/CMakeLists.txt index 10bca46..822f0c4 100644 --- a/homework4/CMakeLists.txt +++ b/homework4/CMakeLists.txt @@ -1,8 +1,8 @@ -cmake_minimum_required(VERSION 3.29) -project(homework4) - -set(CMAKE_CXX_STANDARD 20) - -add_executable(test1 test1.cpp BiTree.h) -add_executable(test2 test2.cpp BiTree.h) -add_executable(test3 test3.cpp BiTree.h) +cmake_minimum_required(VERSION 3.29) +project(homework4) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(test1 test1.cpp BiTree.h) +add_executable(test2 test2.cpp BiTree.h) +add_executable(test3 test3.cpp BiTree.h) diff --git a/homework4/test1.cpp b/homework4/test1.cpp index 0dd56aa..0c80052 100644 --- a/homework4/test1.cpp +++ b/homework4/test1.cpp @@ -1,42 +1,42 @@ -/* -* 二叉树采用二叉链存储结构存放,结点值为int 类型,设计一个递归算法求二叉树 bt 中所有叶子结点值之和 - */ -#include -#include "BiTree.h" - -using namespace std; - -BiTree* createTreeFromArray(const int* values, int size) -{ - BiTree* root = nullptr; - for (int i = 0; i < size; ++i) - { - BiTree::insertNode(root, values[i]); - } - return root; -} - -int main() -{ - int testValues[] = {7, 3, 10, 1, 5, 9, 12}; - int size = sizeof(testValues) / sizeof(testValues[0]); - - BiTree* root = createTreeFromArray(testValues, size); - - if (root) - { - cout << "树结构为:" << endl; - cout << root->toString() << endl; - - cout << "叶子节点数据总和: " << BiTree::sum_leaf(root) << endl; - - root->destroy(); - delete root; - } - else - { - cout << "树为空。" << endl; - } - - return 0; -} +/* +* 二叉树采用二叉链存储结构存放,结点值为int 类型,设计一个递归算法求二叉树 bt 中所有叶子结点值之和 + */ +#include +#include "BiTree.h" + +using namespace std; + +BiTree* createTreeFromArray(const int* values, int size) +{ + BiTree* root = nullptr; + for (int i = 0; i < size; ++i) + { + BiTree::insertNode(root, values[i]); + } + return root; +} + +int main() +{ + int testValues[] = {7, 3, 10, 1, 5, 9, 12}; + int size = sizeof(testValues) / sizeof(testValues[0]); + + BiTree* root = createTreeFromArray(testValues, size); + + if (root) + { + cout << "树结构为:" << endl; + cout << root->toString() << endl; + + cout << "叶子节点数据总和: " << BiTree::sum_leaf(root) << endl; + + root->destroy(); + delete root; + } + else + { + cout << "树为空。" << endl; + } + + return 0; +} diff --git a/homework4/test2.cpp b/homework4/test2.cpp index 8cdd4be..aaa67fd 100644 --- a/homework4/test2.cpp +++ b/homework4/test2.cpp @@ -1,49 +1,49 @@ -/* - * 以字符串的形式定义一棵二叉树的先序序列,若字符是‘#’, 表示该二叉树是空树,否则该字符是相应结点的数据元素。读入相应先序序列,建立二叉链式存储结构的二叉树,然后中序遍历该二叉树并输出结点数据。 - * 输入格式: - * 字符串形式的先序序列(即结点的数据类型为单个字符) - * 输出格式: - * 中序遍历结果 - * 输入样例: - * 在这里给出一组输入。例如: - * ABC##DE#G##F### - * 输出样例: - * CBEGDFA - */ -#include -#include -#include "BiTree.h" - -using namespace std; - -int main() -{ - string input; - cout << "请输入二叉树的先序序列(如ABC##DE#G##F###):"; - cin >> input; - - size_t index = 0; - BiTree* root = BiTree::buildFromPreorderString(input, index); // 构建字符型二叉树 - - ostringstream oss; - if (root) - { - // 中序遍历 - root->inorderTraversal(oss); - cout << "中序遍历结果:" << oss.str() << endl; - - // 打印树结构 - cout << "该树结构为:" << endl; - cout << root->toString(); - - // 销毁树,释放内存 - root->destroy(); - delete root; - } - else - { - cout << "输入的二叉树为空。" << endl; - } - - return 0; -} +/* + * 以字符串的形式定义一棵二叉树的先序序列,若字符是‘#’, 表示该二叉树是空树,否则该字符是相应结点的数据元素。读入相应先序序列,建立二叉链式存储结构的二叉树,然后中序遍历该二叉树并输出结点数据。 + * 输入格式: + * 字符串形式的先序序列(即结点的数据类型为单个字符) + * 输出格式: + * 中序遍历结果 + * 输入样例: + * 在这里给出一组输入。例如: + * ABC##DE#G##F### + * 输出样例: + * CBEGDFA + */ +#include +#include +#include "BiTree.h" + +using namespace std; + +int main() +{ + string input; + cout << "请输入二叉树的先序序列(如ABC##DE#G##F###):"; + cin >> input; + + size_t index = 0; + BiTree* root = BiTree::buildFromPreorderString(input, index); // 构建字符型二叉树 + + ostringstream oss; + if (root) + { + // 中序遍历 + root->inorderTraversal(oss); + cout << "中序遍历结果:" << oss.str() << endl; + + // 打印树结构 + cout << "该树结构为:" << endl; + cout << root->toString(); + + // 销毁树,释放内存 + root->destroy(); + delete root; + } + else + { + cout << "输入的二叉树为空。" << endl; + } + + return 0; +} diff --git a/homework4/test3.cpp b/homework4/test3.cpp index 9112372..69c8942 100644 --- a/homework4/test3.cpp +++ b/homework4/test3.cpp @@ -1,41 +1,41 @@ -/* - * 给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。 - * 输入格式: - * 输入首先给出正整数N(≤50),为树中结点总数。下面两行先后给出先序和中序遍历序列,均是长度为N的不包含重复英文字母(区别大小写)的字符串。 - * 输出格式: - * 输出为一个整数,即该二叉树的高度。 - * 输入样例: - * 9 - * ABDFGHIEC - * FDHGIBEAC - * 输出样例: - * 5 - */ -#include"BiTree.h" -#include -using namespace std; - -int main() -{ - int n; - cout << "请输入节点个数:"; - cin >> n; - - string preorder, inorder; - cout << "请输入前置索引:" << endl; - cin >> preorder; - cout << "请输入后置索引:" << endl; - cin >> inorder; - - std::unordered_map inMap; - for (int i = 0; i < inorder.size(); ++i) - { - inMap[inorder[i]] = i; - } - - BiTree* root = BiTree::buildFromPreorderInorderString(preorder, inorder, 0, n - 1, 0, n - 1, inMap); - - cout << "树高为:" << BiTree::getHeight(root) << endl; - cout << "该数结构为:" << endl << root->toString() << endl; - return 0; -} +/* + * 给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。 + * 输入格式: + * 输入首先给出正整数N(≤50),为树中结点总数。下面两行先后给出先序和中序遍历序列,均是长度为N的不包含重复英文字母(区别大小写)的字符串。 + * 输出格式: + * 输出为一个整数,即该二叉树的高度。 + * 输入样例: + * 9 + * ABDFGHIEC + * FDHGIBEAC + * 输出样例: + * 5 + */ +#include"BiTree.h" +#include +using namespace std; + +int main() +{ + int n; + cout << "请输入节点个数:"; + cin >> n; + + string preorder, inorder; + cout << "请输入前置索引:" << endl; + cin >> preorder; + cout << "请输入后置索引:" << endl; + cin >> inorder; + + std::unordered_map inMap; + for (int i = 0; i < inorder.size(); ++i) + { + inMap[inorder[i]] = i; + } + + BiTree* root = BiTree::buildFromPreorderInorderString(preorder, inorder, 0, n - 1, 0, n - 1, inMap); + + cout << "树高为:" << BiTree::getHeight(root) << endl; + cout << "该数结构为:" << endl << root->toString() << endl; + return 0; +} diff --git a/homework5/CMakeLists.txt b/homework5/CMakeLists.txt index fc40a1b..f30ea50 100644 --- a/homework5/CMakeLists.txt +++ b/homework5/CMakeLists.txt @@ -1,8 +1,8 @@ -cmake_minimum_required(VERSION 3.30) -project(homework5) - -set(CMAKE_CXX_STANDARD 20) - -add_executable(test1 test1.cpp - MatrixGraph.h - GraphExceptions/InsertExistedConnectException.h) +cmake_minimum_required(VERSION 3.30) +project(homework5) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(test1 test1.cpp + MatrixGraph.h + GraphExceptions/InsertExistedConnectException.h) diff --git a/homework5/GraphExceptions/InsertExistedConnectException.h b/homework5/GraphExceptions/InsertExistedConnectException.h index 9f77ff8..bc1986f 100644 --- a/homework5/GraphExceptions/InsertExistedConnectException.h +++ b/homework5/GraphExceptions/InsertExistedConnectException.h @@ -1,26 +1,26 @@ -// 这是一个异常,当用户尝试插入已存在的连接时抛出 - -#ifndef INSERTEXISTEDCONNECTEXCEPTION_H -#define INSERTEXISTEDCONNECTEXCEPTION_H -#include -#include - - -class InsertExistedConnectException : public std::exception{ -private: - std::string message; -public: - InsertExistedConnectException():message("You input id is out of range"){} - InsertExistedConnectException(const std::string& msg) : message(msg) {} - InsertExistedConnectException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){} - - virtual ~InsertExistedConnectException() noexcept {} - virtual const char* what() const noexcept override { - return message.c_str(); - } - -}; - - - -#endif //INSERTEXISTEDCONNECTEXCEPTION_H +// 这是一个异常,当用户尝试插入已存在的连接时抛出 + +#ifndef INSERTEXISTEDCONNECTEXCEPTION_H +#define INSERTEXISTEDCONNECTEXCEPTION_H +#include +#include + + +class InsertExistedConnectException : public std::exception{ +private: + std::string message; +public: + InsertExistedConnectException():message("You input id is out of range"){} + InsertExistedConnectException(const std::string& msg) : message(msg) {} + InsertExistedConnectException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){} + + virtual ~InsertExistedConnectException() noexcept {} + virtual const char* what() const noexcept override { + return message.c_str(); + } + +}; + + + +#endif //INSERTEXISTEDCONNECTEXCEPTION_H diff --git a/homework5/GraphExceptions/NodeIdOutOfRangeException.h b/homework5/GraphExceptions/NodeIdOutOfRangeException.h index 54e18ad..22543e5 100644 --- a/homework5/GraphExceptions/NodeIdOutOfRangeException.h +++ b/homework5/GraphExceptions/NodeIdOutOfRangeException.h @@ -1,25 +1,25 @@ -// 这是一个异常,当用户给的节点id超出最大值时抛出 - -#ifndef NODEIDOUTOFRANGEEXCEPTION_H -#define NODEIDOUTOFRANGEEXCEPTION_H -#include -#include - -class NodeIdOutOfRangeException : public std::exception{ -private: - std::string message; -public: - NodeIdOutOfRangeException():message("You input id is out of range"){} - NodeIdOutOfRangeException(const std::string& msg) : message(msg) {} - NodeIdOutOfRangeException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){} - - virtual ~NodeIdOutOfRangeException() noexcept {} - virtual const char* what() const noexcept override { - return message.c_str(); - } - -}; - - - -#endif //NODEIDOUTOFRANGEEXCEPTION_H +// 这是一个异常,当用户给的节点id超出最大值时抛出 + +#ifndef NODEIDOUTOFRANGEEXCEPTION_H +#define NODEIDOUTOFRANGEEXCEPTION_H +#include +#include + +class NodeIdOutOfRangeException : public std::exception{ +private: + std::string message; +public: + NodeIdOutOfRangeException():message("You input id is out of range"){} + NodeIdOutOfRangeException(const std::string& msg) : message(msg) {} + NodeIdOutOfRangeException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){} + + virtual ~NodeIdOutOfRangeException() noexcept {} + virtual const char* what() const noexcept override { + return message.c_str(); + } + +}; + + + +#endif //NODEIDOUTOFRANGEEXCEPTION_H diff --git a/homework5/GraphExceptions/SameNodeConnectException.h b/homework5/GraphExceptions/SameNodeConnectException.h index 2b155c1..e9c264f 100644 --- a/homework5/GraphExceptions/SameNodeConnectException.h +++ b/homework5/GraphExceptions/SameNodeConnectException.h @@ -1,22 +1,22 @@ -// 这是一个异常,当用户试图连接图中的两个相同节点时抛出 - -#ifndef SAMENODECONNECTEXCEPTION_H -#define SAMENODECONNECTEXCEPTION_H -#include -#include - - -class SameNodeConnectException : public std::exception{ -private: - std::string message; -public: - SameNodeConnectException():message("Don't connect same node"){} - SameNodeConnectException(const std::string& msg) : message(msg) {} - SameNodeConnectException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){} - - virtual ~SameNodeConnectException() noexcept {} - virtual const char* what() const noexcept override { - return message.c_str(); - } -}; -#endif //SAMENODECONNECTEXCEPTION_H +// 这是一个异常,当用户试图连接图中的两个相同节点时抛出 + +#ifndef SAMENODECONNECTEXCEPTION_H +#define SAMENODECONNECTEXCEPTION_H +#include +#include + + +class SameNodeConnectException : public std::exception{ +private: + std::string message; +public: + SameNodeConnectException():message("Don't connect same node"){} + SameNodeConnectException(const std::string& msg) : message(msg) {} + SameNodeConnectException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){} + + virtual ~SameNodeConnectException() noexcept {} + virtual const char* what() const noexcept override { + return message.c_str(); + } +}; +#endif //SAMENODECONNECTEXCEPTION_H diff --git a/homework5/ListGraph.h b/homework5/ListGraph.h index 067aefa..8a72e78 100644 --- a/homework5/ListGraph.h +++ b/homework5/ListGraph.h @@ -1,136 +1,140 @@ -// -// Created by 31416 on 24-12-4. -// - -#ifndef LIST_GRAPH_H -#define LIST_GRAPH_H - -#include -#include - -#include "GraphExceptions/InsertExistedConnectException.h" -#include "GraphExceptions/NodeIdOutOfRangeException.h" -#include "GraphExceptions/SameNodeConnectException.h" -using namespace std; - -/* - * 简述: - * 节点=数据域+指针域 - * 第一个节点存储id - * 第二个节点存储值 - * 后续节点存储连接关系 - * 数据域: - * data:可存id,可存其他节点的id,可存数据 - * weight:边上的权 - */ - -template -class ListGraph { -private: - struct node{ - T data; - T weight; - struct node* next; - }; - vector nodes; - bool flag; //定义是否有向 - bool isWeighted; //定义是否有权 - -public: - ListGraph() { - nodes.clear(); - flag = false; - isWeighted = false; - } - ListGraph(bool flag) { - nodes.clear(); - this->flag = flag; - isWeighted = false; - } - ListGraph(bool flag,bool isWeighted) { - nodes.clear(); - this->flag = flag; - this->isWeighted = isWeighted; - } - void insert(T data) { - node* insert = new node(); - insert->data=nodes.size(); - insert->weight = 0; - insert->next=new node(); - insert->next->data=data; - insert->next->next=NULL; - nodes.push_back(insert); - } - void connect(int id1,int id2,T weight=0) { //flag区分是否为有向图 - if ((id1>nodes.size()-1||id2>nodes.size()-1)||(id1<0)||(id2<0)) { - throw NodeIdOutOfRangeException("您提供的id超出范围",0X002); - } - if(id1==id2) { - throw SameNodeConnectException("禁止连接图中的同一个点",0x001); - } - node *current = nodes.at(id1); - int current_id = 0; - while(current->next!=NULL) { - if((current->next->data==id2)&&(current_id>1)) { - if (current->next->weight==weight) { - throw InsertExistedConnectException("禁止插入已存在的连接",0x003); - } - current->next->weight=weight; - } - current = current->next; - current_id++; - } - current->next=new node(); - current->next->data=id2; - current->next->weight = weight; - current->next->next=NULL; - - if (!flag) { - node *current2 = nodes.at(id2); - int current2_id = 0; - while(current2->next!=NULL) { - if((current2->next->data==id2)&&(current2_id>1)) { - if (current2->next->weight==weight) { - throw InsertExistedConnectException("禁止插入已存在的连接",0x003); - } - current2->next->weight=weight; - } - current2 = current2->next; - current2_id++; - } - current2->next=new node(); - current2->next->data=id1; - current2->next->weight = weight; - current2->next->next=NULL; - } - } - string to_string() { - stringstream ss; - for (int i=0;i<=nodes.size()-1;i++) { - node* current = nodes.at(i); - while(current!=NULL) { - if(current->next==NULL) { - if (isWeighted) { - ss<data<<"|"<weight; - } - else { - ss<data; - } - }else { - if (isWeighted){ - ss<data<<"|"<weight<<" "; - } - else { - ss<data<<" "; - } - } - current = current->next; - } - ss< +#include + +#include "GraphExceptions/InsertExistedConnectException.h" +#include "GraphExceptions/NodeIdOutOfRangeException.h" +#include "GraphExceptions/SameNodeConnectException.h" +using namespace std; + +/* + * 简述: + * 节点=数据域+指针域 + * 第一个节点存储id + * 第二个节点存储值 + * 后续节点存储连接关系 + * 数据域: + * data:可存id,可存其他节点的id,可存数据 + * weight:边上的权 + */ + +template +class ListGraph { +private: + struct node{ + T data; + T weight; + struct node* next; + }; + vector nodes; + bool flag; //定义是否有向 + bool isWeighted; //定义是否有权 + +public: + ListGraph() { + nodes.clear(); + flag = false; + isWeighted = false; + } + ListGraph(bool flag) { + nodes.clear(); + this->flag = flag; + isWeighted = false; + } + ListGraph(bool flag,bool isWeighted) { + nodes.clear(); + this->flag = flag; + this->isWeighted = isWeighted; + } + void insert(T data) { + node* insert = new node(); + insert->data=nodes.size(); + insert->weight = 0; + insert->next=new node(); + insert->next->data=data; + insert->next->next=NULL; + nodes.push_back(insert); + } + void connect(int id1,int id2,T weight=0) { //flag区分是否为有向图 + if ((id1>nodes.size()-1||id2>nodes.size()-1)||(id1<0)||(id2<0)) { + throw NodeIdOutOfRangeException("您提供的id超出范围",0X002); + } + if(id1==id2) { + throw SameNodeConnectException("禁止连接图中的同一个点",0x001); + } + node *current = nodes.at(id1); + //跳过id节点和值节点 + for(int i=0;i<2;i++) { + current = current->next; + } + while(current->next!=NULL) { + if(current->next->data==id2) { + if (current->next->weight==weight) { + throw InsertExistedConnectException("禁止插入已存在的连接",0x003); + } + current->next->weight=weight; + } + current = current->next; + } + current->next=new node(); + current->next->data=id2; + current->next->weight = weight; + current->next->next=NULL; + + if (!flag) { + node *current2 = nodes.at(id2); + //跳过id节点和值节点 + for(int i=0;i<2;i++) { + current = current->next; + } + while(current2->next!=NULL) { + if((current2->next->data==id2)) { + if (current2->next->weight==weight) { + throw InsertExistedConnectException("禁止插入已存在的连接",0x003); + } + current2->next->weight=weight; + } + current2 = current2->next; + } + current2->next=new node(); + current2->next->data=id1; + current2->next->weight = weight; + current2->next->next=NULL; + } + } + string to_string() { + stringstream ss; + for (int i=0;i<=nodes.size()-1;i++) { + node* current = nodes.at(i); + while(current!=NULL) { + if(current->next==NULL) { + if (isWeighted) { + ss<data<<"|"<weight; + } + else { + ss<data; + } + }else { + if (isWeighted){ + ss<data<<"|"<weight<<" "; + } + else { + ss<data<<" "; + } + } + current = current->next; + } + ss< -using namespace std; - -template -class MatrixGraph { -private: - vector data; - vector > matrix; - int nodeCount; -public: - MatrixGraph() { - matrix.clear(); - data.clear(); - nodeCount = 0; - } - void insert(T value) { - data.push_back(value); - nodeCount++; - matrix.push_back(vector(nodeCount, false)); - for(int i = 0; i < nodeCount-1; ++i) { - matrix[i].push_back(false); - } - } -}; - - - -#endif //MATRIXGRAPH_H +// +// Created by 31416 on 24-12-5. +// + +#ifndef MATRIXGRAPH_H +#define MATRIXGRAPH_H + +#include +using namespace std; + +template +class MatrixGraph { +private: + vector data; + vector > matrix; + int nodeCount; +public: + MatrixGraph() { + matrix.clear(); + data.clear(); + nodeCount = 0; + } + void insert(T value) { + data.push_back(value); + nodeCount++; + matrix.push_back(vector(nodeCount, false)); + for(int i = 0; i < nodeCount-1; ++i) { + matrix[i].push_back(false); + } + } +}; + + + +#endif //MATRIXGRAPH_H diff --git a/homework5/test1.cpp b/homework5/test1.cpp index c62b025..b91e18e 100644 --- a/homework5/test1.cpp +++ b/homework5/test1.cpp @@ -1,35 +1,35 @@ -// 利用邻接矩阵存储无向图,并从0号顶点开始进行深度优先遍历。 -// 输入: -// 输入第一行是两个整数n e,其中n表示顶点数(则顶点编号为0至n-1),e表示图中的边数。之后有e行信息输入,每行输入表示一条边,格式是“顶点1 顶点2”,把边插入图中。如: -// 4 4 -// 0 1 -// 1 3 -// 0 3 -// 0 2 -// 输出: -// 先输出存储图的邻接矩阵,同一行元素之间空1格,最后一个元素之后不要有空格。 -// 之后空一行后输出从0号顶点开始的深度优先遍历序列,顶点编号之间空1格。 -// 例如,对于上面的示例输入,输出为: -// 0 1 1 1 -// 1 0 0 1 -// 1 0 0 0 -// 1 1 0 0 -// 从顶点0开始的深度优先遍历序列: -// 0 1 3 2 -// 说明 -// 输入第1个4表示有4个顶点,第2个4表示有4条边。之后的4行输入代表4条边的顶点。 -// 输出的前4行为邻接矩阵,之后空一行,然后输出的“0 1 3 2”是深度优先遍历序列。 - -#include"ListGraph.h" -#include -using namespace std; - -int main() { - ListGraph list_graph; - list_graph.insert(1); - list_graph.insert(2); - list_graph.connect(0,1); - list_graph.connect(1,0); - cout << list_graph.to_string() << endl; - return 0; -} +// 利用邻接矩阵存储无向图,并从0号顶点开始进行深度优先遍历。 +// 输入: +// 输入第一行是两个整数n e,其中n表示顶点数(则顶点编号为0至n-1),e表示图中的边数。之后有e行信息输入,每行输入表示一条边,格式是“顶点1 顶点2”,把边插入图中。如: +// 4 4 +// 0 1 +// 1 3 +// 0 3 +// 0 2 +// 输出: +// 先输出存储图的邻接矩阵,同一行元素之间空1格,最后一个元素之后不要有空格。 +// 之后空一行后输出从0号顶点开始的深度优先遍历序列,顶点编号之间空1格。 +// 例如,对于上面的示例输入,输出为: +// 0 1 1 1 +// 1 0 0 1 +// 1 0 0 0 +// 1 1 0 0 +// 从顶点0开始的深度优先遍历序列: +// 0 1 3 2 +// 说明 +// 输入第1个4表示有4个顶点,第2个4表示有4条边。之后的4行输入代表4条边的顶点。 +// 输出的前4行为邻接矩阵,之后空一行,然后输出的“0 1 3 2”是深度优先遍历序列。 + +#include"ListGraph.h" +#include +using namespace std; + +int main() { + ListGraph list_graph; + list_graph.insert(1); + list_graph.insert(2); + list_graph.connect(0,1); + list_graph.connect(1,0); + cout << list_graph.to_string() << endl; + return 0; +} diff --git a/期中测试.md b/期中测试.md index 651263e..0c01bf1 100644 --- a/期中测试.md +++ b/期中测试.md @@ -1,285 +1,285 @@ -# 期中测试试题📄 - -## 选择题🔤 - -1. 若已知一个栈的入栈序列是1,2,3,…,n,其输出序列为p1,p2,p3,…,pn,若p1=n,则pi为 - -- [ ] A.不确定 - -- [ ] B.i - -- [ ] C.n-i - -- [x] D.n-i+1 - -2. 判定以 head 为头指针的不带头结点的单链表只有一个结点的条件是( )。 - -- [x] A.head->next= =NULL - -- [ ] B.head!=NULL - -- [ ] C.head= =NULL - -- [ ] D.head->next= =head - -3. 对于顺序存储的长度为n的线性表,在第i个位置插入一个元素需要移动( )个元素。其中,0≤i<n。 - -- [ ] A.i - -- [ ] B.n-i-1 - -- [ ] C.n-i+1 - -- [x] D.n-i - -4. 允许对队列进行的操作有()。 - -- [x] A.在队头元素之前插入元素 - -- [ ] B.取出最近进队的元素 - -- [ ] C.删除队头元素 - -- [ ] D.对队列中的元素排序 - -5. 在班级信息管理表中,为了能按班级学号实现快速查询本班级学生信息的算法,采用( )更好。 - -- [x] A.顺序表 - -- [ ] B.链表 - -6. 设某顺序表中第一个元素的地址是 Base ,每个结点占 m 个单元,则第 i 个结点的地址为( )。 - -- [ ] A.Base+(i+1)∗m - -- [ ] B.Base−i∗m - -- [x] C.Base+(i−1)∗m - -- [ ] D.Base+i∗m - -7. 栈在____中有所应用。 - -- [x] A.前三个选项都有 - -- [ ] B.表达式求值 - -- [ ] C.递归调用 - -- [ ] D.函数调用 - -8. 若用一个大小为 6 的数组来实现循环队列,且当前 rear 和 front 的值分别为 0 和 3 ,当从队列中删除一个元素,再加入两个元素后, rear 和 front 的值分别为( )。 - -- [ ] A.1 和 4 - -- [x] B.1 和 5 - -- [ ] C.2 和 4 - -- [ ] D.2 和 3 - -9. 一个顺序表里已经有10个元素,最大容量是100,欲在顺序表中插入一个元素,则不合理的插入位置是( )。 - -- [x] A.15 - -- [ ] B.11 - -- [ ] C.5 - -- [ ] D.1 - -10. 判定以head为首指针的不带头结点的单链表为空的条件是( )。 - -- [x] A.head= =NULL - -- [ ] B.head->next= =NULL - -- [ ] C.head!=NULL - -- [ ] D.head->next= =head - -11. 线性表是( )。 - -- [x] A.一个有限序列,可以为空 - -- [ ] B.一个有限序列,不可以为空 - -- [ ] C.一个无限序列,不可以为空 - -- [ ] D.一个无限序列,可以为空 - -12. 一个顺序表里已经有 5 个元素,最大容量是 100 ,现插入 2 个元素,删除 3 个元素,该顺序表的长度是( )。 - -- [ ] A.5 - -- [x] B.4 - -- [ ] C.不确定 - -- [ ] D.1 - -13. 下面关于串的的叙述中, 是不正确的。 - -- [ ] A.模式匹配是串的一种重要运算 - -- [ ] B.串既可以采用顺序存储,也可以采用链式存储 - -- [x] C.空串是由空格构成的串 - -- [ ] D.串是字符的有限序列 - -14. 能够满足快速完成插入和删除运算的线性表存储结构是( )。 - -- [ ] A.散列存储 - -- [x] B.链式存储 - -- [ ] C.有序存储 - -- [ ] D.顺序存储 - -15. 设进栈次序为ABCDE,( )是不可能得到的出栈序列。 - -- [ ] A.ABCDE - -- [x] B.EABCD - -- [ ] C.BCDEA - -- [ ] D.EDCBA - -16. 铁路进行列车调度时,常把站台设计成栈式结构,若进站的六辆列车顺序为:1,2,3,4,5,6, 不能出栈序列是( )。 - -- [ ] A.1 2 3 4 5 6 - -- [ ] B.3 2 5 6 4 1 - -- [ ] C.1 3 5 4 2 6 - -- [x] D.4 3 5 6 1 2 - -17. 在数据结构中,从逻辑上可以把数据结构分成 - -- [ ] A.线性结构和非线性结构 - -- [x] B.动态结构和静态结构 - -- [ ] C.内部结构和外部结构 - -- [ ] D.紧凑结构和非紧凑结构 - -18. 线性表的顺序存储最适合于实现( )运算。 - -- [ ] A.查找 - -- [ ] B.插入 - -- [x] C.由下标定位 - -- [ ] D.删除 - -19. 顺序表中第一个元素的存储地址是100,每个元素的长度为2,则第5个元素的地址是 - -- [ ] A.110 - -- [ ] B.100 - -- [x] C.108 - -- [ ] D.120 - -20. 在一个单链表中,若删除p所指结点的后续结点,则执行( )。 - -- [ ] A.p=p->next->next - -- [ ] B.p->next=p->next - -- [ ] C.p=p->next;p->next=p->next->next - -- [x] D.p-next=p->next->next - -21. 数组S[M]存储一个栈,top为栈顶指针。如果条件top= =-1表示栈空,在栈不空的情况下,栈顶元素为( )。 - -- [ ] A.S[++top] - -- [ ] B.S[top-1] - -- [ ] C.S[top+1] - -- [x] D.S[top] - -22. 对于链接存储的存储结构所占存储空间,以下选项描述正确的是( )。 - -- [x] A.分两部分,一部分存放结点值,另一部分存放表示结点间关系的指针 - -- [ ] B.分两部分,一部分存放结点值,另一部分存放结点所占单元数 - -- [ ] C.只有一部分,存储表示结点间关系的指针 - -- [ ] D.只有一部分,存放结点值 - -23. 已知last指向单链表的尾结点,将 s 所指结点加在表尾,以下选项描述正确的是( )。 - -- [x] A.s->next=last, last->next=NULL,last=s; - -- [ ] B.last->next=s,s->next=NULL,last=s; - -- [ ] C.s->next=NULL, last->next=s, s=last; - -- [ ] D.s->next=s,last=s,last->next=NULL; - -24. 对于一个头指针为head的带头结点的单链表,判定该表为空表的条件是( )。 - -- [ ] A.head→next==head - -- [ ] B.head==NULL - -- [ ] C.head!=NULL - -- [x] D.head→next==NULL - -25. 线性表L在 情况下适用于使用链式结构实现。 - -- [ ] A.L中结点结构复杂 - -- [x] B.需不断对L进行删除插入 - -- [ ] C.需经常修改L中的结点值 - -- [ ] D.L中含有大量的结点 - -26. 循环队列存储在数组A[0..m]中,则入队时的操作为 - -- [ ] A.rear=rear+1 - -- [x] B.rear=(rear+1)%(m+1) - -- [ ] C.rear=(rear+1)%(m-1) - -- [ ] D.rear=(rear+1)%m - -## 填空题🈳 - -27. 在某个线性链表中在p节点后插入节点x,若s为指向插入节点x的指针,则指针修改语句为s->next=p->next 和什么? - -```c -p->next=s; -``` - -28. 线性表中元素的存储可以使用顺序表和 。 - -```中文(简体) -链表 -``` - -29. 数据结构中数据的关系通常有集合、、树形结构、图状结构和网状结构。 - -```中文(简体) -数组 -``` - -30. 循环队列Q中判断队满的条件为 (最大空间MAXQSIZE、队头指针Q.front,队尾指针Q.rear)。 - -```c -(Q.rear+1)%MAXQSIZE==Q.front; -``` +# 期中测试试题📄 + +## 选择题🔤 + +1. 若已知一个栈的入栈序列是1,2,3,…,n,其输出序列为p1,p2,p3,…,pn,若p1=n,则pi为 + +- [ ] A.不确定 + +- [ ] B.i + +- [ ] C.n-i + +- [x] D.n-i+1 + +2. 判定以 head 为头指针的不带头结点的单链表只有一个结点的条件是( )。 + +- [x] A.head->next= =NULL + +- [ ] B.head!=NULL + +- [ ] C.head= =NULL + +- [ ] D.head->next= =head + +3. 对于顺序存储的长度为n的线性表,在第i个位置插入一个元素需要移动( )个元素。其中,0≤i<n。 + +- [ ] A.i + +- [ ] B.n-i-1 + +- [ ] C.n-i+1 + +- [x] D.n-i + +4. 允许对队列进行的操作有()。 + +- [x] A.在队头元素之前插入元素 + +- [ ] B.取出最近进队的元素 + +- [ ] C.删除队头元素 + +- [ ] D.对队列中的元素排序 + +5. 在班级信息管理表中,为了能按班级学号实现快速查询本班级学生信息的算法,采用( )更好。 + +- [x] A.顺序表 + +- [ ] B.链表 + +6. 设某顺序表中第一个元素的地址是 Base ,每个结点占 m 个单元,则第 i 个结点的地址为( )。 + +- [ ] A.Base+(i+1)∗m + +- [ ] B.Base−i∗m + +- [x] C.Base+(i−1)∗m + +- [ ] D.Base+i∗m + +7. 栈在____中有所应用。 + +- [x] A.前三个选项都有 + +- [ ] B.表达式求值 + +- [ ] C.递归调用 + +- [ ] D.函数调用 + +8. 若用一个大小为 6 的数组来实现循环队列,且当前 rear 和 front 的值分别为 0 和 3 ,当从队列中删除一个元素,再加入两个元素后, rear 和 front 的值分别为( )。 + +- [ ] A.1 和 4 + +- [x] B.1 和 5 + +- [ ] C.2 和 4 + +- [ ] D.2 和 3 + +9. 一个顺序表里已经有10个元素,最大容量是100,欲在顺序表中插入一个元素,则不合理的插入位置是( )。 + +- [x] A.15 + +- [ ] B.11 + +- [ ] C.5 + +- [ ] D.1 + +10. 判定以head为首指针的不带头结点的单链表为空的条件是( )。 + +- [x] A.head= =NULL + +- [ ] B.head->next= =NULL + +- [ ] C.head!=NULL + +- [ ] D.head->next= =head + +11. 线性表是( )。 + +- [x] A.一个有限序列,可以为空 + +- [ ] B.一个有限序列,不可以为空 + +- [ ] C.一个无限序列,不可以为空 + +- [ ] D.一个无限序列,可以为空 + +12. 一个顺序表里已经有 5 个元素,最大容量是 100 ,现插入 2 个元素,删除 3 个元素,该顺序表的长度是( )。 + +- [ ] A.5 + +- [x] B.4 + +- [ ] C.不确定 + +- [ ] D.1 + +13. 下面关于串的的叙述中, 是不正确的。 + +- [ ] A.模式匹配是串的一种重要运算 + +- [ ] B.串既可以采用顺序存储,也可以采用链式存储 + +- [x] C.空串是由空格构成的串 + +- [ ] D.串是字符的有限序列 + +14. 能够满足快速完成插入和删除运算的线性表存储结构是( )。 + +- [ ] A.散列存储 + +- [x] B.链式存储 + +- [ ] C.有序存储 + +- [ ] D.顺序存储 + +15. 设进栈次序为ABCDE,( )是不可能得到的出栈序列。 + +- [ ] A.ABCDE + +- [x] B.EABCD + +- [ ] C.BCDEA + +- [ ] D.EDCBA + +16. 铁路进行列车调度时,常把站台设计成栈式结构,若进站的六辆列车顺序为:1,2,3,4,5,6, 不能出栈序列是( )。 + +- [ ] A.1 2 3 4 5 6 + +- [ ] B.3 2 5 6 4 1 + +- [ ] C.1 3 5 4 2 6 + +- [x] D.4 3 5 6 1 2 + +17. 在数据结构中,从逻辑上可以把数据结构分成 + +- [ ] A.线性结构和非线性结构 + +- [x] B.动态结构和静态结构 + +- [ ] C.内部结构和外部结构 + +- [ ] D.紧凑结构和非紧凑结构 + +18. 线性表的顺序存储最适合于实现( )运算。 + +- [ ] A.查找 + +- [ ] B.插入 + +- [x] C.由下标定位 + +- [ ] D.删除 + +19. 顺序表中第一个元素的存储地址是100,每个元素的长度为2,则第5个元素的地址是 + +- [ ] A.110 + +- [ ] B.100 + +- [x] C.108 + +- [ ] D.120 + +20. 在一个单链表中,若删除p所指结点的后续结点,则执行( )。 + +- [ ] A.p=p->next->next + +- [ ] B.p->next=p->next + +- [ ] C.p=p->next;p->next=p->next->next + +- [x] D.p-next=p->next->next + +21. 数组S[M]存储一个栈,top为栈顶指针。如果条件top= =-1表示栈空,在栈不空的情况下,栈顶元素为( )。 + +- [ ] A.S[++top] + +- [ ] B.S[top-1] + +- [ ] C.S[top+1] + +- [x] D.S[top] + +22. 对于链接存储的存储结构所占存储空间,以下选项描述正确的是( )。 + +- [x] A.分两部分,一部分存放结点值,另一部分存放表示结点间关系的指针 + +- [ ] B.分两部分,一部分存放结点值,另一部分存放结点所占单元数 + +- [ ] C.只有一部分,存储表示结点间关系的指针 + +- [ ] D.只有一部分,存放结点值 + +23. 已知last指向单链表的尾结点,将 s 所指结点加在表尾,以下选项描述正确的是( )。 + +- [x] A.s->next=last, last->next=NULL,last=s; + +- [ ] B.last->next=s,s->next=NULL,last=s; + +- [ ] C.s->next=NULL, last->next=s, s=last; + +- [ ] D.s->next=s,last=s,last->next=NULL; + +24. 对于一个头指针为head的带头结点的单链表,判定该表为空表的条件是( )。 + +- [ ] A.head→next==head + +- [ ] B.head==NULL + +- [ ] C.head!=NULL + +- [x] D.head→next==NULL + +25. 线性表L在 情况下适用于使用链式结构实现。 + +- [ ] A.L中结点结构复杂 + +- [x] B.需不断对L进行删除插入 + +- [ ] C.需经常修改L中的结点值 + +- [ ] D.L中含有大量的结点 + +26. 循环队列存储在数组A[0..m]中,则入队时的操作为 + +- [ ] A.rear=rear+1 + +- [x] B.rear=(rear+1)%(m+1) + +- [ ] C.rear=(rear+1)%(m-1) + +- [ ] D.rear=(rear+1)%m + +## 填空题🈳 + +27. 在某个线性链表中在p节点后插入节点x,若s为指向插入节点x的指针,则指针修改语句为s->next=p->next 和什么? + +```c +p->next=s; +``` + +28. 线性表中元素的存储可以使用顺序表和 。 + +```中文(简体) +链表 +``` + +29. 数据结构中数据的关系通常有集合、、树形结构、图状结构和网状结构。 + +```中文(简体) +数组 +``` + +30. 循环队列Q中判断队满的条件为 (最大空间MAXQSIZE、队头指针Q.front,队尾指针Q.rear)。 + +```c +(Q.rear+1)%MAXQSIZE==Q.front; +```