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