├── BTH1 ├── 01.cpp ├── 02.cpp └── Đề bài.pdf ├── BTH10 ├── Hash Table.cpp └── Đề bài.pdf ├── BTH2 ├── 01.cpp └── 02.cpp ├── BTH3 ├── Single Linked List.cpp └── Đề bài.pdf ├── BTH4 ├── Single Linked List 2.cpp └── Đề bài.pdf ├── BTH5 ├── Single Linked List 3 │ ├── 01.cpp │ ├── 02.cpp │ └── Single Linked List 3.pdf └── Stack and Queue │ ├── 01.cpp │ ├── 02.cpp │ └── Stack and Queue.pdf ├── BTH6 ├── Single Linked List 4.cpp └── Đề bài.pdf ├── BTH7 ├── Sorting Algorithm.cpp └── Đề bài.pdf ├── BTH8 ├── Tree.cpp └── Đề bài.pdf ├── BTH9 ├── Thêm │ ├── 01.cpp │ ├── 02.cpp │ └── 03.cpp ├── Tree 2.cpp └── Đề bài.pdf ├── Giáo trình tham khảo.pdf ├── Mẫu menu.cpp ├── README.md ├── Thi thử cuối học kỳ ├── 01.cpp └── Đề bài.pdf └── Đề cương môn học.pdf /BTH1/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void input(int[], int); 5 | void ouput(int[], int); 6 | 7 | void deleteElementAtPositionK(int[], int&); 8 | void getMaxValue(int[], int); 9 | void getMinValue(int[], int); 10 | 11 | void getMinusCount(int[], int); 12 | 13 | void getMaxMinusValue(int[], int); 14 | void getMinPositiveValue(int[], int); 15 | 16 | void findValuesEqualToX(int[], int&); 17 | void countValuesEqualToX(int[], int); 18 | 19 | void printMenu(); 20 | void choose(int[], int&); 21 | 22 | int main() 23 | { 24 | int arrSize; 25 | cout << "n = "; 26 | cin >> arrSize; 27 | system("cls"); 28 | 29 | int* arr = new int[arrSize]; 30 | 31 | input(arr, arrSize); 32 | system("cls"); 33 | 34 | choose(arr, arrSize); 35 | } 36 | 37 | void printMenu() { 38 | cout << "\n--------------------------------------------------" << endl; 39 | cout << "1. Xuat mang" << endl; 40 | cout << "2. Xoa phan tu tai vi tri x trong mang" << endl; 41 | cout << "3. Liet ke nhung phan tu co gia tri lon nhat trong mang" << endl; 42 | cout << "4. Liet ke nhung phan tu co gia tri nho nhat trong mang" << endl; 43 | cout << "5. Dem so phan tu co gia tri am trong mang" << endl; 44 | cout << "6. Cho biet gia tri am lon nhat trong mang" << endl; 45 | cout << "7. Cho biet gia tri duong nho nhat trong mang" << endl; 46 | cout << "8. Tim phan tu co gia tri bang x co hay khong trong mang" << endl; 47 | cout << "9. Dem so phan tu co gia tri bang x trong mang" << endl; 48 | cout << "--------------------------------------------------" << endl; 49 | } 50 | void choose(int arr[], int& arrSize) { 51 | printMenu(); 52 | int choice; 53 | cout << "Nhap tuy chon: "; 54 | cin >> choice; 55 | system("cls"); 56 | 57 | while (choice != 0) 58 | { 59 | switch (choice) { 60 | case 1: 61 | ouput(arr, arrSize); 62 | break; 63 | case 2: 64 | deleteElementAtPositionK(arr, arrSize); 65 | break; 66 | case 3: 67 | getMaxValue(arr, arrSize); 68 | break; 69 | case 4: 70 | getMinValue(arr, arrSize); 71 | break; 72 | case 5: 73 | getMinusCount(arr, arrSize); 74 | break; 75 | case 6: 76 | getMaxMinusValue(arr, arrSize); 77 | break; 78 | case 7: 79 | getMaxMinusValue(arr, arrSize); 80 | break; 81 | case 8: 82 | findValuesEqualToX(arr, arrSize); 83 | break; 84 | case 9: 85 | countValuesEqualToX(arr, arrSize); 86 | break; 87 | default: 88 | cout << "Khong ton tai lua chon" << endl; 89 | break; 90 | } 91 | choose(arr, arrSize); 92 | } 93 | } 94 | 95 | void input(int arr[], int arrSize) { 96 | cout << "Nhap mang co " << arrSize << " phan tu: " << endl; 97 | for (int i = 0; i < arrSize; i++) 98 | { 99 | cout << "a[" << i << "] = "; 100 | cin >> arr[i]; 101 | } 102 | } 103 | void ouput(int arr[], int arrSize) { 104 | cout << "Mang nhap vao la: "; 105 | for (int i = 0; i < arrSize; i++) 106 | cout << arr[i] << " "; 107 | } 108 | 109 | void deleteElementAtPositionK(int arr[], int& arrSize) { 110 | cout << "k = "; 111 | int k; 112 | cin >> k; 113 | system("cls"); 114 | cout << "Mang sau khi xoa phan tu tai vi tri thu " << k << " la: "; 115 | if (arrSize <= 0) { 116 | return; 117 | } 118 | if (k < 0) 119 | k = 0; 120 | else if (k >= arrSize) 121 | k = arrSize - 1; 122 | for (int i = k; i < arrSize - 1; i++) 123 | arr[i] = arr[i + 1]; 124 | --arrSize; 125 | for (int i = 0; i < arrSize; i++) 126 | cout << arr[i] << " "; 127 | } 128 | 129 | void getMaxValue(int arr[], int arrSize) { 130 | int temp = arr[0]; 131 | for (int i = 1; i < arrSize; i++) 132 | if (arr[i] >= temp) 133 | temp = arr[i]; 134 | cout << "Cac phan tu co gia tri lon nhat trong mang tai vi tri "; 135 | for (int i = 0; i < arrSize; i++) 136 | if (arr[i] == temp) 137 | cout << i << " "; 138 | } 139 | 140 | void getMinValue(int arr[], int arrSize) { 141 | int temp = arr[0]; 142 | for (int i = 1; i < arrSize; i++) 143 | if (arr[i] < temp) 144 | temp = arr[i]; 145 | cout << "Cac phan tu co gia tri nho nhat trong mang tai vi tri "; 146 | for (int i = 0; i < arrSize; i++) 147 | if (arr[i] == temp) 148 | cout << i << " "; 149 | } 150 | 151 | void getMinusCount(int arr[], int arrSize) { 152 | int temp = 0; 153 | for (int i = 0; i < arrSize; i++) 154 | if (arr[i] < 0) 155 | temp++; 156 | cout << "So phan tu am trong mang la " << temp; 157 | } 158 | 159 | void getMaxMinusValue(int arr[], int arrSize) { 160 | cout << "Phan tu am lon nhat trong mang la "; 161 | int temp = arr[0]; 162 | for (int i = 0; i < arrSize; i++) 163 | if (arr[i] > temp && arr[i] < 0) 164 | temp = arr[i]; 165 | cout << temp; 166 | } 167 | 168 | void getMinPositiveValue(int arr[], int arrSize) { 169 | cout << "Phan tu dương nho nhat trong mang la "; 170 | int temp = arr[0]; 171 | for (int i = 1; i < arrSize; i++) 172 | if (arr[i] < temp && arr[i]>0) 173 | temp = arr[i]; 174 | cout << temp; 175 | } 176 | 177 | void findValuesEqualToX(int arr[], int& arrSize) { 178 | cout << "x = "; 179 | int x; 180 | cin >> x; 181 | system("cls"); 182 | for (int i = 0; i < arrSize; i++) 183 | if (arr[i] == x) 184 | { 185 | cout << "Phan tu co gia tri " << x << " co trong mang"; 186 | cout << endl << endl; 187 | return; 188 | } 189 | cout << "Phan tu co gia tri " << x << " khong co trong mang"; 190 | } 191 | 192 | void countValuesEqualToX(int arr[], int arrSize) { 193 | cout << "x = "; 194 | int x; 195 | cin >> x; 196 | system("cls"); 197 | int temp = 0; 198 | for (int i = 0; i < arrSize; i++) 199 | if (arr[i] == x) 200 | temp++; 201 | if (temp > 0) 202 | cout << "So phan tu co gia tri " << x << " co trong mang la " << temp; 203 | else cout << "Phan tu co gia tri " << x << " khong co trong mang"; 204 | } 205 | -------------------------------------------------------------------------------- /BTH1/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student { 6 | public: 7 | string name; 8 | string ID; 9 | double score; 10 | public: 11 | void import() { 12 | cout << "Ten sinh vien: "; 13 | int temp = getchar(); 14 | getline(cin, name); 15 | cout << "Ma sinh vien: "; 16 | getline(cin, ID); 17 | cout << "Diem trung binh: "; 18 | cin >> score; 19 | while (score < 0 || score > 10) 20 | { 21 | cout << "Diem trung binh khong hop le, hay nhap lai" << endl; 22 | cout << "Diem trung binh: "; 23 | cin >> score; 24 | } 25 | cout << endl; 26 | } 27 | void infoExport() { 28 | cout << "Ten sinh vien: "; 29 | cout << name << endl; 30 | cout << "Ma sinh vien: "; 31 | cout << ID << endl; 32 | cout << "Diem trung binh: "; 33 | cout << score << endl; 34 | } 35 | }; 36 | 37 | void arrayImport(student x[], int& y); 38 | void arrayExport(student x[], int& y); 39 | 40 | void maxExport(student x[], int y); 41 | 42 | void find_Above5(student x[], int y); 43 | void find_student(student x[], int y); 44 | 45 | void detele_student(student x[], int& y); 46 | void add_student(student x[], int& y); 47 | 48 | int main() 49 | { 50 | int n, x; 51 | cout << "So sinh vien la: "; 52 | cin >> n; 53 | student* sinhVien = new student[n]; 54 | arrayImport(sinhVien, n); 55 | 56 | cout << endl; 57 | cout << "1. In danh sach sinh vien" << endl; 58 | cout << "2. Liet ke cac sinh vien co diem trung binh cao nhat lop" << endl; 59 | cout << "3. Cho biet so sinh vien co diem trung binh tu 5 tro len" << endl; 60 | cout << "4. Tim sinh vien co ten X trong lop" << endl; 61 | cout << "5. Xoa sinh vien co ma so cho truoc" << endl; 62 | cout << "6. Them mot sinh vien vao lop hoc" << endl; 63 | cout << endl; 64 | 65 | cout << "Nhap tuy chon: "; 66 | cin >> x; 67 | cout << endl; 68 | 69 | while (x != 0) 70 | { 71 | switch (x) { 72 | case 1: 73 | arrayExport(sinhVien, n); 74 | break; 75 | case 2: 76 | maxExport(sinhVien, n); 77 | break; 78 | case 3: 79 | find_Above5(sinhVien, n); 80 | break; 81 | case 4: 82 | find_student(sinhVien, n); 83 | break; 84 | case 5: 85 | detele_student(sinhVien, n); 86 | break; 87 | case 6: 88 | add_student(sinhVien, n); 89 | break; 90 | default: 91 | cout << "Khong ton tai lua chon"; 92 | break; 93 | } 94 | cout << "Nhap tuy chon: "; 95 | cin >> x; 96 | cout << endl; 97 | } 98 | } 99 | 100 | void arrayImport(student x[], int& y) { 101 | cout << endl << "Nhap danh sach sinh vien: " << endl << endl; 102 | for (int i = 0; i < y; i++) 103 | { 104 | cout << "Sinh vien thu " << i + 1 << ": " << endl; 105 | x[i].import(); 106 | } 107 | } 108 | 109 | void arrayExport(student x[], int& y) { 110 | cout << "Danh sach sinh vien vua nhap la: " << endl; 111 | for (int i = 0; i < y; i++) 112 | { 113 | x[i].infoExport(); 114 | cout << endl << endl; 115 | } 116 | } 117 | 118 | void maxExport(student x[], int y) { 119 | cout << "Cac sinh vien co diem trung binh cao nhat la: " << endl; 120 | double max = x[0].score; 121 | for (int i = 1; i < y; i++) 122 | if (x[i].score > max) 123 | max = x[i].score; 124 | for (int i = 1; i < y; i++) 125 | if (x[i].score == max) 126 | cout << " " << x[i].name << endl; 127 | cout << endl; 128 | } 129 | 130 | void find_Above5(student x[], int y) { 131 | int temp = 0; 132 | for (int i = 0; i < y; i++) 133 | if (x[i].score >= 5) 134 | temp += 1; 135 | if (temp > 0) 136 | cout << "So sinh vien co diem trung binh tu 5 tro len la " << temp << endl << endl; 137 | else cout << "Khong co sinh vien nao co diem trung binh tu 5 tro len." << endl << endl; 138 | } 139 | 140 | void find_student(student x[], int y) { 141 | string name; 142 | cout << "Nhap ten sinh vien can tim: "; 143 | int temp = getchar(); 144 | getline(cin, name); 145 | for (int i = 0; i < y; i++) 146 | if (x[i].name == name) 147 | { 148 | cout << "Thong tin cua sinh vien can tim la: " << endl; 149 | x[i].infoExport(); 150 | cout << endl << endl; 151 | return; 152 | } 153 | cout << "Sinh vien khong ton tai" << endl; 154 | cout << endl << endl; 155 | } 156 | 157 | void detele_student(student x[], int& y) { 158 | string ID; 159 | cout << "Nhap ma sinh vien can xoa: "; 160 | int temp = getchar(); 161 | getline(cin, ID); 162 | for (int i = 0; i < y; i++) 163 | if (x[i].ID == ID) 164 | { 165 | y--; 166 | cout << "Da xoa sinh vien co ma " << x[i].ID << " khoi danh sach" << endl; 167 | for (int j = i; j < y; j++) 168 | x[j] = x[j + 1]; 169 | return; 170 | } 171 | cout << "Sinh vien khong ton tai" << endl; 172 | cout << endl; 173 | } 174 | 175 | void add_student(student x[], int& y) { 176 | cout << "Nhap thong tin sinh vien can them: " << endl; 177 | y++; 178 | x[y-1].import(); 179 | for (int i = 0; i < y - 1; i++) 180 | for (int j = i + 1; j < y; j++) 181 | if (x[i].ID > x[j].ID) 182 | { 183 | student temp; 184 | temp = x[i]; 185 | x[i] = x[j]; 186 | x[j] = temp; 187 | } 188 | cout << endl; 189 | } -------------------------------------------------------------------------------- /BTH1/Đề bài.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/BTH1/Đề bài.pdf -------------------------------------------------------------------------------- /BTH10/Hash Table.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class hashTable { 6 | public: 7 | int tableSize; 8 | int* table; 9 | hashTable(); 10 | 11 | virtual int valueOf(int); 12 | virtual void addValue(int); 13 | virtual int getValue(int); 14 | 15 | void add_manyValue(); 16 | virtual void printTable(); 17 | }; 18 | 19 | class hashLinear : public hashTable { 20 | public: 21 | int valueOf(int); 22 | void addValue(int); 23 | int getValue(int); 24 | }; 25 | class hashSquare : public hashTable { 26 | public: 27 | int valueOf(int); 28 | void addValue(int); 29 | int getValue(int); 30 | }; 31 | 32 | class hashChain : public hashTable { 33 | vector chain[11]; 34 | public: 35 | virtual int valueOf(int x); 36 | void addValue(int element); 37 | int getValue(int element); 38 | void printTable(); 39 | }; 40 | class hashChain_f1 : public hashChain { 41 | int valueOf(int x); 42 | }; 43 | class hashChain_f2 : public hashChain { 44 | int valueOf(int x); 45 | }; 46 | 47 | void menu(); 48 | void choose(hashTable*&); 49 | 50 | void menu_hashChain(); 51 | void choose_hashChain(hashTable*&); 52 | 53 | int main(){ 54 | hashTable *table; 55 | choose(table); 56 | } 57 | 58 | hashTable::hashTable() { 59 | tableSize = 11; 60 | table = new int[tableSize]; 61 | for (int i = 0; i < tableSize; i++) 62 | table[i] = -1; 63 | } 64 | int hashTable::valueOf(int) { 65 | return 0; 66 | } 67 | void hashTable::addValue(int) { 68 | cout << "Nothing here"; 69 | } 70 | int hashTable::getValue(int) { 71 | return 0; 72 | } 73 | 74 | void hashTable::add_manyValue() { 75 | addValue(32); 76 | addValue(15); 77 | addValue(25); 78 | addValue(44); 79 | addValue(36); 80 | addValue(21); 81 | } 82 | void hashTable::printTable() { 83 | cout << " KEY VALUE" << endl; 84 | cout << " ---- -----" << endl; 85 | for (int i = 0; i < tableSize; i++) 86 | if (table[i] != -1) 87 | cout << " " << table[i] << " " << getValue(table[i]) << endl; 88 | } 89 | 90 | int hashLinear::valueOf(int x) { 91 | return x % tableSize; 92 | } 93 | void hashLinear::addValue(int element) { 94 | int index = valueOf(element); 95 | while (table[index] != -1) 96 | index = valueOf(index + 1); 97 | table[index] = element; 98 | } 99 | int hashLinear::getValue(int element) { 100 | int index = valueOf(element); 101 | while (table[index] != -1 && table[index] != element) 102 | index = valueOf(index + 1); 103 | if (table[index] == element) 104 | return index; 105 | else return -1; 106 | } 107 | 108 | int hashSquare::valueOf(int x) { 109 | return x % tableSize; 110 | } 111 | void hashSquare::addValue(int element) { 112 | int index = valueOf(element); 113 | int k = 0; 114 | while (table[index] != -1) { 115 | k++; 116 | index = valueOf(valueOf(element) + k * k); 117 | } 118 | table[index] = element; 119 | } 120 | int hashSquare::getValue(int element) { 121 | int index = valueOf(element); 122 | int k = 0; 123 | while (table[index] != -1 && table[index] != element) { 124 | k++; 125 | index = valueOf(valueOf(element) + k * k); 126 | } 127 | if (table[index] == element) 128 | return index; 129 | else return -1; 130 | } 131 | 132 | int hashChain::valueOf(int x) { 133 | return x % tableSize; 134 | } 135 | void hashChain::addValue(int element) { 136 | int index = valueOf(element); 137 | chain[index].push_back(element); 138 | } 139 | int hashChain::getValue(int element) { 140 | int index = valueOf(element); 141 | for (int i = 0; i < tableSize; i++) 142 | for (auto value : chain[index]) 143 | if (value == element) 144 | return index; 145 | return -1; 146 | } 147 | void hashChain::printTable() { 148 | cout << " KEY VALUE" << endl; 149 | cout << " ---- -----" << endl; 150 | for (int i = 0; i < tableSize; i++) { 151 | size_t j; 152 | for (j = 0; j < chain[i].size(); j++) 153 | cout << " " << chain[i][j]; 154 | if (j != 0) 155 | cout << " " << i << endl; 156 | } 157 | } 158 | 159 | int hashChain_f1::valueOf(int x) { 160 | return x % tableSize; 161 | } 162 | int hashChain_f2::valueOf(int x) { 163 | return (tableSize - 2) - (x % (tableSize - 2)); 164 | } 165 | 166 | void menu() { 167 | cout << "=========================================================" << endl; 168 | cout << " Luu y: Bang bam da duoc them san cac gia tri 32, 15, 25, 44, 36 va 21" << endl; 169 | cout << " (1) Tham do tuyen tinh" << endl; 170 | cout << " (2) Tham do binh phuong" << endl; 171 | cout << " (3) Tham do day chuyen" << endl; 172 | cout << "=========================================================" << endl; 173 | } 174 | void choose(hashTable*& table) { 175 | int choice; 176 | menu(); 177 | cout << " Lua chon cua ban la: "; 178 | cin >> choice; 179 | system("cls"); 180 | switch (choice) { 181 | case 1: 182 | system("cls"); 183 | table = new hashLinear; 184 | table->add_manyValue(); 185 | table->printTable(); 186 | choose(table); 187 | break; 188 | case 2: 189 | system("cls"); 190 | table = new hashSquare; 191 | table->add_manyValue(); 192 | table->printTable(); 193 | choose(table); 194 | break; 195 | case 3: 196 | system("cls"); 197 | choose_hashChain(table); 198 | choose(table); 199 | break; 200 | default: 201 | system("cls"); 202 | choose(table); 203 | break; 204 | } 205 | } 206 | 207 | void menu_hashChain() { 208 | cout << "===============================================================================" << endl; 209 | cout << " (1) Bam theo cong thuc F(key) = key % M'" << endl; 210 | cout << " (2) Bam theo cong thuc F(key) = (tableSize - 2) - (x % (tableSize - 2))" << endl; 211 | cout << "===============================================================================" << endl; 212 | } 213 | void choose_hashChain(hashTable*& table) { 214 | int choice; 215 | menu_hashChain(); 216 | cout << " Lua chon cua ban la: "; 217 | cin >> choice; 218 | system("cls"); 219 | switch (choice) { 220 | case 1: 221 | system("cls"); 222 | table = new hashChain_f1; 223 | table->add_manyValue(); 224 | table->printTable(); 225 | choose(table); 226 | break; 227 | case 2: 228 | system("cls"); 229 | table = new hashChain_f2; 230 | table->add_manyValue(); 231 | table->printTable(); 232 | choose(table); 233 | break; 234 | default: 235 | system("cls"); 236 | choose(table); 237 | break; 238 | } 239 | } -------------------------------------------------------------------------------- /BTH10/Đề bài.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/BTH10/Đề bài.pdf -------------------------------------------------------------------------------- /BTH2/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void array_import(int arr[], int x); 5 | int even_sum(int arr[], int x); 6 | bool prime_check(int x); 7 | void prime_sum(int arr[], int x); 8 | void arrange(int arr[], int x); 9 | void main() 10 | { 11 | int n; 12 | cout << "Nhap so phan tu: "; 13 | cin >> n; 14 | int *a = new int[n]; 15 | array_import(a, n); 16 | cout << "Tong cac phan tu chan trong mang la " << even_sum(a, n) << endl; 17 | prime_sum(a, n); 18 | arrange(a, n); 19 | system("pause"); 20 | } 21 | void array_import(int arr[], int x) { 22 | for (int i = 0; i < x; i++) 23 | { 24 | cout << " Phan tu thu " << i + 1 << " la "; 25 | cin >> arr[i]; 26 | } 27 | } 28 | int even_sum(int arr[], int x) { 29 | int sum = 0; 30 | for (int i = 0; i < x; i++) 31 | if (arr[i] % 2 == 0) 32 | sum += arr[i]; 33 | return sum; 34 | } 35 | bool prime_check(int x) { 36 | if (x < 2) 37 | return false; 38 | for (int i = 2; i < x; i++) 39 | if (x % i == 0) 40 | return false; 41 | return true; 42 | } 43 | void prime_sum(int arr[], int x) { 44 | int sum = 0; 45 | for (int i = 0; i < x; i++) 46 | if (prime_check(arr[i])) 47 | sum += arr[i]; 48 | cout << "Tong cac so nguyen to trong mang la " << sum << endl; 49 | } 50 | void arrange(int arr[], int x) { 51 | for (int i = 0; i < x - 1; i++) 52 | for (int j = i + 1; j < x; j++) 53 | if (arr[i] > arr[j]) 54 | { 55 | int temp = arr[i]; 56 | arr[i] = arr[j]; 57 | arr[j] = temp; 58 | } 59 | cout << "Mang sau khi sap xep la: "; 60 | for (int i = 0; i < x; i++) 61 | cout << arr[i] << " "; 62 | cout << endl; 63 | } -------------------------------------------------------------------------------- /BTH2/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class fraction { 4 | public: 5 | int tuSo, mauSo; 6 | public: 7 | void import() { 8 | cout << " Tu so la: "; 9 | cin >> tuSo; 10 | cout << " Mau so la: "; 11 | cin >> mauSo; 12 | while (mauSo == 0) 13 | { 14 | cout << "Mau so phai khac 0.\n Mau so moi la: "; 15 | cin >> mauSo; 16 | } 17 | } 18 | void simple() { 19 | for (int i = tuSo; i >= 1; i--) 20 | if (tuSo % i == 0 && mauSo % i == 0) 21 | { 22 | tuSo /= i; 23 | mauSo /= i; 24 | break; 25 | } 26 | } 27 | fraction operator+(fraction x) { 28 | fraction temp; 29 | temp.tuSo = tuSo * x.mauSo + x.tuSo * mauSo; 30 | temp.mauSo = x.mauSo * mauSo; 31 | return temp; 32 | } 33 | fraction operator-(fraction x) { 34 | fraction temp; 35 | temp.tuSo = tuSo * x.mauSo - x.tuSo * mauSo; 36 | temp.mauSo = x.mauSo * mauSo; 37 | return temp; 38 | } 39 | fraction operator*(fraction x) { 40 | fraction temp; 41 | temp.tuSo = tuSo * x.tuSo; 42 | temp.mauSo = x.mauSo * mauSo; 43 | return temp; 44 | } 45 | void print() { 46 | if (tuSo % mauSo == 0) 47 | cout << tuSo / mauSo; 48 | else cout << tuSo << "/" << mauSo << " "; 49 | } 50 | }; 51 | void array_import(fraction arr[], int x); 52 | void array_print(fraction arr[], int x); 53 | void sum(fraction arr[], int x); 54 | void multiple(fraction arr[], int x); 55 | void arrange(fraction arr[], int x); 56 | void UCLN(int& x, int& y); 57 | void main() 58 | { 59 | int n; 60 | cout << "So phan tu cua mang la: "; 61 | cin >> n; 62 | fraction* a = new fraction[n]; 63 | array_import(a, n); 64 | array_print(a, n); 65 | sum(a, n); 66 | multiple(a, n); 67 | arrange(a, n); 68 | cout << endl; 69 | system("pause"); 70 | } 71 | void array_import(fraction arr[], int x) { 72 | for (int i = 0; i < x; i++) 73 | { 74 | cout << " Phan tu thu " << i + 1 << " la: " << endl; 75 | arr[i].import(); 76 | arr[i].simple(); 77 | } 78 | } 79 | void array_print(fraction arr[], int x) { 80 | cout << "Mang phan so: "; 81 | for (int i = 0; i < x; i++) 82 | arr[i].print(); 83 | } 84 | void sum(fraction arr[], int x) { 85 | fraction temp; 86 | temp.tuSo = 0; 87 | temp.mauSo = 1; 88 | for (int i = 0; i < x; i++) 89 | temp = temp + arr[i]; 90 | cout << endl << "Tong cac phan so trong mang la "; 91 | temp.simple(); 92 | temp.print(); 93 | cout << endl; 94 | } 95 | void multiple(fraction arr[], int x) { 96 | fraction temp; 97 | temp.tuSo = 1; 98 | temp.mauSo = 1; 99 | for (int i = 0; i < x; i++) 100 | temp = temp * arr[i]; 101 | cout << "Tich cac phan so trong mang la "; 102 | temp.simple(); 103 | temp.print(); 104 | cout << endl; 105 | } 106 | void arrange(fraction arr[], int x) { 107 | for (int i = 0; i < x-1; i++) 108 | for (int j = i + 1; j < x; j++) 109 | if ((arr[i] - arr[j]).tuSo < 0) 110 | { 111 | fraction temp = arr[i]; 112 | arr[i] = arr[j]; 113 | arr[j] = temp; 114 | } 115 | array_print(arr, x); 116 | } -------------------------------------------------------------------------------- /BTH3/Single Linked List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node { 5 | int number; 6 | node* link_toNext; 7 | void get_Newlink(node* x); 8 | node* import_node(int x); 9 | }; 10 | 11 | class list { 12 | node* head; 13 | node* tail; 14 | public: 15 | node* get_Head() { 16 | return head; 17 | } 18 | node* get_Tail() { 19 | return tail; 20 | } 21 | void create() { 22 | head = NULL; 23 | tail = NULL; 24 | } 25 | void print_fromHead(); 26 | void print_fromTail(node* p); 27 | void import_fromHead(node* x); 28 | void import_fromTail(node* x); 29 | void del_Head(); 30 | void del_Tail(); 31 | void del_after(node* p); 32 | void import_after(node*& pre, node* ins); 33 | }; 34 | 35 | void call_menu(); 36 | void choose(); 37 | void import_List_Head(list& l); 38 | void import_List_Tail(list& l); 39 | void quantity(list* x); 40 | void delete_node(list& l); 41 | void insert_node(list& l); 42 | void find_node(list* l); 43 | 44 | void main(){ 45 | call_menu(); 46 | choose(); 47 | system("pause"); 48 | } 49 | 50 | node* node::import_node(int x) { 51 | node* p = new node; 52 | if (p == NULL) 53 | return NULL; 54 | p->number = x; 55 | p->link_toNext = NULL; 56 | return p; 57 | } 58 | void node::get_Newlink(node* x) { 59 | this->link_toNext = x; 60 | } 61 | 62 | void list::print_fromHead() { 63 | cout << "Danh sach: "; 64 | for (node* temp = head; temp != NULL; temp = temp->link_toNext) 65 | cout << temp->number << " "; 66 | cout << endl; 67 | } 68 | void list::print_fromTail(node* p) { 69 | if (p != NULL) { 70 | print_fromTail(p->link_toNext); 71 | cout << p->number << " "; 72 | } 73 | } 74 | void list::import_fromHead(node* x) { 75 | if (head == NULL) { 76 | head = tail = x; 77 | } 78 | else { 79 | x->link_toNext = head; 80 | head = x; 81 | } 82 | } 83 | void list::import_fromTail(node* x) { 84 | if (head == NULL) { 85 | head = tail = x; 86 | } 87 | else { 88 | tail->get_Newlink(x); 89 | tail = x; 90 | } 91 | } 92 | void list::del_Head() { 93 | if (head == NULL) 94 | cout << "Danh sach khong co phan tu de xoa" << endl; 95 | else if (head == tail) { 96 | node* temp = head; 97 | delete temp; 98 | head = tail = NULL; 99 | cout << "Da xoa phan tu dau danh sach" << endl; 100 | } 101 | else { 102 | node* temp = head; 103 | head = head->link_toNext; 104 | delete temp; 105 | cout << "Da xoa phan tu dau danh sach" << endl; 106 | } 107 | } 108 | void list::del_Tail() { 109 | if (head == NULL) 110 | cout << "Danh sach rong, khong co phan tu de xoa" << endl; 111 | else if (head == tail) { 112 | delete head; 113 | head = tail = NULL; 114 | cout << "Da xoa phan tu cuoi danh sach" << endl; 115 | } 116 | else { 117 | node* temp; 118 | for (temp = head; (temp->link_toNext)->link_toNext != NULL; temp = temp->link_toNext); 119 | node* last = tail; 120 | temp->link_toNext = NULL; 121 | tail = temp; 122 | delete last; 123 | cout << "Da xoa phan tu cuoi danh sach" << endl; 124 | } 125 | } 126 | void list::del_after(node* p) { 127 | if (p != NULL && p != tail) { 128 | node* temp = p->link_toNext; 129 | p->link_toNext = temp->link_toNext; 130 | delete temp; 131 | } 132 | } 133 | void list::import_after(node*& pre, node* ins) { 134 | if (pre != NULL && pre != tail) { 135 | ins->link_toNext = pre->link_toNext; 136 | pre->link_toNext = ins; 137 | } 138 | } 139 | 140 | void call_menu() { 141 | cout << "_____________________________" << endl << endl; 142 | cout << "1. Nhap vao danh sach n phan tu bang cach them vao dau danh sach va in ra danh sach vua nhap" << endl; 143 | cout << "2. Nhap vao danh sach n phan tu bang cach them vao cuoi danh sach va in ra danh sach vua nhap" << endl; 144 | cout << "3. In ra danh sach theo chieu tu cuoi len dau" << endl; 145 | cout << "4. Dem so nut co trong danh sach lien ket" << endl; 146 | cout << "5. Tim mot nut co thanh phan du lieu nhap tu ban phim" << endl; 147 | cout << "6. Xoa nut dau danh sach" << endl; 148 | cout << "7. Xoa nut cuoi danh sach" << endl; 149 | cout << "8. Xoa mot nut co thanh phan du lieu nhap tu ban phim" << endl; 150 | cout << "9. Chen mot nut co thanh phan du lieu nhap vao tai vi tri n" << endl; 151 | cout << "_____________________________" << endl << endl; 152 | } 153 | void choose() { 154 | cout << "Lua chon cua ban la: "; 155 | int choice; 156 | cin >> choice; 157 | system("cls"); 158 | while (choice >= 11 || choice < 1) { 159 | cout << "Lua chon khong hop le, vui long nhap lai" << endl; 160 | cout << "Lua chon cua ban la: "; 161 | cin >> choice; 162 | system("cls"); 163 | } 164 | list* l = new list; 165 | l->create(); 166 | while (choice < 11 && choice >0) { 167 | switch (choice) { 168 | case 1: 169 | system("cls"); 170 | import_List_Head(*l); 171 | l->print_fromHead(); 172 | call_menu(); 173 | break; 174 | case 2: 175 | system("cls"); 176 | import_List_Tail(*l); 177 | l->print_fromHead(); 178 | call_menu(); 179 | break; 180 | case 3: 181 | system("cls"); 182 | cout << "Danh sach dao nguoc: "; 183 | l->print_fromTail(l->get_Head()); 184 | cout << endl; 185 | call_menu(); 186 | break; 187 | case 4: 188 | system("cls"); 189 | quantity(l); 190 | call_menu(); 191 | break; 192 | case 5: 193 | system("cls"); 194 | find_node(l); 195 | call_menu(); 196 | break; 197 | case 6: 198 | system("cls"); 199 | l->del_Head(); 200 | l->print_fromHead(); 201 | call_menu(); 202 | break; 203 | case 7: 204 | system("cls"); 205 | l->del_Tail(); 206 | l->print_fromHead(); 207 | call_menu(); 208 | break; 209 | case 8: 210 | system("cls"); 211 | delete_node(*l); 212 | l->print_fromHead(); 213 | call_menu(); 214 | break; 215 | case 9: 216 | system("cls"); 217 | insert_node(*l); 218 | l->print_fromHead(); 219 | call_menu(); 220 | break; 221 | default: 222 | system("cls"); 223 | cout << "Nhan phim bat ky de thoat" << endl; 224 | system("pause"); 225 | break; 226 | } 227 | cout << "Lua chon cua ban la: "; 228 | cin >> choice; 229 | } 230 | } 231 | void import_List_Head(list& l) { 232 | int size; 233 | cout << "Nhap so phan tu them vao: "; 234 | cin >> size; 235 | cout << "Nhap danh sach lien ket" << endl; 236 | node* temp = new node; 237 | for (int i = 1; i <= size; i++) { 238 | temp = new node; 239 | cout << " Phan tu thu " << i << " la: "; 240 | int n; 241 | cin >> n; 242 | temp = temp->import_node(n); 243 | l.import_fromHead(temp); 244 | } 245 | cout << endl; 246 | } 247 | void import_List_Tail(list& l) { 248 | int size; 249 | cout << "Nhap so phan tu them vao: "; 250 | cin >> size; 251 | cout << "Nhap danh sach lien ket" << endl; 252 | node* temp = new node; 253 | for (int i = 1; i <= size; i++) { 254 | temp = new node; 255 | cout << " Phan tu thu " << i << " la: "; 256 | int n; 257 | cin >> n; 258 | temp = temp->import_node(n); 259 | l.import_fromTail(temp); 260 | } 261 | cout << endl; 262 | } 263 | void quantity(list* x) { 264 | int count = 0; 265 | for (node* temp = x->get_Head(); temp != NULL; temp = temp->link_toNext) 266 | count++; 267 | cout << "So nut cua danh sach la " << count << endl; 268 | } 269 | void delete_node(list& l) { 270 | int x; 271 | cout << "Nhap so nguyen can xoa: "; 272 | cin >> x; 273 | if (l.get_Head()->number == x) { 274 | l.del_Head(); 275 | return; 276 | } 277 | if (l.get_Tail()->number == x) { 278 | l.del_Tail(); 279 | return; 280 | } 281 | node* p; 282 | for (p = l.get_Head(); p != NULL && p->link_toNext->number != x; p = p->link_toNext); 283 | if (p != NULL) { 284 | l.del_after(p); 285 | return; 286 | } 287 | cout << "Phan tu khong ton tai" << endl; 288 | } 289 | void insert_node(list& l) { 290 | cout << "So can chen la: "; 291 | int number; 292 | cin >> number; 293 | node* add = new node; 294 | add = add->import_node(number); 295 | cout << "Vi tri can chen la "; 296 | int pos; 297 | cin >> pos; 298 | int count = 0; 299 | node* p = l.get_Head(); 300 | while (p != NULL) { 301 | if (++count == pos) 302 | break; 303 | p = p->link_toNext; 304 | } 305 | if (p == NULL) { 306 | if (pos == 0) { 307 | l.import_fromHead(add); 308 | cout << endl; 309 | } 310 | else { 311 | l.import_fromTail(add); 312 | cout << endl; 313 | } 314 | } 315 | else l.import_after(p, add); 316 | } 317 | void find_node(list* l) { 318 | cout << "Nhap so can tim: "; 319 | int x; 320 | cin >> x; 321 | for (node* temp = l->get_Head(); temp != NULL; temp = temp->link_toNext) 322 | if (x == temp->number) { 323 | cout << "Phan tu " << x << " co ton tai trong danh sach" << endl; 324 | return; 325 | } 326 | cout << "Phan tu " << x << " khong ton tai trong danh sach" << endl; 327 | } -------------------------------------------------------------------------------- /BTH3/Đề bài.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/BTH3/Đề bài.pdf -------------------------------------------------------------------------------- /BTH4/Single Linked List 2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student { 6 | public: 7 | string name; 8 | string ID; 9 | int year_birth; 10 | double score; 11 | public: 12 | 13 | double return_Score() { 14 | return score; 15 | } 16 | string return_Name() { 17 | return name; 18 | } 19 | int return_Year() { 20 | return year_birth; 21 | } 22 | void student_import(); 23 | void student_export(); 24 | }; 25 | 26 | struct node { 27 | student sinhVien; 28 | node* link_toNext; 29 | double get_Score() { 30 | return sinhVien.return_Score(); 31 | } 32 | int get_Year() { 33 | return sinhVien.return_Year(); 34 | } 35 | void get_Newlink(node*); 36 | node* import_node(student); 37 | }; 38 | 39 | class list { 40 | node* head; 41 | node* tail; 42 | public: 43 | node* get_Head() { 44 | return head; 45 | } 46 | node* get_Tail() { 47 | return tail; 48 | } 49 | void create() { 50 | head = NULL; 51 | tail = NULL; 52 | } 53 | void print(); 54 | void import_fromHead(node*); 55 | void import_fromTail(node*); 56 | void del_Head(); 57 | void del_Tail(); 58 | void del_after(node* p); 59 | }; 60 | 61 | void call_menu(); 62 | void choose(); 63 | void import_List_Head(list&); 64 | void import_List_Tail(list&); 65 | void score_belowFive(list); 66 | void delete_2000(list&); 67 | void reset_info(student&, student&); 68 | void switch_info(student&, student&); 69 | void re_arrange(list&); 70 | void delete_list(list&); 71 | 72 | int main(){ 73 | call_menu(); 74 | choose(); 75 | system("pause"); 76 | } 77 | 78 | void student::student_import() { 79 | cout << " Ho va ten sinh vien: "; 80 | int temp = getchar(); 81 | getline(cin, name); 82 | cout << " Ma sinh vien: "; 83 | int temp1 = getchar(); 84 | getline(cin, ID); 85 | cout << " Nam sinh: "; 86 | cin >> year_birth; 87 | while (year_birth < 1) { 88 | cout << "Nam sinh khong hop le, hay nhap lai" << endl; 89 | cout << " Nam sinh: "; 90 | cin >> year_birth; 91 | } 92 | cout << " Diem trung binh: "; 93 | cin >> score; 94 | while (score < 0 || score >10) { 95 | cout << "Diem khong hop le, hay nhap lai" << endl; 96 | cout << " Diem trung binh: "; 97 | cin >> year_birth; 98 | } 99 | } 100 | void student::student_export() { 101 | cout << " Ho va ten sinh vien: "; 102 | cout << name << endl; 103 | cout << " Ma sinh vien: "; 104 | cout << ID << endl; 105 | cout << " Nam sinh: "; 106 | cout << year_birth << endl; 107 | cout << " Diem trung binh: "; 108 | cout << score << endl; 109 | } 110 | 111 | node* node::import_node(student x) { 112 | node* p = new node; 113 | if (p == NULL) 114 | return NULL; 115 | p->sinhVien = x; 116 | p->link_toNext = NULL; 117 | return p; 118 | } 119 | void node::get_Newlink(node* x) { 120 | this->link_toNext = x; 121 | } 122 | 123 | void list::print() { 124 | cout << "Danh sach: "; 125 | for (node* temp = head; temp != NULL; temp = temp->link_toNext) 126 | temp->sinhVien.student_export(); 127 | cout << endl; 128 | } 129 | void list::import_fromHead(node* x) { 130 | if (head == NULL) { 131 | head = tail = x; 132 | } 133 | else { 134 | x->link_toNext = head; 135 | head = x; 136 | } 137 | } 138 | void list::import_fromTail(node* x) { 139 | if (head == NULL) { 140 | head = tail = x; 141 | } 142 | else { 143 | tail->get_Newlink(x); 144 | tail = x; 145 | } 146 | } 147 | void list::del_Head() { 148 | if (head == NULL) 149 | cout << "Danh sach khong co sinh vien de xoa" << endl; 150 | else if (head == tail) { 151 | node* temp = head; 152 | delete temp; 153 | head = tail = NULL; 154 | cout << "Da xoa sinh vien dau danh sach" << endl; 155 | } 156 | else { 157 | node* temp = head; 158 | head = head->link_toNext; 159 | delete temp; 160 | cout << "Da xoa sinh vien dau danh sach" << endl; 161 | } 162 | } 163 | void list::del_Tail() { 164 | if (head == NULL) 165 | cout << "Danh sach rong, khong co sinh vien de xoa" << endl; 166 | else if (head == tail) { 167 | delete head; 168 | head = tail = NULL; 169 | cout << "Da xoa sinh vien cuoi danh sach" << endl; 170 | } 171 | else { 172 | node* temp; 173 | for (temp = head; (temp->link_toNext)->link_toNext != NULL; temp = temp->link_toNext); 174 | node* last = tail; 175 | temp->link_toNext = NULL; 176 | tail = temp; 177 | delete last; 178 | cout << "Da xoa sinh vien cuoi danh sach" << endl; 179 | } 180 | } 181 | void list::del_after(node* p) { 182 | if (p != NULL && p != tail) { 183 | node* temp = p->link_toNext; 184 | p->link_toNext = temp->link_toNext; 185 | delete temp; 186 | } 187 | } 188 | 189 | void call_menu() { 190 | cout << "_____________________________" << endl << endl; 191 | cout << "1. Nhap vao danh sach n sinh vien bang cach them vao dau danh sach va in ra danh sach vua nhap" << endl; 192 | cout << "2. Nhap vao danh sach n sinh vien bang cach them vao cuoi danh sach va in ra danh sach vua nhap" << endl; 193 | cout << "3. In ra danh sach cac sinh vien co diem trung binh nho hon 5" << endl; 194 | cout << "4. Xoa cac sinh vien co nam sinh truoc 2000" << endl; 195 | cout << "5. Sap xep danh sach theo thu tu tang dan diem trung binh" << endl; 196 | cout << "6. In ra danh sach roi xoa toan bo danh sach" << endl; 197 | cout << "_____________________________" << endl << endl; 198 | } 199 | void choose() { 200 | cout << "Lua chon cua ban la: "; 201 | int choice; 202 | cin >> choice; 203 | system("cls"); 204 | while (choice >= 7 || choice < 1) { 205 | cout << "Lua chon khong hop le, vui long nhap lai" << endl; 206 | cout << "Lua chon cua ban la: "; 207 | cin >> choice; 208 | system("cls"); 209 | } 210 | list* l = new list; 211 | l->create(); 212 | while (choice < 7 && choice >0) { 213 | switch (choice) { 214 | case 1: 215 | system("cls"); 216 | import_List_Head(*l); 217 | l->print(); 218 | call_menu(); 219 | break; 220 | case 2: 221 | system("cls"); 222 | import_List_Tail(*l); 223 | l->print(); 224 | call_menu(); 225 | break; 226 | case 3: 227 | system("cls"); 228 | score_belowFive(*l); 229 | cout << endl; 230 | call_menu(); 231 | break; 232 | case 4: 233 | system("cls"); 234 | delete_2000(*l); 235 | l->print(); 236 | call_menu(); 237 | break; 238 | case 5: 239 | system("cls"); 240 | re_arrange(*l); 241 | l->print(); 242 | call_menu(); 243 | break; 244 | case 6: 245 | system("cls"); 246 | delete_list(*l); 247 | delete l; 248 | l = NULL; 249 | call_menu(); 250 | break; 251 | default: 252 | system("cls"); 253 | cout << "Nhan phim bat ky de thoat" << endl; 254 | system("pause"); 255 | break; 256 | } 257 | cout << "Lua chon cua ban la: "; 258 | cin >> choice; 259 | } 260 | } 261 | void import_List_Head(list& l) { 262 | int size; 263 | cout << "Nhap so sinh vien can them vao: "; 264 | cin >> size; 265 | cout << "Nhap danh sach lien ket" << endl; 266 | node* temp = new node; 267 | for (int i = 1; i <= size; i++) { 268 | temp = new node; 269 | cout << " Sinh vien thu " << i << ":" << endl; 270 | student x; 271 | x.student_import(); 272 | temp = temp->import_node(x); 273 | l.import_fromHead(temp); 274 | } 275 | cout << endl; 276 | } 277 | void import_List_Tail(list& l) { 278 | int size; 279 | cout << "Nhap so sinh vien can them vao: "; 280 | cin >> size; 281 | cout << "Nhap danh sach lien ket" << endl; 282 | node* temp = new node; 283 | for (int i = 1; i <= size; i++) { 284 | temp = new node; 285 | cout << " sinh vien thu " << i << ":" << endl; 286 | student x; 287 | x.student_import(); 288 | temp = temp->import_node(x); 289 | l.import_fromTail(temp); 290 | } 291 | cout << endl; 292 | } 293 | void score_belowFive(list l) { 294 | cout << "Danh sach cac sinh vien co diem trung binh duoi 5: "; 295 | for (node* temp = l.get_Head(); temp != NULL; temp = temp->link_toNext) 296 | if (temp->get_Score() < 5) { 297 | cout << temp->sinhVien.return_Name() << endl; 298 | } 299 | } 300 | void delete_2000(list& l) { 301 | if (l.get_Head()->get_Year() < 2000) { 302 | l.del_Head(); 303 | return; 304 | } 305 | if (l.get_Tail()->get_Year() < 2000) { 306 | l.del_Tail(); 307 | return; 308 | } 309 | for (node *p = l.get_Head(); p->link_toNext->get_Year() < 2000; p = p->link_toNext) { 310 | if (p != NULL) { 311 | l.del_after(p); 312 | return; 313 | } 314 | } 315 | cout << "Sinh vien khong ton tai" << endl; 316 | } 317 | void reset_info(student& x, student& y) { 318 | x.name = y.name; 319 | x.ID = y.ID; 320 | x.year_birth = y.year_birth; 321 | x.score = y.score; 322 | } 323 | void switch_info(student& x, student& y) { 324 | student temp; 325 | temp.name = ""; 326 | temp.ID = ""; 327 | temp.year_birth = 0; 328 | temp.score = 0; 329 | reset_info(temp, x); 330 | reset_info(x, y); 331 | reset_info(y, temp); 332 | } 333 | void re_arrange(list& l) { 334 | for (node* p = l.get_Head(); p->link_toNext->link_toNext != l.get_Tail(); p = p->link_toNext) 335 | for (node* q = l.get_Head()->link_toNext; q->link_toNext != l.get_Tail(); q = q->link_toNext) { 336 | if (p->sinhVien.return_Score() > q->sinhVien.return_Score()) 337 | switch_info(p->sinhVien, q->sinhVien); 338 | } 339 | } 340 | void delete_list(list& l) { 341 | l.del_Head(); 342 | l.del_Tail(); 343 | for (node* p = l.get_Head(); p != NULL; p = p->link_toNext) { 344 | l.del_after(p); 345 | cout << "Da xoa toan bo danh sach" << endl; 346 | } 347 | } -------------------------------------------------------------------------------- /BTH4/Đề bài.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/BTH4/Đề bài.pdf -------------------------------------------------------------------------------- /BTH5/Single Linked List 3/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 10 3 | using namespace std; 4 | struct Node 5 | { 6 | int infor; 7 | Node *pNext; 8 | }; 9 | struct List 10 | { 11 | Node *pHead; 12 | Node *pTail; 13 | }; 14 | struct QueueA 15 | { 16 | int a[max]; 17 | int Front; 18 | int Rear; 19 | }; 20 | void CreateQueue(QueueA &queue); 21 | void init(List &); 22 | 23 | Node *CreateNode(int x); 24 | void EnQueue(List &queue, int x); 25 | int DeQueue(List &queue, int &x); 26 | int DeQueue(QueueA &queue, int &x); 27 | int EnQueue(QueueA &queue, int x); 28 | void output(List); 29 | void output(QueueA queue); 30 | bool isEmpty(List &queue); 31 | bool isEmpty(QueueA queue); 32 | int demSoPT(QueueA queue); 33 | int demSoPT(List queue); 34 | void deleteQueue(List &queue); 35 | void deleteQueue(QueueA &queue); 36 | int luaChonMenu(); 37 | int ChooseAorL(); 38 | void QueueList(List queue); 39 | void QueueArr(QueueA); 40 | int main() 41 | { 42 | List queueL; 43 | QueueA queueA; 44 | bool flag = true; 45 | int chosen; 46 | while (flag != false) 47 | { 48 | chosen = ChooseAorL(); 49 | switch (chosen) 50 | { 51 | case 1: 52 | { 53 | QueueArr(queueA); 54 | break; 55 | } 56 | case 2: 57 | { 58 | 59 | QueueList(queueL); 60 | break; 61 | } 62 | case 0: 63 | { 64 | cout << "BYE! \n"; 65 | flag = false; 66 | break; 67 | } 68 | default: 69 | { 70 | cout << "Lua chon khong hop le. VUi long chon lai: "; 71 | break; 72 | } 73 | } 74 | } 75 | system("pause"); 76 | 77 | return 0; 78 | } 79 | 80 | void QueueArr(QueueA queue) 81 | { 82 | CreateQueue(queue); 83 | cout << "BAN DANG LAM THAO TAC VOI QUEUE LA MANG"; 84 | int lua_chon; 85 | bool flag = true; 86 | while (flag != false) 87 | { 88 | int x; 89 | lua_chon = luaChonMenu(); 90 | switch (lua_chon) 91 | { 92 | case 1: 93 | { 94 | cout << "Nhap gia tri can them vao queue: "; 95 | cin >> x; 96 | EnQueue(queue, x); 97 | cout << "Cac phan tu cua Queue sau khi EnQueue: "; 98 | output(queue); 99 | break; 100 | } 101 | case 2: 102 | if (DeQueue(queue, x)) 103 | { 104 | cout << "Gia tri lay duoc khi dequeue la: " << x << endl; 105 | cout 106 | << "Cac phan tu cua Queue sau khi DeQueue: "; 107 | if (queue.Front != -1) 108 | { 109 | output(queue); 110 | } 111 | } 112 | break; 113 | case 3: 114 | { 115 | if (isEmpty(queue)) 116 | { 117 | cout << "Queue rong!\n"; 118 | } 119 | else 120 | { 121 | cout << "Queue co phan tu!\n"; 122 | } 123 | 124 | break; 125 | } 126 | case 4: 127 | cout << "So luong phan tu trong queue: " << demSoPT(queue); 128 | break; 129 | case 5: 130 | deleteQueue(queue); 131 | cout << "Queue is deleted!"; 132 | break; 133 | case 0: 134 | flag = false; 135 | break; 136 | default: 137 | cout << "Lua chon khong dung, vui long nhap lai.\n"; 138 | cin >> lua_chon; 139 | break; 140 | } 141 | } 142 | } 143 | void QueueList(List queue) 144 | { 145 | cout << "BAN DANG LAM THAO TAC VOI QUEUE LA LIST"; 146 | init(queue); 147 | int lua_chon; 148 | bool flag = true; 149 | while (flag != false) 150 | { 151 | int x; 152 | lua_chon = luaChonMenu(); 153 | switch (lua_chon) 154 | { 155 | case 1: 156 | { 157 | cout << "Nhap gia tri can them vao queue: "; 158 | cin >> x; 159 | EnQueue(queue, x); 160 | cout << "Cac phan tu cua Queue sau khi EnQueue: "; 161 | output(queue); 162 | break; 163 | } 164 | case 2: 165 | if (DeQueue(queue, x)) 166 | { 167 | cout << "Gia tri lay duoc khi dequeue la: " << x << endl; 168 | cout 169 | << "Cac phan tu cua Queue sau khi DeQueue: "; 170 | output(queue); 171 | } 172 | break; 173 | case 3: 174 | { 175 | if (isEmpty(queue)) 176 | { 177 | cout << "Queue rong!\n"; 178 | } 179 | else 180 | { 181 | cout << "Queue co phan tu!\n"; 182 | } 183 | 184 | break; 185 | } 186 | case 4: 187 | cout << "So luong phan tu trong queue: " << demSoPT(queue); 188 | break; 189 | case 5: 190 | deleteQueue(queue); 191 | cout << "Queue is deleted!"; 192 | break; 193 | case 0: 194 | flag = false; 195 | break; 196 | default: 197 | cout << "Lua chon khong dung, vui long nhap lai.\n"; 198 | cin >> lua_chon; 199 | break; 200 | } 201 | } 202 | } 203 | int ChooseAorL() 204 | { 205 | int chosen; 206 | cout << "\n \n ================================ \n"; 207 | cout << "\n Ban muon lam viec voi MANG hay DSLK ? "; 208 | cout << "\n 1. MANG "; 209 | cout << "\n 2. Danh sach lien ket "; 210 | cout << "\n 0. Thoat chuong trinh "; 211 | cout << "\nMoi ban nhap thao tac can thuc hien: "; 212 | cin >> chosen; 213 | return chosen; 214 | } 215 | int luaChonMenu() 216 | { 217 | int lua_chon; 218 | cout << "\n \n ================================ \n"; 219 | cout << "\n Vui long chon chuc nang tuong ung"; 220 | cout << "\n1. Enqueue "; 221 | cout 222 | << "\n2. Dequeue "; 223 | cout << "\n3. Kiem tra Queue co rong hay khong"; 224 | cout << "\n4. Dem so phan tu trong Queue"; 225 | cout << "\n5. Xoa cac phan tu trong Queue"; 226 | cout << "\n0. Thoat chuong trinh"; 227 | cout << "\n ================================"; 228 | cout << "\n Lua chon cua ban: "; 229 | cin >> lua_chon; 230 | return lua_chon; 231 | } 232 | 233 | void CreateQueue(QueueA &queue) 234 | { 235 | queue.Front = -1; 236 | queue.Rear = -1; 237 | } 238 | void init(List &queue) 239 | { 240 | queue.pHead = queue.pTail = NULL; 241 | } 242 | 243 | void deleteQueue(List &queue) 244 | { 245 | Node *p = queue.pHead; 246 | while (p != NULL) 247 | { 248 | Node *temp = p; 249 | p = p->pNext; 250 | delete temp; 251 | temp = NULL; 252 | } 253 | queue.pHead = queue.pTail = NULL; 254 | } 255 | void deleteQueue(QueueA &queue) 256 | { 257 | queue.Front = -1; 258 | queue.Rear = -1; 259 | } 260 | Node *CreateNode(int x) 261 | { 262 | Node *node = new Node; 263 | if (node == NULL) 264 | { 265 | return NULL; 266 | } 267 | node->infor = x; 268 | node->pNext = NULL; 269 | return node; 270 | } 271 | void EnQueue(List &queue, int x) 272 | { 273 | Node *node = CreateNode(x); 274 | if (queue.pHead == NULL) 275 | { 276 | queue.pHead = queue.pTail = node; 277 | } 278 | else 279 | { 280 | queue.pTail->pNext = node; 281 | queue.pTail = node; 282 | } 283 | } 284 | int EnQueue(QueueA &queue, int x) 285 | { 286 | if (queue.Rear - queue.Front + 1 == max) 287 | { 288 | cout << "Queue is full!" << endl; 289 | return 0; 290 | } 291 | else 292 | { 293 | if (isEmpty(queue)) // queue rong~ 294 | { 295 | queue.Front = 0; 296 | } 297 | 298 | if (queue.Rear == max - 1) 299 | { 300 | int f = queue.Front; 301 | int r = queue.Rear; 302 | for (int i = f; i <= r; i++) 303 | { 304 | queue.a[i - f] = queue.a[f]; 305 | } 306 | queue.Front = 0; 307 | queue.Rear = r - f; 308 | } 309 | 310 | queue.Rear++; 311 | queue.a[queue.Rear] = x; 312 | } 313 | return 1; 314 | } 315 | bool isEmpty(List &queue) 316 | { 317 | if (queue.pHead == NULL) 318 | { 319 | return true; 320 | } 321 | return false; 322 | } 323 | bool isEmpty(QueueA queue) 324 | { 325 | if (queue.Front == -1) 326 | { 327 | return true; 328 | } 329 | return false; 330 | } 331 | 332 | int DeQueue(QueueA &queue, int &x) 333 | { 334 | if (!isEmpty(queue)) 335 | { 336 | x = queue.a[queue.Front]; 337 | queue.Front++; 338 | if (queue.Front > queue.Rear) //truong hop co mot phan tu 339 | { 340 | queue.Front = -1; 341 | queue.Rear = -1; 342 | } 343 | return 1; 344 | } 345 | else 346 | { 347 | cout << "QueueA rong"; 348 | return 0; 349 | } 350 | } 351 | int DeQueue(List &queue, int &x) 352 | { 353 | if (!isEmpty(queue)) 354 | { 355 | Node *p = queue.pHead; 356 | x = p->infor; 357 | queue.pHead = p->pNext; 358 | if (queue.pHead == NULL) 359 | { 360 | queue.pTail = NULL; 361 | } 362 | delete p; 363 | p = NULL; 364 | return 1; 365 | } 366 | else 367 | { 368 | cout << "Queue is empty!"; 369 | } 370 | 371 | return 0; 372 | } 373 | 374 | int demSoPT(QueueA queue) 375 | { 376 | if (queue.Front == -1) 377 | { 378 | return 0; 379 | } 380 | 381 | return queue.Rear - queue.Front + 1; 382 | } 383 | int demSoPT(List queue) 384 | { 385 | int dem = 0; 386 | Node *p = queue.pHead; 387 | while (p != NULL) 388 | { 389 | dem++; 390 | p = p->pNext; 391 | } 392 | return dem; 393 | } 394 | void output(QueueA queue) 395 | { 396 | for (int i = queue.Front; i < queue.Rear + 1; i++) 397 | { 398 | cout << queue.a[i] << " "; 399 | } 400 | } 401 | void output(List queue) 402 | { 403 | Node *p = queue.pHead; 404 | while (p != NULL) 405 | { 406 | cout << p->infor << " "; 407 | p = p->pNext; 408 | } 409 | } 410 | -------------------------------------------------------------------------------- /BTH5/Single Linked List 3/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 10 3 | using namespace std; 4 | struct Stack 5 | { 6 | int s[max]; 7 | int top; 8 | }; 9 | struct Node 10 | { 11 | int infor; 12 | Node *pNext; 13 | }; 14 | struct List 15 | { 16 | Node *pHead; 17 | Node *pTail; 18 | }; 19 | //Menu 20 | int luaChonMenu(); 21 | int ChooseAorL(); 22 | void StackList(List stack); 23 | void StackArr(Stack stack); 24 | 25 | //List 26 | void CreateStack(List &); 27 | Node *CreateNode(int x); 28 | void push(List &stack, int); 29 | int pop(List &stack, int &); 30 | bool deleteAfter(List &stack, Node *node); 31 | bool isEmpty(List stack); 32 | void output(List); 33 | void deleteStack(List &stack); 34 | int demSoPT(List stack); 35 | 36 | //Array 37 | void CreatStack(Stack &); 38 | int pop(Stack &, int &x); 39 | int push(Stack &, int x); 40 | bool isEmpty(Stack); 41 | int demSoPT(Stack); 42 | void deleteStack(Stack &); 43 | void output(Stack); 44 | int main() 45 | { 46 | List stackL; 47 | Stack stackA; 48 | bool flag = true; 49 | int chosen; 50 | while (flag != false) 51 | { 52 | chosen = ChooseAorL(); 53 | switch (chosen) 54 | { 55 | case 1: 56 | { 57 | StackArr(stackA); 58 | break; 59 | } 60 | case 2: 61 | { 62 | 63 | StackList(stackL); 64 | break; 65 | } 66 | case 0: 67 | { 68 | cout << "BYE! \n"; 69 | flag = false; 70 | break; 71 | } 72 | default: 73 | { 74 | cout << "Lua chon khong hop le. VUi long chon lai: "; 75 | break; 76 | } 77 | } 78 | } 79 | system("pause"); 80 | return 0; 81 | } 82 | void StackArr(Stack stack) 83 | { 84 | CreatStack(stack); 85 | cout << "BAN DANG LAM THAO TAC VOI STACK LA MANG"; 86 | int lua_chon; 87 | bool flag = true; 88 | while (flag != false) 89 | { 90 | int x; 91 | lua_chon = luaChonMenu(); 92 | switch (lua_chon) 93 | { 94 | case 1: 95 | { 96 | cout << "Nhap gia tri can them vao STACK: "; 97 | cin >> x; 98 | push(stack, x); 99 | cout << "Cac phan tu cua STACK sau khi PUSH: "; 100 | output(stack); 101 | break; 102 | } 103 | case 2: 104 | if (pop(stack, x)) 105 | { 106 | cout << "Gia tri lay duoc khi POP la: " << x << endl; 107 | cout 108 | << "Cac phan tu cua STACK sau khi POP: "; 109 | output(stack); 110 | } 111 | break; 112 | case 3: 113 | { 114 | if (isEmpty(stack)) 115 | { 116 | cout << "STACK is empty!\n"; 117 | } 118 | else 119 | { 120 | cout << "STACK co phan tu!\n"; 121 | } 122 | 123 | break; 124 | } 125 | case 4: 126 | cout << "So luong phan tu trong STACK: " << demSoPT(stack); 127 | break; 128 | case 5: 129 | deleteStack(stack); 130 | break; 131 | case 0: 132 | flag = false; 133 | break; 134 | default: 135 | cout << "Lua chon khong dung, vui long nhap lai.\n"; 136 | cin >> lua_chon; 137 | break; 138 | } 139 | } 140 | } 141 | void StackList(List stack) 142 | { 143 | CreateStack(stack); 144 | cout << "BAN DANG LAM THAO TAC VOI STACK LA LIST"; 145 | int lua_chon; 146 | bool flag = true; 147 | while (flag != false) 148 | { 149 | int x; 150 | lua_chon = luaChonMenu(); 151 | switch (lua_chon) 152 | { 153 | case 1: 154 | { 155 | cout << "Nhap gia tri can them vao STACK: "; 156 | cin >> x; 157 | push(stack, x); 158 | cout << "Cac phan tu cua STACK sau khi PUSH: "; 159 | output(stack); 160 | break; 161 | } 162 | case 2: 163 | if (pop(stack, x)) 164 | { 165 | cout << "Gia tri lay duoc khi POP la: " << x << endl; 166 | cout 167 | << "Cac phan tu cua STACK sau khi POP: "; 168 | output(stack); 169 | } 170 | break; 171 | case 3: 172 | { 173 | if (isEmpty(stack)) 174 | { 175 | cout << "STACK is empty!\n"; 176 | } 177 | else 178 | { 179 | cout << "STACK co phan tu!\n"; 180 | } 181 | 182 | break; 183 | } 184 | case 4: 185 | cout << "So luong phan tu trong STACK: " << demSoPT(stack); 186 | break; 187 | case 5: 188 | deleteStack(stack); 189 | break; 190 | case 0: 191 | flag = false; 192 | break; 193 | default: 194 | cout << "Lua chon khong dung, vui long nhap lai.\n"; 195 | cin >> lua_chon; 196 | break; 197 | } 198 | } 199 | } 200 | int ChooseAorL() 201 | { 202 | int chosen; 203 | cout << "\n \n ================================ \n"; 204 | cout << "\n Ban muon lam viec voi MANG hay DSLK ? "; 205 | cout << "\n 1. MANG "; 206 | cout << "\n 2. Danh sach lien ket "; 207 | cout << "\n 0. Thoat chuong trinh "; 208 | cout << "\nMoi ban nhap thao tac can thuc hien: "; 209 | cin >> chosen; 210 | return chosen; 211 | } 212 | int luaChonMenu() 213 | { 214 | int lua_chon; 215 | cout << "\n \n ================================ \n"; 216 | cout << "\nVui long chon chuc nang tuong ung"; 217 | cout << "\n1. PUSH "; 218 | cout << "\n2. POP"; 219 | cout << "\n3. Kiem tra Stack co rong hay khong"; 220 | cout << "\n4. Dem so phan tu trong Stack"; 221 | cout << "\n5. Xoa cac phan tu trong Stack"; 222 | cout << "\n0. Thoat chuong trinh"; 223 | cout << "\n ================================"; 224 | cout << "\n Lua chon cua ban: "; 225 | cin >> lua_chon; 226 | return lua_chon; 227 | } 228 | 229 | // ARRAY 230 | void CreatStack(Stack &stack) 231 | { 232 | stack.top = -1; 233 | } 234 | void output(Stack stack) 235 | { 236 | for (int i = 0; i <= stack.top; i++) 237 | { 238 | cout << stack.s[i] << " "; 239 | } 240 | } 241 | int demSoPT(Stack stack) 242 | { 243 | if (stack.top == -1) 244 | { 245 | return 0; 246 | } 247 | 248 | return stack.top + 1; 249 | } 250 | int pop(Stack &stack, int &x) 251 | { 252 | if (!isEmpty(stack)) 253 | { 254 | x = stack.s[stack.top]; 255 | stack.top--; 256 | return 1; 257 | } 258 | else 259 | { 260 | cout << "Stack rong"; 261 | return 0; 262 | } 263 | } 264 | int push(Stack &stack, int x) 265 | { 266 | if (stack.top == max - 1) 267 | { 268 | cout << "STACK is full!" << endl; 269 | return 0; 270 | } 271 | else 272 | { 273 | stack.s[++stack.top] = x; 274 | } 275 | return 1; 276 | } 277 | void deleteStack(Stack &stack) 278 | { 279 | stack.top = -1; 280 | } 281 | bool isEmpty(Stack stack) 282 | { 283 | if (stack.top == -1) 284 | { 285 | return true; 286 | } 287 | return false; 288 | } 289 | 290 | //List 291 | void CreateStack(List &stack) 292 | { 293 | stack.pHead = stack.pTail = NULL; 294 | } 295 | Node *CreateNode(int x) 296 | { 297 | Node *node = new Node; 298 | if (node == NULL) 299 | { 300 | return NULL; 301 | } 302 | node->infor = x; 303 | node->pNext = NULL; 304 | return node; 305 | } 306 | void push(List &stack, int x) 307 | { 308 | Node *node = CreateNode(x); 309 | if (stack.pHead == NULL) 310 | { 311 | stack.pHead = stack.pTail = node; 312 | } 313 | else 314 | { 315 | stack.pTail->pNext = node; 316 | stack.pTail = node; 317 | } 318 | } 319 | int pop(List &stack, int &x) 320 | { 321 | Node *p = stack.pHead; 322 | if (p == NULL) 323 | { 324 | cout << "Stack is empty! You can't pop"; 325 | return 0; 326 | } 327 | if (p->pNext == NULL) 328 | { 329 | stack.pHead = stack.pTail = NULL; 330 | delete p; 331 | p = NULL; 332 | return 1; 333 | } 334 | 335 | while (true) 336 | { 337 | if (p->pNext == stack.pTail) 338 | { 339 | deleteAfter(stack, p); 340 | return 1; 341 | } 342 | p = p->pNext; 343 | } 344 | return 0; 345 | } 346 | bool deleteAfter(List &stack, Node *node) 347 | { 348 | if (node != NULL) 349 | { 350 | Node *p = node->pNext; 351 | 352 | if (p != NULL) 353 | { 354 | node->pNext = p->pNext; 355 | if (p == stack.pTail) 356 | { 357 | stack.pTail = node; 358 | } 359 | delete p; 360 | p = NULL; 361 | } 362 | return true; 363 | } 364 | return false; 365 | } 366 | int demSoPT(List stack) 367 | { 368 | int dem = 0; 369 | Node *p = stack.pHead; 370 | while (p != NULL) 371 | { 372 | dem++; 373 | p = p->pNext; 374 | } 375 | return dem; 376 | } 377 | bool isEmpty(List stack) 378 | { 379 | if (stack.pHead == NULL) 380 | { 381 | return true; 382 | } 383 | return false; 384 | } 385 | void deleteStack(List &stack) 386 | { 387 | Node *p = stack.pHead; 388 | while (p != NULL) 389 | { 390 | Node *temp = p; 391 | p = p->pNext; 392 | delete temp; 393 | temp = NULL; 394 | } 395 | stack.pHead = stack.pTail = NULL; 396 | cout << "Stack is deleted!\n"; 397 | } 398 | void output(List stack) 399 | { 400 | Node *p = stack.pHead; 401 | while (p != NULL) 402 | { 403 | cout << p->infor << " "; 404 | p = p->pNext; 405 | } 406 | } -------------------------------------------------------------------------------- /BTH5/Single Linked List 3/Single Linked List 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/BTH5/Single Linked List 3/Single Linked List 3.pdf -------------------------------------------------------------------------------- /BTH5/Stack and Queue/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int infor; 6 | Node *pNext; 7 | }; 8 | struct List 9 | { 10 | Node *pHead; 11 | Node *pTail; 12 | }; 13 | 14 | 15 | Node *createNode(int x); 16 | void push(List &list, Node *node); 17 | int pop(List &list, int &x); 18 | void init(List &l); 19 | void Xuat(List list); 20 | 21 | void ChuyenNhiPhan(int n, List &list); 22 | bool isEmpty(List list); 23 | int main() 24 | { 25 | List list; 26 | init(list); 27 | cout << "Nhap so can chuyen tu co so 10 sang co so 2:"; 28 | int n; 29 | cin >> n; 30 | ChuyenNhiPhan(n, list); 31 | cout << endl; 32 | while (!isEmpty(list)) 33 | { 34 | int x; 35 | pop(list, x); 36 | cout << x << " "; 37 | } 38 | 39 | return 0; 40 | } 41 | Node *createNode(int x) 42 | { 43 | Node *node = new Node; 44 | if (node == NULL) 45 | { 46 | return NULL; 47 | } 48 | 49 | node->infor = x; 50 | node->pNext = NULL; 51 | return node; 52 | } 53 | void push(List &list, Node *node) 54 | { 55 | if (list.pHead == NULL) 56 | { 57 | list.pHead = list.pTail = node; 58 | return; 59 | } 60 | node->pNext = list.pHead; 61 | list.pHead = node; 62 | } 63 | int pop(List &list, int &x) 64 | { 65 | if (list.pHead == NULL) 66 | { 67 | return 0; 68 | } 69 | 70 | Node *p = list.pHead; 71 | x = p->infor; 72 | list.pHead = p->pNext; 73 | if (list.pHead == NULL) 74 | { 75 | list.pTail = NULL; 76 | } 77 | delete p; 78 | p = NULL; 79 | return 1; 80 | } 81 | void init(List &l) 82 | { 83 | l.pHead = l.pTail = NULL; 84 | } 85 | void Xuat(List list) 86 | { 87 | Node *p = list.pHead; 88 | while (p != NULL) 89 | { 90 | cout << p->infor << " "; 91 | p = p->pNext; 92 | } 93 | } 94 | 95 | void ChuyenNhiPhan(int n, List &list) 96 | { 97 | while (n != 0) 98 | { 99 | Node *p = createNode(n % 2); 100 | push(list, p); 101 | n /= 2; 102 | } 103 | } 104 | bool isEmpty(List list) 105 | { 106 | if (list.pHead == NULL) 107 | { 108 | return true; 109 | } 110 | return false; 111 | } -------------------------------------------------------------------------------- /BTH5/Stack and Queue/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Customer 4 | { 5 | string name; 6 | int seatNumber; 7 | Customer *pNext; 8 | }; 9 | 10 | struct ListCustomer 11 | { 12 | Customer *pHead; 13 | Customer *pTail; 14 | }; 15 | 16 | struct Seat 17 | { 18 | int number; 19 | Seat *pNext; 20 | }; 21 | struct ListSeat 22 | { 23 | int quantity; 24 | Seat *pHead; 25 | Seat *pTail; 26 | }; 27 | 28 | struct SoThuTu 29 | { 30 | int stt; 31 | SoThuTu *pNext; 32 | }; 33 | struct List 34 | { 35 | SoThuTu *pHead; 36 | SoThuTu *pTail; 37 | }; 38 | 39 | int luaChonMenu(); 40 | 41 | //A Danh sach ghe 42 | void addSeat(ListSeat &listSeat, Seat *seat); 43 | void init(ListSeat &); 44 | Seat *CreateSeat(int x); 45 | void output(ListSeat); 46 | int demSoPT(ListSeat listSeat); 47 | void deleteSeat(ListSeat &listSeat); 48 | bool deleteX(ListSeat &listSeat, int x); 49 | bool deleteAfter(ListSeat &listSeat, Seat *seat); 50 | bool isEmpty(ListSeat); 51 | 52 | //B Hang doi chua so thu tu cua khach 53 | void init(List &); 54 | SoThuTu *CreateSoThuTu(int x); 55 | void EnQueue(List &queue); 56 | int DeQueue(List &queue); 57 | void output(List); 58 | bool isEmpty(List queue); 59 | 60 | //C Danh sach chua thong tin khac da mua ve 61 | void init(ListCustomer &); 62 | Customer *CreateCustomer(); 63 | void addCustomer(ListCustomer &listCustomer, Customer *); 64 | void output(ListCustomer); 65 | void deleteCustomer(ListCustomer &listCustomer); 66 | bool deleteX(ListCustomer &listCustomer, int x); 67 | bool deleteAfter(ListCustomer &listCustomer, Customer *); 68 | 69 | int main() 70 | { 71 | ListSeat listSeat; 72 | List queue; 73 | ListCustomer listCustomer; 74 | init(queue); 75 | init(listSeat); 76 | init(listCustomer); 77 | 78 | int lua_chon; 79 | bool flag = true; 80 | while (flag != false) 81 | { 82 | lua_chon = luaChonMenu(); 83 | switch (lua_chon) 84 | { 85 | case 1: 86 | { 87 | EnQueue(queue); 88 | output(queue); 89 | cout << "\n"; 90 | break; 91 | } 92 | case 2: 93 | { 94 | if (!isEmpty(queue)) 95 | { 96 | if (!isEmpty(listSeat)) 97 | { 98 | DeQueue(queue); 99 | cout << "Danh sach ghe con trong: "; 100 | output(listSeat); 101 | Customer *cust = CreateCustomer(); 102 | addCustomer(listCustomer,cust); 103 | deleteX(listSeat, cust->seatNumber); 104 | } 105 | else 106 | { 107 | cout << "\nKhong co ghe nao trong"; 108 | } 109 | } 110 | else 111 | { 112 | cout << "\nKhong co khach hang nao xep hang!"; 113 | } 114 | 115 | break; 116 | } 117 | case 3:{ 118 | 119 | break; 120 | } 121 | case 4: 122 | { 123 | cout<<"Danh sach khang hang da dat ve:\n"; 124 | output(listCustomer); 125 | break; 126 | } 127 | case 0: 128 | { 129 | flag = false; 130 | break; 131 | } 132 | default: 133 | cout << "Lua chon nhap khong dung! Vui long nhap lai: "; 134 | cin >> lua_chon; 135 | break; 136 | } 137 | } 138 | return 0; 139 | } 140 | 141 | int luaChonMenu() 142 | { 143 | int lua_chon; 144 | cout << "\n \n ================================ \n"; 145 | cout << "\n Vui long chon chuc nang tuong ung."; 146 | cout << "\n 1. Lay so xep hang."; 147 | cout 148 | << "\n 2. Mua ve."; 149 | cout 150 | << "\n 3. Huy ve."; 151 | cout << "\n 4. Hien thi thong tin nhung ve da ban."; 152 | cout << "\n 0. Thoat chuong trinh"; 153 | cout << "\n ================================"; 154 | cout << "\n Lua chon cua ban: "; 155 | cin >> lua_chon; 156 | return lua_chon; 157 | } 158 | //Danh sach ghe A 159 | void init(ListSeat &listSeat) 160 | { 161 | listSeat.pHead = listSeat.pTail = NULL; 162 | cout << "Nhap so ghe :"; 163 | cin >> listSeat.quantity; 164 | for (int i = 0; i < listSeat.quantity; i++) 165 | { 166 | Seat *seat = CreateSeat(i + 1); 167 | addSeat(listSeat, seat); 168 | } 169 | } 170 | Seat *CreateSeat(int x) 171 | { 172 | Seat *seat = new Seat; 173 | if (seat == NULL) 174 | { 175 | return NULL; 176 | } 177 | seat->number = x; 178 | seat->pNext = NULL; 179 | return seat; 180 | } 181 | void addSeat(ListSeat &listSeat, Seat *seat) 182 | { 183 | if (listSeat.pHead == NULL) 184 | { 185 | listSeat.pHead = listSeat.pTail = seat; 186 | } 187 | else 188 | { 189 | listSeat.pTail->pNext = seat; 190 | listSeat.pTail = seat; 191 | } 192 | } 193 | void deleteSeat(ListSeat &listSeat) 194 | { 195 | if (listSeat.pHead == NULL) 196 | { 197 | return; 198 | } 199 | Seat *p = listSeat.pHead; 200 | listSeat.pHead = p->pNext; 201 | if (listSeat.pHead == NULL) 202 | { 203 | listSeat.pTail = NULL; 204 | } 205 | delete p; 206 | p = NULL; 207 | } 208 | 209 | int demSoPT(ListSeat listSeat) 210 | { 211 | int dem = 0; 212 | Seat *p = listSeat.pHead; 213 | while (p != NULL) 214 | { 215 | dem++; 216 | p = p->pNext; 217 | } 218 | return dem; 219 | } 220 | void output(ListSeat listSeat) 221 | { 222 | Seat *p = listSeat.pHead; 223 | while (p != NULL) 224 | { 225 | cout << p->number << " "; 226 | p = p->pNext; 227 | } 228 | } 229 | bool deleteX(ListSeat &listSeat, int x) 230 | { 231 | Seat *p = listSeat.pHead; 232 | Seat *prev = NULL; 233 | while (true) 234 | { 235 | if (p == NULL) 236 | { 237 | cout << "Khong co thanh phan du lieu can xoa trong danh sach\n"; 238 | return false; 239 | } 240 | 241 | if (p->number == x) 242 | { 243 | if (prev == NULL) 244 | { 245 | deleteSeat(listSeat); 246 | } 247 | else 248 | { 249 | deleteAfter(listSeat, prev); 250 | } 251 | return true; 252 | } 253 | prev = p; 254 | p = p->pNext; 255 | } 256 | } 257 | bool deleteAfter(ListSeat &listSeat, Seat *seat) 258 | { 259 | if (seat != NULL) 260 | { 261 | Seat *p = seat->pNext; 262 | 263 | if (p != NULL) 264 | { 265 | seat->pNext = p->pNext; 266 | if (p == listSeat.pTail) 267 | { 268 | listSeat.pTail = seat; 269 | } 270 | delete p; 271 | p = NULL; 272 | } 273 | return true; 274 | } 275 | return false; 276 | } 277 | bool isEmpty(ListSeat listSeat) 278 | { 279 | if (listSeat.pHead == NULL) 280 | { 281 | return true; 282 | } 283 | return false; 284 | } 285 | 286 | //Hang doi B 287 | void init(List &queue) 288 | { 289 | queue.pHead = queue.pTail = NULL; 290 | } 291 | 292 | SoThuTu *CreateSoThuTu(int x) 293 | { 294 | SoThuTu *soThuTu = new SoThuTu; 295 | if (soThuTu == NULL) 296 | { 297 | return NULL; 298 | } 299 | soThuTu->stt = x; 300 | soThuTu->pNext = NULL; 301 | return soThuTu; 302 | } 303 | void EnQueue(List &queue) 304 | { 305 | if (isEmpty(queue)) 306 | { 307 | SoThuTu *soThuTu = CreateSoThuTu(1); 308 | queue.pHead = queue.pTail = soThuTu; 309 | } 310 | else 311 | { 312 | int x = queue.pTail->stt; 313 | SoThuTu *soThuTu = CreateSoThuTu(x + 1); 314 | queue.pTail->pNext = soThuTu; 315 | queue.pTail = soThuTu; 316 | } 317 | } 318 | 319 | bool isEmpty(List queue) 320 | { 321 | if (queue.pHead == NULL) 322 | { 323 | return true; 324 | } 325 | return false; 326 | } 327 | 328 | int DeQueue(List &queue) 329 | { 330 | if (!isEmpty(queue)) 331 | { 332 | SoThuTu *p = queue.pHead; 333 | queue.pHead = p->pNext; 334 | if (queue.pHead == NULL) 335 | { 336 | queue.pTail = NULL; 337 | } 338 | delete p; 339 | p = NULL; 340 | return 1; 341 | } 342 | else 343 | { 344 | cout << "Queue is empty!"; 345 | } 346 | 347 | return 0; 348 | } 349 | void output(List queue) 350 | { 351 | SoThuTu *p = queue.pHead; 352 | while (p != NULL) 353 | { 354 | cout << p->stt << " "; 355 | p = p->pNext; 356 | } 357 | } 358 | 359 | // Danh sach C chua thong tin khac dat ve 360 | void init(ListCustomer &listCustomer) 361 | { 362 | listCustomer.pHead = listCustomer.pTail = NULL; 363 | } 364 | Customer *CreateCustomer() 365 | { 366 | Customer *cust = new Customer; 367 | 368 | if (cust == NULL) 369 | { 370 | return NULL; 371 | } 372 | cout << "\nNhap so ghe khach muon chon: "; 373 | cin >> cust->seatNumber; 374 | cout << "\nNhap ten khach hang: "; 375 | cin.ignore(); 376 | getline(cin, cust->name); 377 | cust->pNext = NULL; 378 | return cust; 379 | } 380 | 381 | void addCustomer(ListCustomer &listCustomer, Customer *cust) 382 | { 383 | if (listCustomer.pHead == NULL) 384 | { 385 | listCustomer.pHead = listCustomer.pTail = cust; 386 | } 387 | else 388 | { 389 | listCustomer.pTail->pNext = cust; 390 | listCustomer.pTail = cust; 391 | } 392 | } 393 | void deleteCustomer(ListCustomer &listCustomer) 394 | { 395 | if (listCustomer.pHead == NULL) 396 | { 397 | return; 398 | } 399 | Customer *p = listCustomer.pHead; 400 | listCustomer.pHead = p->pNext; 401 | if (listCustomer.pHead == NULL) 402 | { 403 | listCustomer.pTail = NULL; 404 | } 405 | delete p; 406 | p = NULL; 407 | } 408 | void output(ListCustomer listCustomer) 409 | { 410 | Customer *p = listCustomer.pHead; 411 | while (p != NULL) 412 | { 413 | cout <<"So ghe: "<< p->seatNumber << " Name:" << p->name << endl; 414 | p = p->pNext; 415 | } 416 | } 417 | bool deleteX(ListCustomer &listCustomer, int x) 418 | { 419 | Customer *p = listCustomer.pHead; 420 | Customer *prev = NULL; 421 | while (true) 422 | { 423 | if (p == NULL) 424 | { 425 | cout << "Khong co thanh phan du lieu can xoa trong danh sach\n"; 426 | return false; 427 | } 428 | 429 | if (p->seatNumber == x) 430 | { 431 | if (prev == NULL) 432 | { 433 | deleteCustomer(listCustomer); 434 | } 435 | else 436 | { 437 | deleteAfter(listCustomer, prev); 438 | } 439 | return true; 440 | } 441 | prev = p; 442 | p = p->pNext; 443 | } 444 | } 445 | bool deleteAfter(ListCustomer &listCustomer, Customer *cust) 446 | { 447 | if (cust != NULL) 448 | { 449 | Customer *p = cust->pNext; 450 | 451 | if (p != NULL) 452 | { 453 | cust->pNext = p->pNext; 454 | if (p == listCustomer.pTail) 455 | { 456 | listCustomer.pTail = cust; 457 | } 458 | delete p; 459 | p = NULL; 460 | } 461 | return true; 462 | } 463 | return false; 464 | } 465 | -------------------------------------------------------------------------------- /BTH5/Stack and Queue/Stack and Queue.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/BTH5/Stack and Queue/Stack and Queue.pdf -------------------------------------------------------------------------------- /BTH6/Single Linked List 4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student { 6 | public: 7 | string name; 8 | string ID; 9 | int year_birth; 10 | double score; 11 | public: 12 | double return_Score() { 13 | return score; 14 | } 15 | string return_Name() { 16 | return name; 17 | } 18 | int return_Year() { 19 | return year_birth; 20 | } 21 | void student_import(); 22 | void student_export(); 23 | }; 24 | 25 | struct node { 26 | student sinhVien; 27 | node* link_toNext; 28 | double get_Score() { 29 | return sinhVien.return_Score(); 30 | } 31 | int get_Year() { 32 | return sinhVien.return_Year(); 33 | } 34 | void get_Newlink(node*); 35 | node* import_node(student); 36 | }; 37 | 38 | class list { 39 | node* head; 40 | node* tail; 41 | public: 42 | node* get_Head() { 43 | return head; 44 | } 45 | node* get_Tail() { 46 | return tail; 47 | } 48 | void create() { 49 | head = NULL; 50 | tail = NULL; 51 | } 52 | void print(); 53 | void import_fromHead(node*); 54 | void import_fromTail(node*); 55 | void del_Head(); 56 | void del_Tail(); 57 | void del_after(node* p); 58 | }; 59 | 60 | void call_menu(); 61 | void choose(); 62 | void import_List_Head(list&); 63 | void import_List_Tail(list&); 64 | void score_belowFive(list); 65 | void delete_2000(list&); 66 | void re_arrange(list&); 67 | void delete_list(list&); 68 | 69 | int main() { 70 | call_menu(); 71 | choose(); 72 | system("pause"); 73 | } 74 | 75 | void student::student_import() { 76 | cout << " Ho va ten sinh vien: "; 77 | int temp = getchar(); 78 | getline(cin, name); 79 | cout << " Ma sinh vien: "; 80 | int temp1 = getchar(); 81 | getline(cin, ID); 82 | cout << " Nam sinh: "; 83 | cin >> year_birth; 84 | while (year_birth < 1) { 85 | cout << "Nam sinh khong hop le, hay nhap lai" << endl; 86 | cout << " Nam sinh: "; 87 | cin >> year_birth; 88 | } 89 | cout << " Diem trung binh: "; 90 | cin >> score; 91 | while (score < 0 || score >10) { 92 | cout << "Diem khong hop le, hay nhap lai" << endl; 93 | cout << " Diem trung binh: "; 94 | cin >> score; 95 | } 96 | } 97 | void student::student_export() { 98 | cout << " Ho va ten sinh vien: "; 99 | cout << name << endl; 100 | cout << " Ma sinh vien: "; 101 | cout << ID << endl; 102 | cout << " Nam sinh: "; 103 | cout << year_birth << endl; 104 | cout << " Diem trung binh: "; 105 | cout << score << endl; 106 | } 107 | 108 | node* node::import_node(student x) { 109 | node* p = new node; 110 | if (p == NULL) 111 | return NULL; 112 | p->sinhVien = x; 113 | p->link_toNext = NULL; 114 | return p; 115 | } 116 | void node::get_Newlink(node* x) { 117 | this->link_toNext = x; 118 | } 119 | 120 | void list::print() { 121 | cout << "Danh sach: " << endl; 122 | for (node* temp = head; temp != NULL; temp = temp->link_toNext) 123 | temp->sinhVien.student_export(); 124 | cout << endl; 125 | } 126 | void list::import_fromHead(node* x) { 127 | if (head == NULL) { 128 | head = tail = x; 129 | } 130 | else { 131 | x->link_toNext = head; 132 | head = x; 133 | } 134 | } 135 | void list::import_fromTail(node* x) { 136 | if (head == NULL) { 137 | head = tail = x; 138 | } 139 | else { 140 | tail->get_Newlink(x); 141 | tail = x; 142 | } 143 | } 144 | void list::del_Head() { 145 | if (head == NULL) 146 | cout << "Danh sach khong co sinh vien de xoa" << endl; 147 | else if (head == tail) { 148 | node* temp = head; 149 | delete temp; 150 | head = tail = NULL; 151 | cout << "Da xoa sinh vien dau danh sach" << endl; 152 | } 153 | else { 154 | node* temp = head; 155 | head = head->link_toNext; 156 | delete temp; 157 | cout << "Da xoa sinh vien dau danh sach" << endl; 158 | } 159 | } 160 | void list::del_Tail() { 161 | if (head == NULL) 162 | cout << "Danh sach rong, khong co sinh vien de xoa" << endl; 163 | else if (head == tail) { 164 | delete head; 165 | head = tail = NULL; 166 | cout << "Da xoa sinh vien cuoi danh sach" << endl; 167 | } 168 | else { 169 | node* temp; 170 | for (temp = head; (temp->link_toNext)->link_toNext != NULL; temp = temp->link_toNext); 171 | node* last = tail; 172 | temp->link_toNext = NULL; 173 | tail = temp; 174 | delete last; 175 | cout << "Da xoa sinh vien cuoi danh sach" << endl; 176 | } 177 | } 178 | void list::del_after(node* p) { 179 | if (p != NULL && p != tail) { 180 | node* temp = p->link_toNext; 181 | p->link_toNext = temp->link_toNext; 182 | delete temp; 183 | } 184 | } 185 | 186 | void call_menu() { 187 | cout << "_____________________________" << endl << endl; 188 | cout << "1. Nhap vao danh sach n sinh vien bang cach them vao dau danh sach va in ra danh sach vua nhap" << endl; 189 | cout << "2. Nhap vao danh sach n sinh vien bang cach them vao cuoi danh sach va in ra danh sach vua nhap" << endl; 190 | cout << "3. In ra danh sach cac sinh vien co diem trung binh nho hon 5" << endl; 191 | cout << "4. Xoa cac sinh vien co nam sinh truoc 2000" << endl; 192 | cout << "5. Sap xep danh sach theo thu tu tang dan diem trung binh" << endl; 193 | cout << "6. In ra danh sach roi xoa toan bo danh sach" << endl; 194 | cout << "_____________________________" << endl << endl; 195 | } 196 | void choose() { 197 | cout << "Lua chon cua ban la: "; 198 | int choice; 199 | cin >> choice; 200 | system("cls"); 201 | while (choice >= 7 || choice < 1) { 202 | cout << "Lua chon khong hop le, vui long nhap lai" << endl; 203 | cout << "Lua chon cua ban la: "; 204 | cin >> choice; 205 | system("cls"); 206 | } 207 | list* l = new list; 208 | l->create(); 209 | while (choice < 7 && choice >0) { 210 | switch (choice) { 211 | case 1: 212 | system("cls"); 213 | import_List_Head(*l); 214 | l->print(); 215 | call_menu(); 216 | break; 217 | case 2: 218 | system("cls"); 219 | import_List_Tail(*l); 220 | l->print(); 221 | call_menu(); 222 | break; 223 | case 3: 224 | system("cls"); 225 | score_belowFive(*l); 226 | cout << endl; 227 | call_menu(); 228 | break; 229 | case 4: 230 | system("cls"); 231 | delete_2000(*l); 232 | l->print(); 233 | call_menu(); 234 | break; 235 | case 5: 236 | system("cls"); 237 | re_arrange(*l); 238 | l->print(); 239 | call_menu(); 240 | break; 241 | case 6: 242 | system("cls"); 243 | delete_list(*l); 244 | delete l; 245 | l = NULL; 246 | call_menu(); 247 | break; 248 | default: 249 | system("cls"); 250 | cout << "Nhan phim bat ky de thoat" << endl; 251 | system("pause"); 252 | break; 253 | } 254 | cout << "Lua chon cua ban la: "; 255 | cin >> choice; 256 | } 257 | } 258 | void import_List_Head(list& l) { 259 | int size; 260 | cout << "Nhap so sinh vien can them vao: "; 261 | cin >> size; 262 | cout << "Nhap danh sach lien ket" << endl; 263 | node* temp = new node; 264 | for (int i = 1; i <= size; i++) { 265 | temp = new node; 266 | cout << " Sinh vien thu " << i << ":" << endl; 267 | student x; 268 | x.student_import(); 269 | temp = temp->import_node(x); 270 | l.import_fromHead(temp); 271 | } 272 | cout << endl; 273 | } 274 | void import_List_Tail(list& l) { 275 | int size; 276 | cout << "Nhap so sinh vien can them vao: "; 277 | cin >> size; 278 | cout << "Nhap danh sach lien ket" << endl; 279 | node* temp = new node; 280 | for (int i = 1; i <= size; i++) { 281 | temp = new node; 282 | cout << " sinh vien thu " << i << ":" << endl; 283 | student x; 284 | x.student_import(); 285 | temp = temp->import_node(x); 286 | l.import_fromTail(temp); 287 | } 288 | cout << endl; 289 | } 290 | void score_belowFive(list l) { 291 | cout << "Danh sach cac sinh vien co diem trung binh duoi 5: "; 292 | for (node* temp = l.get_Head(); temp != NULL; temp = temp->link_toNext) 293 | if (temp->get_Score() < 5) { 294 | cout << temp->sinhVien.return_Name() << endl; 295 | } 296 | } 297 | void delete_2000(list& l) { 298 | if (l.get_Head()->get_Year() < 2000) { 299 | l.del_Head(); 300 | return; 301 | } 302 | if (l.get_Tail()->get_Year() < 2000) { 303 | l.del_Tail(); 304 | return; 305 | } 306 | for (node* p = l.get_Head(); p->link_toNext->get_Year() < 2000; p = p->link_toNext) { 307 | if (p != NULL) { 308 | l.del_after(p); 309 | return; 310 | } 311 | } 312 | cout << "Sinh vien khong ton tai" << endl; 313 | } 314 | void re_arrange(list& l) { 315 | for (node *p = l.get_Head(); p != NULL; p = p->link_toNext) 316 | for (node *q = p->link_toNext; q!= NULL; q = q->link_toNext) { 317 | if (p->sinhVien.return_Score() >= q->sinhVien.return_Score()) 318 | swap(p->sinhVien, q->sinhVien); 319 | } 320 | } 321 | void delete_list(list& l) { 322 | for (node* p = l.get_Head(); p != NULL; p = p->link_toNext) { 323 | l.del_after(p); 324 | cout << "Da xoa toan bo danh sach" << endl; 325 | } 326 | l.del_Head(); 327 | } -------------------------------------------------------------------------------- /BTH6/Đề bài.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/BTH6/Đề bài.pdf -------------------------------------------------------------------------------- /BTH7/Sorting Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int random(int, int); 6 | int* generator_random(int); 7 | 8 | void array_Generate(int[], int); 9 | void array_Generate_Increase(int[], int); 10 | void array_Export(int[], int); 11 | void GenerateArray_and_Find(); 12 | void GenerateArray_and_Search(); 13 | void print_pair(int[], int); 14 | 15 | int maxValue(int[], int); 16 | int indexOf_maxValue(int[], int); 17 | void indexesOf_maxValue(int[], int); 18 | 19 | int IndexOf_X(int[], int, int); 20 | int IndexOf_X_Improved(int[], int, int); 21 | void IndexesOf_X(int[], int, int); 22 | void find_IndexOf_X(int[], int, int); 23 | void find_IndexOf_X_improved(int[], int, int); 24 | void find_IndexsOf_X(int[], int, int); 25 | 26 | int InterPolationSearch(int [], int, int); 27 | void find_IndexOf_X_with_InterPolationSearch(int[], int, int); 28 | void find_IndexsOf_X_with_InterPolationSearch(int[], int, int); 29 | 30 | void Interchange_Sort(int[], int); 31 | void Selection_Sort(int[], int); 32 | 33 | void choose(int[], int); 34 | 35 | int main(){ 36 | int n; 37 | cout << "So luong phan tu la "; 38 | cin >> n; 39 | int* array = new int[n]; 40 | array_Generate(array, n); 41 | choose(array, n); 42 | } 43 | 44 | void choose(int arr[], int size) { 45 | cout << "_______________________________" << endl; 46 | cout << "Nhap lua chon tuong ung theo de bai trong LAB 5" << endl; 47 | cout << "Lua chon cua ban la: "; 48 | int choice, x; 49 | cin >> choice; 50 | switch (choice) { 51 | case 1: 52 | system("cls"); 53 | array_Export(arr, size); 54 | choose(arr, size); 55 | break; 56 | case 2: 57 | system("cls"); 58 | array_Export(arr, size); 59 | cout << "Gia tri lon nhat trong mang la " << maxValue(arr, size) << endl; 60 | choose(arr, size); 61 | break; 62 | case 3: 63 | system("cls"); 64 | array_Export(arr, size); 65 | cout << "Vi tri chua gia tri lon nhat trong mang la " << indexOf_maxValue(arr, size) << endl; 66 | choose(arr, size); 67 | break; 68 | case 4: 69 | system("cls"); 70 | array_Export(arr, size); 71 | indexesOf_maxValue(arr, size); 72 | choose(arr, size); 73 | break; 74 | case 5: 75 | system("cls"); 76 | array_Export(arr, size); 77 | cout << "Gia tri X can tim la "; 78 | cin >> x; 79 | find_IndexOf_X(arr, size, x); 80 | choose(arr, size); 81 | break; 82 | case 6: 83 | system("cls"); 84 | array_Export(arr, size); 85 | cout << "Gia tri X can tim la "; 86 | cin >> x; 87 | find_IndexOf_X_improved(arr, size, x); 88 | choose(arr, size); 89 | break; 90 | case 7: 91 | system("cls"); 92 | array_Export(arr, size); 93 | cout << "Gia tri X can tim la "; 94 | cin >> x; 95 | find_IndexsOf_X(arr, size, x); 96 | choose(arr, size); 97 | break; 98 | case 8: 99 | system("cls"); 100 | GenerateArray_and_Find(); 101 | choose(arr, size); 102 | break; 103 | case 9: 104 | system("cls"); 105 | GenerateArray_and_Search(); 106 | choose(arr, size); 107 | break; 108 | case 10: 109 | system("cls"); 110 | print_pair(arr, size); 111 | choose(arr, size); 112 | break; 113 | case 11: 114 | system("cls"); 115 | Interchange_Sort(arr, size); 116 | choose(arr, size); 117 | break; 118 | case 12: 119 | system("cls"); 120 | Selection_Sort(arr, size); 121 | choose(arr, size); 122 | break; 123 | default: 124 | system("cls"); 125 | cout << "Lua chon khong hop le, hay nhap lai" << endl; 126 | choose(arr, size); 127 | break; 128 | } 129 | } 130 | 131 | int random(int min, int max) { 132 | static bool first = true; 133 | if (first) { 134 | srand(time(NULL)); 135 | first = false; 136 | } 137 | return min + rand() % ((max + 1) - min); 138 | } 139 | int* generator_random(int n) { 140 | int* arr = new int[n]; 141 | if (arr != NULL) 142 | for (int i = 0; i < n; i++) 143 | *(arr + i) = random(-1000, 1000); 144 | return arr; 145 | } 146 | 147 | void array_Generate(int arr[], int size) { 148 | for (int i = 0; i < size; i++) 149 | arr[i] = random(-10000, 10000); 150 | } 151 | void array_Generate_Increase(int arr[], int size) { 152 | srand(time(NULL)); 153 | int i = 0; 154 | if (size > 0) { 155 | arr[i] = rand() % 100; 156 | for (int i = 1; i < size; i++) 157 | arr[i] = arr[i - 1] + rand() % 100; 158 | } 159 | } 160 | void array_Export(int arr[], int size) { 161 | for (int i = 0; i < size; i++) 162 | cout << arr[i] << " "; 163 | cout << endl; 164 | cout << "_______________________________" << endl; 165 | } 166 | void GenerateArray_and_Find() { 167 | int n; 168 | cout << "So luong phan tu la "; 169 | cin >> n; 170 | int* array = new int[n]; 171 | array_Generate_Increase(array, n); 172 | system("cls"); 173 | array_Export(array, n); 174 | int x; 175 | cout << "Gia tri x can tim la "; 176 | cin >> x; 177 | cout << "Tim kiem tuyen tinh: " << endl << " "; 178 | find_IndexOf_X(array, n, x); 179 | cout << "_______" << endl; 180 | cout << "Tim kiem tuyen tinh cai tien: " << endl << " "; 181 | find_IndexOf_X_improved(array, n, x); 182 | cout << "_______" << endl; 183 | cout << "Tim tat ca cac vi tri cua x: " << endl << " "; 184 | find_IndexsOf_X(array, n, x); 185 | 186 | } 187 | void GenerateArray_and_Search() { 188 | int n; 189 | cout << "So luong phan tu la "; 190 | cin >> n; 191 | int* array = new int[n]; 192 | array_Generate_Increase(array, n); 193 | system("cls"); 194 | array_Export(array, n); 195 | int x; 196 | cout << "Gia tri x can tim la "; 197 | cin >> x; 198 | InterPolationSearch(array, n, x); 199 | cout << "_______" << endl; 200 | find_IndexOf_X_with_InterPolationSearch(array, n, x); 201 | cout << "_______" << endl; 202 | find_IndexsOf_X_with_InterPolationSearch(array, n, x); 203 | } 204 | void print_pair(int arr[], int size) { 205 | for (int i = 0; i < size - 1; i++) { 206 | for (int j = i + 1; j < size; j++) 207 | cout << "(" << arr[i] << "," << arr[j] << ") "; 208 | cout << endl; 209 | } 210 | } 211 | 212 | int maxValue(int arr[], int size) { 213 | int max = arr[0]; 214 | for (int i = 1; i < size; i++) 215 | if (arr[i] > max) 216 | max = arr[i]; 217 | return max; 218 | } 219 | int indexOf_maxValue(int arr[], int size) { 220 | for (int i = 0; i < size; i++) 221 | if (arr[i] == maxValue(arr, size)) 222 | return i; 223 | } 224 | void indexesOf_maxValue(int arr[], int size) { 225 | system("cls"); 226 | cout << "Cac vi tri chua gia tri lon nhat gom: "; 227 | for (int i = 0; i < size; i++) 228 | if (arr[i] == maxValue(arr, size)) 229 | cout << i << " "; 230 | cout << endl; 231 | } 232 | 233 | int IndexOf_X(int arr[], int size, int key) { 234 | for (int i = 0; i < size; i++) 235 | if (arr[i] == key) 236 | return i; 237 | return -1; 238 | } 239 | int IndexOf_X_Improved(int arr[], int size, int key) { 240 | arr[size] = key; 241 | int i; 242 | for (i = 0; arr[i] != key; i++); 243 | if (i < size) 244 | return i; 245 | return -1; 246 | } 247 | void IndexesOf_X(int arr[], int size, int key) { 248 | for (int i = 0; i < size; i++) 249 | if (arr[i] == key) 250 | cout << i << " "; 251 | } 252 | void find_IndexOf_X(int arr[], int size, int key) { 253 | clock_t begin, end; 254 | begin = clock(); 255 | if (IndexOf_X(arr, size, key) == -1) 256 | cout << "Gia tri " << key << " khong ton tai trong mang" << endl; 257 | else cout << "Gia tri x nam tai vi tri " << IndexOf_X(arr, size, key) << endl; 258 | end = clock(); 259 | cout << "Tong thoi gian thuc hien la " << (double)(end - begin) / CLOCKS_PER_SEC << " giay" << endl; 260 | } 261 | void find_IndexOf_X_improved(int arr[], int size, int key) { 262 | clock_t begin, end; 263 | begin = clock(); 264 | if (IndexOf_X_Improved(arr, size, key) == -1) 265 | cout << "Gia tri " << key << " khong ton tai trong mang" << endl; 266 | else cout << "Gia tri x nam tai vi tri " << IndexOf_X_Improved(arr, size, key) << endl; 267 | end = clock(); 268 | cout << "Tong thoi gian thuc hien la " << (double)(end - begin) / CLOCKS_PER_SEC << " giay" << endl; 269 | } 270 | void find_IndexsOf_X(int arr[], int size, int key) { 271 | if (IndexOf_X(arr, size, key) == -1) 272 | cout << "Gia tri x khong ton tai trong mang" << endl; 273 | else { 274 | cout << "Gia tri x nam tai vi tri "; 275 | IndexesOf_X(arr, size, key); 276 | cout << endl; 277 | } 278 | } 279 | 280 | int InterPolationSearch(int arr[], int size, int key) { 281 | int left = 0, right = size - 1; 282 | while (left <= right && key >= arr[left] && key <= arr[right]) { 283 | double mid1 = (double)(key - arr[left]) / (arr[right] - arr[left]); 284 | int mid2 = (right - left); 285 | int search_index = left + mid1 * mid2; 286 | if (arr[search_index] == key) 287 | return search_index; 288 | if (arr[search_index] < key) 289 | left = search_index + 1; 290 | else right = search_index - 1; 291 | } 292 | return -1; 293 | } 294 | void find_IndexOf_X_with_InterPolationSearch(int arr[], int size, int key) { 295 | clock_t begin, end; 296 | begin = clock(); 297 | if (InterPolationSearch(arr, size, key) == -1) 298 | cout << "Gia tri " << key << " khong ton tai trong mang" << endl; 299 | else cout << "Gia tri x nam tai vi tri " << InterPolationSearch(arr, size, key) << endl; 300 | end = clock(); 301 | cout << "Tong thoi gian thuc hien la " << (double)(end - begin) / CLOCKS_PER_SEC << " giay" << endl; 302 | }; 303 | void find_IndexsOf_X_with_InterPolationSearch(int arr[], int size, int key) { 304 | if (InterPolationSearch(arr, size, key) == -1) 305 | cout << "Gia tri x khong ton tai trong mang" << endl; 306 | else { 307 | cout << "Gia tri x nam tai vi tri "; 308 | IndexesOf_X(arr, size, key); 309 | cout << endl; 310 | } 311 | } 312 | 313 | void Interchange_Sort(int arr[], int size) { 314 | for (int i = 0; i < size - 1; i++) 315 | for (int j = i + 1; j < size; j++) 316 | if (arr[i] > arr[j]) { 317 | int temp = arr[i]; 318 | arr[i] = arr[j]; 319 | arr[j] = temp; 320 | } 321 | cout << "Mang sau khi sap xep la: "; 322 | array_Export(arr, size); 323 | } 324 | void Selection_Sort(int arr[], int size) { 325 | int i, j, min_index; 326 | for (i = 0; i < size - 1; i++){ 327 | min_index = i; 328 | for (j = i + 1; j < size; j++) 329 | if (arr[j] < arr[min_index]) 330 | min_index = j; 331 | int temp = arr[i]; 332 | arr[i] = arr[min_index]; 333 | arr[min_index] = temp; 334 | } 335 | cout << "Mang sau khi sap xep la: "; 336 | array_Export(arr, size); 337 | } -------------------------------------------------------------------------------- /BTH7/Đề bài.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/BTH7/Đề bài.pdf -------------------------------------------------------------------------------- /BTH8/Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct node { 6 | int value; 7 | node* leftBranch; 8 | node* rightBranch; 9 | node(int value) { 10 | this->value = value; 11 | leftBranch = rightBranch = NULL; 12 | } 13 | }; 14 | 15 | void add(node*&, int); 16 | void importTree(node*&, int&); 17 | 18 | void print_RLN(node*); 19 | void print_NLR(node*); 20 | void print_NRL(node*); 21 | void print_LRN(node*); 22 | void print_LNR(node*); 23 | void print_RNL(node*); 24 | void printTree(node*); 25 | 26 | bool isAvailable(node*, int); 27 | void findX(node*); 28 | 29 | node* minNode(node*); 30 | void deleteNode(node*&, int); 31 | void deleteX(node*&); 32 | 33 | int countNode(node*); 34 | int countNode_2branch(node*); 35 | int countNode_1branch(node*); 36 | int countNode_rightBranch(node*); 37 | int countNode_noBranch(node*); 38 | 39 | int height(node*); 40 | 41 | void find_evenNode(node* root); 42 | 43 | int amount_greaterThanX(node*, int); 44 | void find_greaterThanX(node*); 45 | 46 | int maxValue(node*); 47 | 48 | int maxValue_rightBranch(node*); 49 | 50 | void printNode_level(node*); 51 | 52 | node* Node(node*, int); 53 | int heightX(node*, int); 54 | void findPath_X(node*); 55 | 56 | void menu(); 57 | void choose(node*&, int&); 58 | 59 | int main() { 60 | node* tree = NULL; 61 | int n; 62 | choose(tree, n); 63 | system("pause"); 64 | } 65 | 66 | void add(node*& root, int value) { 67 | if (root == NULL) 68 | root = new node(value); 69 | else if (value < root->value) 70 | add(root->leftBranch, value); 71 | else if (root->value < value) 72 | add(root->rightBranch, value); 73 | } 74 | void importTree(node*& TREE, int& size) { 75 | cout << "So phan tu muon them la "; 76 | cin >> size; 77 | system("cls"); 78 | int addValue; 79 | for (int i = 0; i < size; i++) { 80 | cout << "Nhap cac phan tu!" << endl; 81 | cout << "Phan tu them vao thu " << i + 1 << ": "; 82 | cin >> addValue; 83 | add(TREE, addValue); 84 | system("cls"); 85 | } 86 | } 87 | 88 | void print_RLN(node* root) { 89 | if (root) { 90 | print_RLN(root->rightBranch); 91 | print_RLN(root->leftBranch); 92 | cout << root->value << " "; 93 | } 94 | } 95 | void print_NLR(node* root) { 96 | if (root) { 97 | cout << root->value << " "; 98 | print_NLR(root->leftBranch); 99 | print_NLR(root->rightBranch); 100 | } 101 | } 102 | void print_NRL(node* root) { 103 | if (root) { 104 | cout << root->value << " "; 105 | print_NRL(root->rightBranch); 106 | print_NRL(root->leftBranch); 107 | } 108 | } 109 | void print_LRN(node* root) { 110 | if (root) { 111 | print_LRN(root->leftBranch); 112 | print_LRN(root->rightBranch); 113 | cout << root->value << " "; 114 | } 115 | } 116 | void print_LNR(node* root) { 117 | if (root) { 118 | print_LNR(root->leftBranch); 119 | cout << root->value << " "; 120 | print_LNR(root->rightBranch); 121 | } 122 | } 123 | void print_RNL(node* root) { 124 | if (root) { 125 | print_LNR(root->rightBranch); 126 | cout << root->value << " "; 127 | print_LNR(root->leftBranch); 128 | } 129 | } 130 | void printTree(node* root) { 131 | cout << "RLN: "; 132 | print_RLN(root); 133 | cout << "\nLRN: "; 134 | print_LRN(root); 135 | cout << "\nNLR: "; 136 | print_NLR(root); 137 | cout << "\nNRL: "; 138 | print_NRL(root); 139 | cout << "\nRNL: "; 140 | print_RNL(root); 141 | cout << "\nLNR: "; 142 | print_LNR(root); 143 | cout << endl; 144 | } 145 | 146 | bool isAvailable(node* root, int value) { 147 | if (root == NULL) 148 | return false; 149 | if (root->value == value) 150 | return true; 151 | if (value < root->value) 152 | return isAvailable(root->leftBranch, value); 153 | else if (root->value < value) 154 | return isAvailable(root->rightBranch, value); 155 | return false; 156 | } 157 | void findX(node* root) { 158 | int x; 159 | cout << "Gia tri can tim la "; 160 | cin >> x; 161 | if (isAvailable(root, x)) 162 | cout << "Gia tri " << x << " co ton tai trong cay" << endl; 163 | else cout << "Gia tri " << x << " khong ton tai trong cay" << endl; 164 | } 165 | 166 | node* minNode(node* root) { 167 | while (root->leftBranch != NULL) 168 | root = root->leftBranch; 169 | return root; 170 | } 171 | void deleteNode(node*& root, int value) { 172 | if (root == NULL || isAvailable(root, value) == false) { 173 | cout << "Khong ton tai node can xoa" << endl; 174 | return; 175 | } 176 | if (value > root->value) 177 | deleteNode(root->rightBranch, value); 178 | else if (value < root->value) 179 | deleteNode(root->leftBranch, value); 180 | else { 181 | if (root->leftBranch == NULL && root->rightBranch == NULL) { 182 | delete root; 183 | root = NULL; 184 | cout << "Xoa thanh cong" << endl; 185 | } 186 | else if (root->leftBranch == NULL && root->rightBranch != NULL) { 187 | node* temp = root; 188 | root = root->rightBranch; 189 | delete temp; 190 | temp = NULL; 191 | } 192 | else if (root->rightBranch == NULL && root->leftBranch != NULL) { 193 | node* temp = root; 194 | root = root->leftBranch; 195 | delete temp; 196 | temp = NULL; 197 | cout << "Xoa thanh cong" << endl; 198 | } 199 | else { 200 | node* temp = minNode(root->rightBranch); 201 | swap(root->value, temp->value); 202 | deleteNode(root->rightBranch, value); 203 | } 204 | } 205 | } 206 | void deleteX(node*& root) { 207 | int x; 208 | cout << "Gia tri can xoa la "; 209 | cin >> x; 210 | deleteNode(root, x); 211 | } 212 | 213 | int countNode(node* root) { 214 | if (root == NULL) 215 | return 0; 216 | int a = countNode(root->leftBranch); 217 | int b = countNode(root->rightBranch); 218 | return a+ b + 1; 219 | } 220 | int countNode_2branch(node* root) { 221 | if (root == NULL) 222 | return 0; 223 | int a = countNode_2branch(root->leftBranch); 224 | int b = countNode_2branch(root->rightBranch); 225 | if (root->leftBranch != NULL && root->rightBranch != NULL) 226 | return a + b + 1; 227 | return a + b; 228 | } 229 | int countNode_1branch(node* root) { 230 | if (root == NULL) 231 | return 0; 232 | int a = countNode_1branch(root->leftBranch); 233 | int b = countNode_1branch(root->rightBranch); 234 | if ((root->leftBranch != NULL && root->rightBranch == NULL) || (root->leftBranch == NULL && root->rightBranch != NULL)) 235 | return a + b + 1; 236 | return a + b; 237 | } 238 | int countNode_rightBranch(node* root) { 239 | if (root == NULL) 240 | return 0; 241 | int a = countNode_rightBranch(root->leftBranch); 242 | int b = countNode_rightBranch(root->rightBranch); 243 | if (root->leftBranch == NULL && root->rightBranch != NULL) 244 | return a + b + 1; 245 | return a + b; 246 | } 247 | int countNode_noBranch(node* root) { 248 | if (root == NULL) 249 | return 0; 250 | int a = countNode_noBranch(root->leftBranch); 251 | int b = countNode_noBranch(root->rightBranch); 252 | if (root->leftBranch == NULL && root->rightBranch == NULL) 253 | return 1; 254 | return a + b; 255 | } 256 | 257 | int height(node* root) { 258 | if (root == NULL) 259 | return 0; 260 | int a = height(root->leftBranch); 261 | int b = height(root->rightBranch); 262 | return max(a, b) + 1; 263 | } 264 | 265 | void find_evenNode(node* root) { 266 | if (root != NULL) { 267 | if (root->value % 2 == 0) 268 | cout << root->value << " "; 269 | find_evenNode(root->leftBranch); 270 | find_evenNode(root->rightBranch); 271 | } 272 | } 273 | 274 | int amount_greaterThanX(node* root, int value) { 275 | if (root == NULL) 276 | return 0; 277 | int a = amount_greaterThanX(root->leftBranch, value); 278 | int b = amount_greaterThanX(root->rightBranch, value); 279 | if (root->value > value) 280 | return a + b + 1; 281 | return a + b; 282 | } 283 | void find_greaterThanX(node* root) { 284 | int x; 285 | cout << "Gia tri X la "; 286 | cin >> x; 287 | cout << "So node co gia tri lon hon " << x << " la " << amount_greaterThanX(root, x) << endl; 288 | } 289 | 290 | int maxValue(node* root) { 291 | if (root == NULL) 292 | return 0; 293 | int max = root->value; 294 | int a = maxValue(root->leftBranch); 295 | int b = maxValue(root->rightBranch); 296 | if (a > max) 297 | max = a; 298 | if (b > max) 299 | max = b; 300 | return max; 301 | } 302 | 303 | int maxValue_rightBranch(node* root) { 304 | if (root == NULL) 305 | return 0; 306 | int min = root->value; 307 | int a = maxValue(root->rightBranch); 308 | if (a < min) 309 | min = a; 310 | return min; 311 | } 312 | 313 | void printNode_level(node* root) { 314 | if (root == NULL) 315 | return; 316 | queue q; 317 | q.push(root); 318 | while (!q.empty()) { 319 | int size = q.size(); 320 | int n = size; 321 | while (n--) { 322 | node* Node = q.front(); 323 | q.pop(); 324 | if (n == size - 1 || n == 0) 325 | cout << Node->value << " "; 326 | if (Node->leftBranch != NULL) 327 | q.push(Node->leftBranch); 328 | if (Node->rightBranch != NULL) 329 | q.push(Node->rightBranch); 330 | } 331 | cout << endl; 332 | } 333 | } 334 | 335 | node* Node(node* root, int value) { 336 | if (root == NULL) 337 | return NULL; 338 | if (root->value == value) 339 | return root; 340 | if (value < root->value) 341 | return Node(root->leftBranch, value); 342 | else if (root->value < value) 343 | return Node(root->rightBranch, value); 344 | return NULL; 345 | } 346 | int heightX(node* tree, int data) { 347 | if (tree != NULL && tree->value != data) { 348 | if (tree->value > data) { 349 | return heightX(tree->leftBranch, data) + 1; 350 | } 351 | else return heightX(tree->rightBranch, data) + 1; 352 | } 353 | return 0; 354 | } 355 | void findPath_X(node* root) { 356 | int x; 357 | cout << "Gia tri X la "; 358 | cin >> x; 359 | if (Node(root, x)) 360 | cout << "Do dai duong di ngan nhat tu goc den node co gia tri " << x << " la " << heightX(root, x) << endl; 361 | else cout << "Duong di khong ton tai" << endl; 362 | } 363 | 364 | void menu() { 365 | cout << "==============================================================" << endl; 366 | cout << " (1) Nhap vao cay nhi phan va in ra cay theo moi thu tu" << endl; 367 | cout << " (2) Tim mot node tren cay" << endl; 368 | cout << " (3) Xoa mot node tren cay" << endl; 369 | cout << " (4) Tinh tong so node tren cay" << endl; 370 | cout << " (5) Tinh tong so node co hai cay con" << endl; 371 | cout << " (6) Tinh tong so node co mot cay con" << endl; 372 | cout << " (7) Tinh tong so node chi co cay con phai" << endl; 373 | cout << " (8) Tinh tong so node la tren cay" << endl; 374 | cout << " (9) Tinh chieu cao cua cay" << endl; 375 | cout << " (10) In ra cac node co gia tri chan" << endl; 376 | cout << " (11) Tim so node co gia tri lon hon gia tri X nhap tu ban phim" << endl; 377 | cout << " (12) Tim gia tri cua node co gia tri lon nhat tren cay" << endl; 378 | cout << " (13) Tim gia tri co key nho nhat cua cay con phai" << endl; 379 | cout << " (14) In ra cac node tren tung muc cua cay" << endl; 380 | cout << " (15) Tim do dai duong di tu goc den node co gia tri X" << endl; 381 | cout << " (16) Thoat chuong trinh" << endl; 382 | cout << "==============================================================" << endl; 383 | } 384 | void choose(node*& tree, int& size) { 385 | system("cls"); 386 | int choice; 387 | menu(); 388 | cout << " Lua chon cua ban la: "; 389 | cin >> choice; 390 | switch (choice) { 391 | case 1: 392 | system("cls"); 393 | importTree(tree, size); 394 | system("cls"); 395 | printTree(tree); 396 | break; 397 | case 2: 398 | system("cls"); 399 | findX(tree); 400 | break; 401 | case 3: 402 | system("cls"); 403 | deleteX(tree); 404 | printTree(tree); 405 | break; 406 | case 4: 407 | system("cls"); 408 | cout << "Tong so node tren cay la " << countNode(tree) << endl; 409 | break; 410 | case 5: 411 | system("cls"); 412 | cout << "Tong so node co day du hai cay con tren cay la " << countNode_2branch(tree) << endl; 413 | break; 414 | case 6: 415 | system("cls"); 416 | cout << "Tong so node chi co mot cay con tren cay la " << countNode_1branch(tree) << endl; 417 | break; 418 | case 7: 419 | system("cls"); 420 | cout << "Tong so node chi co mot cay con phai tren cay la " << countNode_rightBranch(tree) << endl; 421 | break; 422 | case 8: 423 | system("cls"); 424 | cout << "Tong so la tren cay la " << countNode_noBranch(tree) << endl; 425 | break; 426 | case 9: 427 | system("cls"); 428 | cout << "Chieu cao cua cay la " << height(tree) - 1 << endl; 429 | break; 430 | case 10: 431 | system("cls"); 432 | cout << "Cac node mang gia tri chan tren cay la "; 433 | if (tree == NULL) 434 | cout << "khong co" << endl; 435 | else find_evenNode(tree); 436 | cout << endl; 437 | break; 438 | case 11: 439 | system("cls"); 440 | find_greaterThanX(tree); 441 | break; 442 | case 12: 443 | system("cls"); 444 | cout << "Gia tri lon nhat tren cay la " << maxValue(tree) << endl; 445 | break; 446 | case 13: 447 | system("cls"); 448 | cout << "Gia tri lon nhat tren cay con phai la " << maxValue_rightBranch(tree) << endl; 449 | break; 450 | case 14: 451 | system("cls"); 452 | printNode_level(tree); 453 | break; 454 | case 15: 455 | system("cls"); 456 | findPath_X(tree); 457 | break; 458 | case 16: 459 | return; 460 | default: 461 | choose(tree, size); 462 | break; 463 | } 464 | system("pause"); 465 | choose(tree, size); 466 | } -------------------------------------------------------------------------------- /BTH8/Đề bài.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/BTH8/Đề bài.pdf -------------------------------------------------------------------------------- /BTH9/Thêm/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node { 5 | int value; 6 | node* leftBranch; 7 | node* rightBranch; 8 | node(int value) { 9 | this->value = value; 10 | leftBranch = rightBranch = NULL; 11 | } 12 | }; 13 | 14 | void addNode(node*&, int); 15 | void importTree(node*&, int&); 16 | 17 | void print_NLR(node*); 18 | 19 | void findNode_1branch(node*); 20 | 21 | bool isAvailable(node*, int); 22 | void findX(node*); 23 | 24 | bool isPrime(int); 25 | void findPrime(node*); 26 | 27 | void copyTree_toArray(node*, int[], int&); 28 | void selectionSort(int[], int); 29 | void copySort(node*); 30 | 31 | void menu(); 32 | void choose(node*& tree); 33 | 34 | int main() { 35 | int n; 36 | node* tree = NULL; 37 | importTree(tree, n); 38 | choose(tree); 39 | } 40 | 41 | void addNode(node*& root, int value) { 42 | if (root == NULL) 43 | root = new node(value); 44 | else if (value < root->value) 45 | addNode(root->leftBranch, value); 46 | else if (root->value < value) 47 | addNode(root->rightBranch, value); 48 | } 49 | void importTree(node*& tree, int& size) { 50 | cout << "So phan tu muon them la "; 51 | cin >> size; 52 | system("cls"); 53 | int addElement; 54 | for (int i = 0; i < size; i++) { 55 | cout << "Phan tu thu " << i + 1 << " la: "; 56 | cin >> addElement; 57 | addNode(tree, addElement); 58 | system("cls"); 59 | } 60 | } 61 | 62 | void print_NLR(node* tree) { 63 | if (tree == NULL) 64 | return; 65 | cout << tree->value << " "; 66 | print_NLR(tree->leftBranch); 67 | print_NLR(tree->rightBranch); 68 | } 69 | 70 | void findNode_1branch(node* tree) { 71 | if (tree == NULL) 72 | return; 73 | if ((tree->leftBranch == NULL && tree->rightBranch != NULL) || (tree->leftBranch != NULL && tree->rightBranch == NULL)) 74 | cout << tree->value << " "; 75 | findNode_1branch(tree->leftBranch); 76 | findNode_1branch(tree->rightBranch); 77 | } 78 | 79 | bool isAvailable(node* tree, int x) { 80 | if (tree == NULL) 81 | return false; 82 | if (tree->value == x) 83 | return true; 84 | return isAvailable(tree->leftBranch, x) || isAvailable(tree->rightBranch, x); 85 | } 86 | void findX(node* tree) { 87 | int x; 88 | cout << "Gia tri can tim la "; 89 | cin >> x; 90 | system("cls"); 91 | if (isAvailable(tree, x)) 92 | cout << "Gia tri " << x << " co ton tai tren cay" << endl; 93 | else cout << "Gia tri " << x << " khong ton tai tren cay" << endl; 94 | } 95 | 96 | bool isPrime(int x) { 97 | if (x < 2) 98 | return false; 99 | for (int i = 2; i <= sqrt(x); i++) 100 | if (x % i == 0) 101 | return false; 102 | return true; 103 | } 104 | void findPrime(node* tree) { 105 | if (tree == NULL) 106 | return; 107 | if (isPrime(tree->value)) 108 | cout << tree->value << " "; 109 | findPrime(tree->leftBranch); 110 | findPrime(tree->rightBranch); 111 | } 112 | 113 | void copyTree_toArray(node* tree, int arr[], int& ArraySize) { 114 | if (tree == NULL) 115 | return; 116 | copyTree_toArray(tree->leftBranch, arr, ArraySize); 117 | copyTree_toArray(tree->rightBranch, arr, ArraySize); 118 | arr[ArraySize] = tree->value; 119 | ArraySize++; 120 | } 121 | void selectionSort(int arr[], int size) { 122 | int minIndex; 123 | for (int i = 0; i < size - 1; i++) { 124 | minIndex = i; 125 | for (int j = i + 1; j < size; j++) 126 | if (arr[minIndex] > arr[j]) 127 | minIndex = j; 128 | swap(arr[minIndex], arr[i]); 129 | } 130 | } 131 | void copySort(node* tree) { 132 | int size = 0; 133 | int arr[1000]; 134 | copyTree_toArray(tree, arr, size); 135 | selectionSort(arr, size); 136 | cout << "Mang sau khi duoc sap xep la: "; 137 | for (int i = 0; i < size; i++) 138 | cout << arr[i] << " "; 139 | cout << endl; 140 | } 141 | 142 | void menu() { 143 | cout << "================================================================================" << endl; 144 | cout << " (1) In ra cay theo phep duyet NLR" << endl; 145 | cout << " (2) In ra gia tri cac nut co mot cay con" << endl; 146 | cout << " (3) Kiem tra gia tri X co ton tai tren cay khong" << endl; 147 | cout << " (4) In ra cac so nguyen to tren cay" << endl; 148 | cout << " (5) Luu cac gia tri tren cay vao mang roi sap xep lai bang Selection Sort" << endl; 149 | cout << "================================================================================" << endl; 150 | } 151 | void choose(node*& tree) { 152 | menu(); 153 | int choice; 154 | cout << " Lua chon cua ban la: "; 155 | cin >> choice; 156 | switch (choice) { 157 | case 1: 158 | system("cls"); 159 | cout << "Cay theo phep duyet NLR la: "; 160 | print_NLR(tree); 161 | cout << endl; 162 | choose(tree); 163 | break; 164 | case 2: 165 | system("cls"); 166 | cout << "Cac nut chi co mot cay con la: "; 167 | findNode_1branch(tree); 168 | cout << endl; 169 | choose(tree); 170 | break; 171 | case 3: 172 | system("cls"); 173 | findX(tree); 174 | choose(tree); 175 | break; 176 | case 4: 177 | system("cls"); 178 | cout << "Cac so nguyen to tren cay la: "; 179 | findPrime(tree); 180 | cout << endl; 181 | choose(tree); 182 | break; 183 | case 5: 184 | system("cls"); 185 | copySort(tree); 186 | choose(tree); 187 | break; 188 | default: 189 | system("cls"); 190 | cout << " LUA CHON KHONG HOP LE" << endl; 191 | choose(tree); 192 | break; 193 | } 194 | } -------------------------------------------------------------------------------- /BTH9/Thêm/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student { 6 | long ID; 7 | string lastName, firstName; 8 | double score; 9 | public: 10 | bool isID(); 11 | bool isScore(); 12 | double getScore() { 13 | return score; 14 | } 15 | 16 | void studentImport(); 17 | void studentExport(); 18 | }; 19 | 20 | struct node { 21 | student value; 22 | node* leftBranch; 23 | node* rightBranch; 24 | node(student element) { 25 | value = element; 26 | leftBranch = rightBranch = NULL; 27 | } 28 | }; 29 | 30 | void addNode(node*&, student); 31 | void importTree(node*&, int&); 32 | void exportTree(node*); 33 | 34 | node* minNode(node*); 35 | void deleteNode(node*&); 36 | 37 | void menu(); 38 | void choose(node*&, int&); 39 | 40 | int main(){ 41 | int n; 42 | node* tree = NULL; 43 | choose(tree, n); 44 | } 45 | 46 | bool student::isID() { 47 | if (ID < 0) 48 | return false; 49 | return true; 50 | } 51 | bool student::isScore() { 52 | if (score < 0 || score>10) 53 | return false; 54 | return true; 55 | } 56 | void student::studentImport() { 57 | cout << "Ma sinh vien: "; 58 | cin >> ID; 59 | while (!isID()) { 60 | cout << "Ma sinh vien khong hop le, ma sinh vien moi la: "; 61 | cin >> ID; 62 | } 63 | int temp1 = getchar(); 64 | cout << "Ho cua sinh vien: "; 65 | getline(cin, firstName); 66 | cout << "Ten cua sinh vien: "; 67 | getline(cin, lastName); 68 | cout << "Diem trung binh cua sinh vien: "; 69 | cin >> score; 70 | while (!isScore()) { 71 | cout << "Diem trung binh khong hop le, diem trung binh moi la: "; 72 | cin >> score; 73 | } 74 | } 75 | void student::studentExport() { 76 | cout << "Ma sinh vien: " << ID << endl; 77 | cout << "Ho cua sinh vien: " << firstName << endl; 78 | cout << "Ten cua sinh vien: " << lastName << endl; 79 | cout << "Diem trung binh cua sinh vien: " << score << endl; 80 | } 81 | 82 | void addNode(node*& root, student addElement) { 83 | if (root == NULL) 84 | root = new node(addElement); 85 | else if (root->value.getScore() < addElement.getScore()) 86 | addNode(root->rightBranch, addElement); 87 | else if (root->value.getScore() > addElement.getScore()) 88 | addNode(root->leftBranch, addElement); 89 | } 90 | void importTree(node*& root, int& size) { 91 | cout << "So luong sinh vien nhap vao la: "; 92 | cin >> size; 93 | student* addElement; 94 | system("cls"); 95 | for (int i = 0; i < size; i++) { 96 | cout << " SINH VIEN THU " << i + 1 << endl; 97 | addElement = new student; 98 | addElement->studentImport(); 99 | addNode(root, *addElement); 100 | system("cls"); 101 | } 102 | } 103 | void exportTree(node* root) { 104 | if (root == NULL) 105 | return; 106 | root->value.studentExport(); 107 | cout << endl; 108 | exportTree(root->leftBranch); 109 | exportTree(root->rightBranch); 110 | } 111 | 112 | node* minNode(node* root) { 113 | while (root->leftBranch != NULL) 114 | root = root->leftBranch; 115 | return root; 116 | } 117 | void deleteNode(node*& root) { 118 | if (root == NULL) 119 | return; 120 | deleteNode(root->leftBranch); 121 | deleteNode(root->rightBranch); 122 | if (root->value.getScore() < 5) { 123 | if (root->leftBranch == NULL && root->rightBranch == NULL) { 124 | delete root; 125 | root = NULL; 126 | } 127 | else if (root->leftBranch != NULL && root->rightBranch == NULL) { 128 | node* temp = root; 129 | root = root->leftBranch; 130 | delete temp; 131 | temp = NULL; 132 | } 133 | else if (root->leftBranch == NULL && root->rightBranch != NULL) { 134 | node* temp = root; 135 | root = root->rightBranch; 136 | delete temp; 137 | temp = NULL; 138 | } 139 | else { 140 | node* temp = minNode(root->rightBranch); 141 | swap(root->value, temp->value); 142 | deleteNode(root->rightBranch); 143 | } 144 | return; 145 | } 146 | } 147 | 148 | void menu() { 149 | cout << "====================================================" << endl; 150 | cout << " (1) Them n sinh vien vao cay" << endl; 151 | cout << " (2) In ra cay theo thu tu duyet tien tu" << endl; 152 | cout << " (3) Xoa cac sinh vien co diem trung binh duoi 5" << endl; 153 | cout << "====================================================" << endl; 154 | } 155 | void choose(node*& root, int& size) { 156 | int choice; 157 | menu(); 158 | cout << " Lua chon cua ban la: "; 159 | cin >> choice; 160 | switch (choice) { 161 | case 1: 162 | system("cls"); 163 | importTree(root, size); 164 | choose(root, size); 165 | break; 166 | case 2: 167 | system("cls"); 168 | exportTree(root); 169 | choose(root, size); 170 | break; 171 | case 3: 172 | system("cls"); 173 | deleteNode(root); 174 | exportTree(root); 175 | choose(root, size); 176 | break; 177 | default: 178 | system("cls"); 179 | choose(root, size); 180 | break; 181 | } 182 | } -------------------------------------------------------------------------------- /BTH9/Thêm/03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class item { 6 | string word; 7 | string meaning; 8 | public: 9 | int getOrder_firstChar(); 10 | string getMeaning(); 11 | string getWord(); 12 | 13 | void itemImport(); 14 | void itemExport(); 15 | 16 | }; 17 | 18 | struct node { 19 | item value; 20 | node* leftBranch; 21 | node* rightBranch; 22 | node(item element) { 23 | value = element; 24 | leftBranch = rightBranch = NULL; 25 | } 26 | }; 27 | 28 | bool isAvailable(node*, string); 29 | void addItem(item, node*&); 30 | void addWord(node*&, int&); 31 | 32 | void exportWord(node*); 33 | 34 | void getMeaning(node*, string); 35 | void findMeaning(node* y); 36 | 37 | node* minNode(node*); 38 | void deleteNode(node*&, string); 39 | void deleteWord(node*&); 40 | 41 | void printLNR(node*); 42 | 43 | void menu(); 44 | void choose(node*, int&); 45 | 46 | int main(){ 47 | int n; 48 | node* dictionary = NULL; 49 | choose(dictionary, n); 50 | } 51 | 52 | int item::getOrder_firstChar() { 53 | return int(word[0]); 54 | } 55 | string item::getMeaning() { 56 | return meaning; 57 | } 58 | string item::getWord() { 59 | return word; 60 | } 61 | void item::itemImport() { 62 | int temp = getchar(); 63 | cout << "Tu vung: "; 64 | getline(cin, word); 65 | cout << "Nghia cua tu: "; 66 | getline(cin, meaning); 67 | system("cls"); 68 | } 69 | void item::itemExport() { 70 | cout << "Tu vung: " << word << endl; 71 | cout << "Nghia cua tu '" << word << "' la: " << meaning << endl; 72 | } 73 | 74 | bool isAvailable(node* dictionary, string element) { 75 | if (dictionary == NULL) 76 | return false; 77 | if (dictionary->value.getWord() == element) 78 | return true; 79 | return isAvailable(dictionary->leftBranch, element) || isAvailable(dictionary->rightBranch, element); 80 | } 81 | void addItem(item element, node*& dictionary) { 82 | if (dictionary == NULL) 83 | dictionary = new node(element); 84 | else if (element.getOrder_firstChar() > dictionary->value.getOrder_firstChar()) 85 | addItem(element, dictionary->rightBranch); 86 | else if (element.getOrder_firstChar() < dictionary->value.getOrder_firstChar()) 87 | addItem(element, dictionary->leftBranch); 88 | } 89 | void addWord(node*& dictionary, int& amount) { 90 | item element; 91 | element.itemImport(); 92 | addItem(element, dictionary); 93 | amount++; 94 | } 95 | 96 | void exportWord(node* dictionary) { 97 | if (dictionary == NULL) 98 | return; 99 | cout << dictionary->value.getWord() << " is " << dictionary->value.getMeaning() << endl; 100 | exportWord(dictionary->leftBranch); 101 | exportWord(dictionary->rightBranch); 102 | } 103 | 104 | void getMeaning(node* dictionary, string x) { 105 | if (dictionary == NULL) 106 | return; 107 | if (dictionary->value.getWord() == x) 108 | cout << dictionary->value.getMeaning(); 109 | getMeaning(dictionary->rightBranch, x); 110 | getMeaning(dictionary->leftBranch, x); 111 | } 112 | void findMeaning(node* dictionary) { 113 | string s; 114 | int temp = getchar(); 115 | cout << "Tu can tim nghia la: "; 116 | getline(cin, s); 117 | system("cls"); 118 | if (!isAvailable(dictionary, s)) 119 | cout << "Tu '" << s << "' khong ton tai trong tu dien" << endl; 120 | else { 121 | cout << "Nghia cua tu '" << s << "' la: "; 122 | getMeaning(dictionary, s); 123 | cout << endl; 124 | } 125 | } 126 | 127 | node* minNode(node* dictionary) { 128 | if (dictionary == NULL) 129 | return NULL; 130 | while (dictionary != NULL) 131 | dictionary = dictionary->leftBranch; 132 | return dictionary; 133 | } 134 | void deleteNode(node*& dictionary, string element) { 135 | if (dictionary == NULL || !isAvailable(dictionary, element)) 136 | return; 137 | if (dictionary->value.getWord() == element) { 138 | if (dictionary->leftBranch == NULL && dictionary->rightBranch == NULL) { 139 | delete dictionary; 140 | dictionary = NULL; 141 | } 142 | else if (dictionary->leftBranch == NULL && dictionary->rightBranch != NULL) { 143 | node* temp = dictionary; 144 | dictionary = dictionary->rightBranch; 145 | delete temp; 146 | temp = NULL; 147 | } 148 | else if (dictionary->leftBranch != NULL && dictionary->rightBranch == NULL) { 149 | node* temp = dictionary; 150 | dictionary = dictionary->leftBranch; 151 | delete temp; 152 | temp = NULL; 153 | } 154 | else { 155 | node* temp = dictionary; 156 | dictionary = minNode(dictionary->rightBranch); 157 | delete temp; 158 | temp = NULL; 159 | } 160 | } 161 | deleteNode(dictionary->leftBranch, element); 162 | deleteNode(dictionary->rightBranch, element); 163 | } 164 | void deleteWord(node*& dictionary) { 165 | string s; 166 | int temp = getchar(); 167 | cout << "Tu muon xoa khoi tu dien la: "; 168 | getline(cin, s); 169 | system("cls"); 170 | if (!isAvailable(dictionary, s)) 171 | cout << "Tu '" << s << "' khong ton tai trong tu dien" << endl; 172 | else { 173 | deleteNode(dictionary, s); 174 | cout << "Da xoa tu '" << s << "' khoi tu dien" << endl; 175 | } 176 | } 177 | 178 | void printLNR(node* dictionary) { 179 | if (dictionary == NULL) 180 | return; 181 | printLNR(dictionary->leftBranch); 182 | dictionary->value.itemExport(); 183 | cout << endl; 184 | printLNR(dictionary->rightBranch); 185 | } 186 | 187 | void menu() { 188 | cout << "===================================================" << endl; 189 | cout << " (1) Them mot item vao tu dien" << endl; 190 | cout << " (2) Tim nghia cua mot tu nhap vao tu ban phim" << endl; 191 | cout << " (3) Xoa mot tu khoi tu dien" << endl; 192 | cout << " (4) In tu dien theo thu tu tang dang cua word" << endl; 193 | cout << "===================================================" << endl; 194 | } 195 | void choose(node* dictionary, int& n) { 196 | menu(); 197 | int choice; 198 | cout << " Lua chon cua ban la: "; 199 | cin >> choice; 200 | switch (choice) { 201 | case 1: 202 | system("cls"); 203 | addWord(dictionary, n); 204 | cout << "Da them" << endl; 205 | choose(dictionary, n); 206 | break; 207 | case 2: 208 | system("cls"); 209 | findMeaning(dictionary); 210 | choose(dictionary, n); 211 | break; 212 | case 3: 213 | system("cls"); 214 | deleteWord(dictionary); 215 | choose(dictionary, n); 216 | break; 217 | case 4: 218 | system("cls"); 219 | printLNR(dictionary); 220 | choose(dictionary, n); 221 | break; 222 | default: 223 | system("cls"); 224 | choose(dictionary, n); 225 | break; 226 | } 227 | } -------------------------------------------------------------------------------- /BTH9/Tree 2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct node { 6 | int value; 7 | node* leftBranch; 8 | node* rightBranch; 9 | node(int value) { 10 | this->value = value; 11 | leftBranch = rightBranch = NULL; 12 | } 13 | }; 14 | 15 | int random(int min, int max); 16 | void add(node*&, int); 17 | void importTree(node*&, int&); 18 | 19 | void print_RLN(node*); 20 | void print_NLR(node*); 21 | void print_NRL(node*); 22 | void print_LRN(node*); 23 | void print_LNR(node*); 24 | void print_RNL(node*); 25 | void printTree(node*); 26 | 27 | bool isPrime(int x); 28 | void printPrime(node* root); 29 | int maxPrime(node* root); 30 | 31 | void printNodes_levelK(node* tree, int level); 32 | void checkNodes_levelK(node* root); 33 | 34 | int evenTotal(node* root); 35 | 36 | int minValue(node* root); 37 | int maxValue(node* root); 38 | 39 | int countNode_belowX(node* root, int x); 40 | void print_nodeAmount(node* root); 41 | 42 | int countNode_inRange(node* root, int x, int y); 43 | void printNode_inRange(node* root); 44 | 45 | void menu(); 46 | void choose(node*& tree, int& size); 47 | 48 | int main() { 49 | node* tree = NULL; 50 | int n; 51 | choose(tree, n); 52 | system("pause"); 53 | } 54 | 55 | int random(int min, int max) { 56 | static bool first = true; 57 | if (first) { 58 | srand(time(NULL)); 59 | first = false; 60 | } 61 | return min + rand() % ((max + 1) - min); 62 | } 63 | void add(node*& root, int value) { 64 | if (root == NULL) 65 | root = new node(value); 66 | else if (value < root->value) 67 | add(root->leftBranch, value); 68 | else if (root->value < value) 69 | add(root->rightBranch, value); 70 | } 71 | void importTree(node*& TREE, int& size) { 72 | cout << "So phan tu muon them la "; 73 | cin >> size; 74 | system("cls"); 75 | for (int i = 0; i < size; i++) 76 | add(TREE, random(10, 99)); 77 | cout << "Cay nhi phan sau khi them cac phan tu la:" << endl; 78 | printTree(TREE); 79 | cout << endl; 80 | } 81 | 82 | void print_RLN(node* root) { 83 | if (root) { 84 | print_RLN(root->rightBranch); 85 | print_RLN(root->leftBranch); 86 | cout << root->value << " "; 87 | } 88 | } 89 | void print_NLR(node* root) { 90 | if (root) { 91 | cout << root->value << " "; 92 | print_NLR(root->leftBranch); 93 | print_NLR(root->rightBranch); 94 | } 95 | } 96 | void print_NRL(node* root) { 97 | if (root) { 98 | cout << root->value << " "; 99 | print_NRL(root->rightBranch); 100 | print_NRL(root->leftBranch); 101 | } 102 | } 103 | void print_LRN(node* root) { 104 | if (root) { 105 | print_LRN(root->leftBranch); 106 | print_LRN(root->rightBranch); 107 | cout << root->value << " "; 108 | } 109 | } 110 | void print_LNR(node* root) { 111 | if (root) { 112 | print_LNR(root->leftBranch); 113 | cout << root->value << " "; 114 | print_LNR(root->rightBranch); 115 | } 116 | } 117 | void print_RNL(node* root) { 118 | if (root) { 119 | print_LNR(root->rightBranch); 120 | cout << root->value << " "; 121 | print_LNR(root->leftBranch); 122 | } 123 | } 124 | void printTree(node* root) { 125 | cout << " RLN: "; 126 | print_RLN(root); 127 | cout << "\n LRN: "; 128 | print_LRN(root); 129 | cout << "\n NLR: "; 130 | print_NLR(root); 131 | cout << "\n NRL: "; 132 | print_NRL(root); 133 | cout << "\n RNL: "; 134 | print_RNL(root); 135 | cout << "\n LNR: "; 136 | print_LNR(root); 137 | cout << endl; 138 | } 139 | 140 | bool isPrime(int x) { 141 | if (x < 2) 142 | return false; 143 | for (int i = 2; i <= sqrt(x); i++) 144 | if (x % i == 0) 145 | return false; 146 | return true; 147 | } 148 | void printPrime(node* root) { 149 | if (root == NULL) 150 | return; 151 | if (isPrime(root->value)) 152 | cout << root->value << " "; 153 | printPrime(root->leftBranch); 154 | printPrime(root->rightBranch); 155 | } 156 | int maxPrime(node* root) { 157 | static int result = 0; 158 | if (root == NULL) 159 | return 0; 160 | if ((result = maxPrime(root->rightBranch)) != 0) 161 | return result; 162 | if (isPrime(root->value)) 163 | return root->value; 164 | if ((result = maxPrime(root->leftBranch)) != 0) 165 | return result; 166 | } 167 | 168 | void printNodes_levelK(node* tree, int level) { 169 | if (tree == NULL) 170 | return; 171 | level--; 172 | if (level == 0) 173 | cout << tree->value << " "; 174 | if (tree->leftBranch != NULL) 175 | printNodes_levelK(tree->leftBranch, level); 176 | if (tree->rightBranch != NULL) 177 | printNodes_levelK(tree->rightBranch, level); 178 | } 179 | void checkNodes_levelK(node* root) { 180 | int k; 181 | cout << "Muc can in la "; 182 | cin >> k; 183 | system("cls"); 184 | printNodes_levelK(root, k + 1); 185 | cout << endl; 186 | } 187 | 188 | int evenTotal(node* root) { 189 | if (root == NULL) 190 | return 0; 191 | int a = evenTotal(root->leftBranch); 192 | int b = evenTotal(root->rightBranch); 193 | if (root->value % 2 == 0) 194 | return root->value + a + b; 195 | return a + b; 196 | } 197 | 198 | int minValue(node* root) { 199 | if (root->leftBranch == NULL) 200 | return root->value; 201 | return minValue(root->leftBranch); 202 | } 203 | int maxValue(node* root) { 204 | if (root->rightBranch == NULL) 205 | return root->value; 206 | return maxValue(root->rightBranch); 207 | } 208 | 209 | int countNode_belowX(node* root, int x) { 210 | if (root == NULL) 211 | return 0; 212 | int count = countNode_belowX(root->leftBranch, x) + countNode_belowX(root->rightBranch, x); 213 | if (root->value < x) 214 | return count + 1; 215 | else return count; 216 | } 217 | void print_nodeAmount(node* root) { 218 | int x; 219 | cout << "Gia tri x can doi chieu la "; 220 | cin >> x; 221 | system("cls"); 222 | cout << "Trong cay co " << countNode_belowX(root, x) << " nut mang gia tri nho hon " << x << endl; 223 | } 224 | 225 | int countNode_inRange(node* root, int x, int y) { 226 | if (root == NULL) 227 | return 0; 228 | int count = countNode_inRange(root->leftBranch, x, y) + countNode_inRange(root->rightBranch, x, y); 229 | if (root->value > x && root->value < y) 230 | return count + 1; 231 | else return count; 232 | } 233 | void printNode_inRange(node* root) { 234 | int x, y; 235 | cout << "Hai can doi chieu la: " << endl; 236 | cout << " x = "; 237 | cin >> x; 238 | cout << " y = "; 239 | cin >> y; 240 | system("cls"); 241 | cout << "Trong cay co " << countNode_inRange(root, x, y) << " nut mang gia tri lon hon " << x << " va nho hon " << y << endl; 242 | } 243 | 244 | void menu() { 245 | cout << "==============================================================" << endl; 246 | cout << " (1) Tao day so ngau nhien roi nhap vao cay nhi phan" << endl; 247 | cout << " (2) In ra cac so nguyen to tren cay" << endl; 248 | cout << " (3) In ra so nguyen to lon nhat tren cay" << endl; 249 | cout << " (4) In ra cac nut o muc K" << endl; 250 | cout << " (5) Tinh tong cac so chan o cay con trai" << endl; 251 | cout << " (6) In ra phan tu nho nhat o cay con phai" << endl; 252 | cout << " (7) In ra phan tu lon nhat o cay con trai" << endl; 253 | cout << " (8) Dem so nut co gia tri nho hon X" << endl; 254 | cout << " (9) Dem so nut co gia tri lon hon X va nho hon Y" << endl; 255 | cout << "==============================================================" << endl; 256 | } 257 | void choose(node*& tree, int& size) { 258 | system("cls"); 259 | int choice; 260 | menu(); 261 | cout << " Lua chon cua ban la: "; 262 | cin >> choice; 263 | switch (choice) { 264 | case 1: 265 | system("cls"); 266 | importTree(tree, size); 267 | break; 268 | case 2: 269 | system("cls"); 270 | if (tree == NULL) 271 | cout << "Khong co du lieu de thuc hien" << endl; 272 | else if (maxPrime(tree) > 0) { 273 | cout << "Cac so nguyen to co tren cay la: "; 274 | printPrime(tree); 275 | cout << endl; 276 | } 277 | else cout << "Khong co so nguyen to tren cay" << endl; 278 | break; 279 | case 3: 280 | system("cls"); 281 | if (tree == NULL) 282 | cout << "Khong co du lieu de thuc hien" << endl; 283 | else { 284 | if (maxPrime(tree) > 0) 285 | cout << "So nguyen to lon nhat tren cay la " << maxPrime(tree) << endl; 286 | else cout << "Khong co so nguyen to tren cay" << endl; 287 | } 288 | break; 289 | case 4: 290 | system("cls"); 291 | if (tree == NULL) 292 | cout << "Khong co du lieu de thuc hien" << endl; 293 | else checkNodes_levelK(tree); 294 | break; 295 | case 5: 296 | system("cls"); 297 | if (tree == NULL) 298 | cout << "Khong co du lieu de thuc hien" << endl; 299 | else cout << "Tong cac so chan o cay con trai la " << evenTotal(tree->leftBranch) << endl; 300 | break; 301 | case 6: 302 | system("cls"); 303 | if (tree == NULL) 304 | cout << "Khong co du lieu de thuc hien" << endl; 305 | else { 306 | if (minValue(tree->rightBranch) > 0) 307 | cout << "Phan tu nho nhat o cay con phai la " << minValue(tree->rightBranch) << endl; 308 | else cout << "Cay con phan khong co phan tu" << endl; 309 | } 310 | break; 311 | case 7: 312 | system("cls"); 313 | if (tree == NULL) 314 | cout << "Khong co du lieu de thuc hien" << endl; 315 | else { 316 | if (maxValue(tree->leftBranch) > 0) 317 | cout << "Phan tu lon nhat o cay con trai la " << maxValue(tree->leftBranch) << endl; 318 | else cout << "Cay con trai khong co phan tu" << endl; 319 | } 320 | break; 321 | case 8: 322 | system("cls"); 323 | if (tree == NULL) 324 | cout << "Khong co du lieu de thuc hien" << endl; 325 | else print_nodeAmount(tree); 326 | break; 327 | case 9: 328 | system("cls"); 329 | if (tree == NULL) 330 | cout << "Khong co du lieu de thuc hien" << endl; 331 | else printNode_inRange(tree); 332 | break; 333 | default: 334 | choose(tree, size); 335 | break; 336 | } 337 | system("pause"); 338 | choose(tree, size); 339 | } -------------------------------------------------------------------------------- /BTH9/Đề bài.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/BTH9/Đề bài.pdf -------------------------------------------------------------------------------- /Giáo trình tham khảo.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/Giáo trình tham khảo.pdf -------------------------------------------------------------------------------- /Mẫu menu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class yourClass { 5 | int yourVariable; 6 | 7 | public: 8 | void input(); 9 | }; 10 | 11 | void printMenu(); 12 | void choose(yourClass& yourObject); 13 | 14 | int main() { 15 | yourClass yourObject; 16 | yourObject.input(); 17 | 18 | choose(yourObject); 19 | } 20 | 21 | void yourClass::input() { 22 | cout << "Value of your variable is "; 23 | cin >> yourVariable; 24 | } 25 | 26 | void printMenu() { 27 | cout << "-----------------------------" << endl; 28 | cout << " (0) Exit." << endl; 29 | cout << " (1) Do something usefull." << endl; 30 | cout << " (2) Do something usefull." << endl; 31 | cout << " (3) Do something usefull." << endl; 32 | cout << "-----------------------------" << endl; 33 | } 34 | void choose(yourClass& yourObject) { 35 | printMenu(); 36 | 37 | int choice; 38 | cout << "Your choice: "; 39 | cin >> choice; 40 | system("cls"); 41 | 42 | switch (choice) 43 | { 44 | case 0: 45 | return; 46 | case 1: 47 | // Call the function for selection (1) 48 | break; 49 | case 2: 50 | // Call the function for selection (2) 51 | break; 52 | case 3: 53 | // Call the function for selection (3) 54 | break; 55 | default: 56 | cout << "Invalid choice. Please try again!" << endl; 57 | break; 58 | } 59 | 60 | choose(yourObject); 61 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cấu trúc dữ liệu và giải thuật UIT - Tất cả bài thực hành 2 | Tất cả bài thực hành môn Cấu trúc dữ liệu và giải thuật - Trường Đại học Công nghệ Thông tin 3 | __________________________________________ 4 | Bài làm chỉ mang tính chất tham khảo cho các bạn sinh viên khác. Cách triển khai cấu trúc dữ liệu được mình áp dụng phương pháp lập trình hướng đối tượng để tối ưu hóa khi sử dụng. Ngoài ra, đây là môn học tiên quyết phục vụ cho tất cả các môn học chuyên ngành kỹ thuật phần mềm, do đó cần phải hiểu thật sâu bản chất của tối thiểu 75% số lượng giải thuật và cấu trúc dữ liệu trong chương trình học. 5 | __________________________________________ 6 | Lời khuyên cá nhân: Để học môn này đạt hiệu quả cao nhất, các bạn nên chuẩn bị trước: 7 | - Kiến thức nền tảng thật chắc về mảng và giải thuật đệ quy để dễ dàng hiểu bản chất của các giải thuật tìm kiếm và sắp xếp. 8 | - Cách thức triển khai chương trình áp dụng phương pháp lập trình hướng đối tượng để triển khai các cấu trúc dữ liệu một cách trực quan và dễ hiểu hơn. 9 | -------------------------------------------------------------------------------- /Thi thử cuối học kỳ/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node { 5 | int value; 6 | node* leftBranch; 7 | node* rightBranch; 8 | node(int value) { 9 | this->value = value; 10 | leftBranch = rightBranch = NULL; 11 | } 12 | }; 13 | 14 | void add(node*&, int); 15 | void importTree(node*&, int&); 16 | void print_RNL(node*); 17 | 18 | bool isAvailable(node* tree, int x); 19 | void findParent(node* tree, int val, int parent); 20 | void findNode(node* tree); 21 | 22 | int level(node* tree, int data); 23 | void printLevel(node* root, node* tree); 24 | 25 | int rankOf(node* tree); 26 | void rank_ofLevel(node* BST, node* root, int level); 27 | void printRank(node* tree); 28 | 29 | void menu(); 30 | void choose(node*& tree); 31 | 32 | int main() { 33 | node* tree = NULL; 34 | choose(tree); 35 | } 36 | 37 | void add(node*& root, int value) { 38 | if (root == NULL) 39 | root = new node(value); 40 | else if (value < root->value) 41 | add(root->leftBranch, value); 42 | else if (root->value < value) 43 | add(root->rightBranch, value); 44 | } 45 | void importTree(node*& TREE) { 46 | int addValue = 1; 47 | while (addValue > 0) { 48 | cout << "Phan tu them vao la: "; 49 | cin >> addValue; 50 | add(TREE, addValue); 51 | system("cls"); 52 | } 53 | cout << "Cay nhi phan sau khi them cac phan tu la: "; 54 | print_RNL(TREE); 55 | cout << endl; 56 | } 57 | 58 | void print_RNL(node* root) { 59 | if (root) { 60 | print_RNL(root->rightBranch); 61 | cout << root->value << " "; 62 | print_RNL(root->leftBranch); 63 | } 64 | } 65 | 66 | bool isAvailable(node* tree, int x) { 67 | if (tree == NULL) 68 | return false; 69 | if (tree->value == x) 70 | return true; 71 | return isAvailable(tree->leftBranch, x) || isAvailable(tree->rightBranch, x); 72 | } 73 | void findParent(node* tree, int val, int parent) { 74 | if (tree == NULL) 75 | return; 76 | if (tree->value == val) 77 | cout << parent << endl; 78 | else { 79 | findParent(tree->leftBranch, val, tree->value); 80 | findParent(tree->rightBranch, val, tree->value); 81 | } 82 | } 83 | void findNode(node* tree) { 84 | int x; 85 | cout << "Gia tri cua nut con la: "; 86 | cin >> x; 87 | system("cls"); 88 | if (tree == NULL) 89 | cout << "Gia tri cua nut cha la: " << 0 << endl; 90 | else { 91 | cout << "Gia tri cua nut cha la: "; 92 | if (tree->value == x) 93 | cout << 1 << endl; 94 | else if (!isAvailable(tree, x)) 95 | cout << 0 << endl; 96 | else findParent(tree, x, 0); 97 | } 98 | } 99 | 100 | int level(node* tree, int data) { 101 | if (tree != NULL && tree->value != data) { 102 | if (tree->value > data) 103 | return 1 + level(tree->leftBranch, data); 104 | else return 1 + level(tree->rightBranch, data); 105 | } 106 | return 0; 107 | } 108 | void printLevel(node* tree, node *root) { 109 | if (tree == NULL) 110 | return; 111 | printLevel(tree->leftBranch, root); 112 | cout << level(root, tree->value) << " "; 113 | printLevel(tree->rightBranch, root); 114 | } 115 | 116 | int rankOf(node* tree) { 117 | if (tree != NULL) { 118 | if (tree->leftBranch != NULL) { 119 | if (tree->rightBranch != NULL) 120 | return 2; 121 | return 1; 122 | } 123 | else if (tree->rightBranch != NULL) { 124 | return 1; 125 | } 126 | else return 0; 127 | } 128 | return -1; 129 | } 130 | void rank_ofLevel(node* BST, node* root, int levelX) { 131 | if (BST != NULL) { 132 | rank_ofLevel(BST->rightBranch, root, levelX); 133 | if (level(root, BST->value) == levelX + 1) 134 | cout << rankOf(BST) << " "; 135 | rank_ofLevel(BST->leftBranch, root, levelX); 136 | } 137 | } 138 | void printRank(node *tree) { 139 | int l; 140 | cout << "L = "; 141 | cin >> l; 142 | system("cls"); 143 | rank_ofLevel(tree, tree, l); 144 | } 145 | 146 | void menu() { 147 | cout << "==============================================================" << endl; 148 | cout << " (1) Nhap vao mang so nguyen duong" << endl; 149 | cout << " (2) In ra gia tri nut cha cua X" << endl; 150 | cout << " (3) In ra bac cua cac nut theo thu tu tang dan" << endl; 151 | cout << " (4) In ra bac cua cac nut co muc L+1 theo thu tu giam dan" << endl; 152 | cout << "==============================================================" << endl; 153 | } 154 | void choose(node*& tree) { 155 | int choice; 156 | menu(); 157 | cout << " Lua chon cua ban la: "; 158 | cin >> choice; 159 | switch (choice) { 160 | case 1: 161 | system("cls"); 162 | importTree(tree); 163 | choose(tree); 164 | break; 165 | case 2: 166 | system("cls"); 167 | findNode(tree); 168 | choose(tree); 169 | break; 170 | case 3: 171 | system("cls"); 172 | printLevel(tree, tree); 173 | cout << endl; 174 | choose(tree); 175 | break; 176 | case 4: 177 | system("cls"); 178 | printRank(tree); 179 | cout << endl; 180 | choose(tree); 181 | break; 182 | default: 183 | choose(tree); 184 | break; 185 | } 186 | } -------------------------------------------------------------------------------- /Thi thử cuối học kỳ/Đề bài.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/Thi thử cuối học kỳ/Đề bài.pdf -------------------------------------------------------------------------------- /Đề cương môn học.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Data-Structures-and-Algorithms-UIT/f6a2885d497ae54ee9560d9a12cad394d0508682/Đề cương môn học.pdf --------------------------------------------------------------------------------