0% found this document useful (0 votes)
10 views2 pages

StrukturData Pertemuan20

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

StrukturData Pertemuan20

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Nama : Andhika Vallerian Ramadhani SantosaNIM : 231011401957Kelas

: 03TPLE018Mata Kuliah : Struktur DataTugas : Pertemuan-20


1. Kode program:
#include <iostream>#include <string>#include <limits>using namespace std;struct
Mahasiswa { long long NIM; string nama; float nilai;};struct Node {
Mahasiswa data; Node* next;};class LinkedList {private: Node* head; Node*
merge(Node* left, Node* right) { if (!left) return right; if (!right)
return left; if (left->data.NIM < right->data.NIM) { left->next =
merge(left->next, right); return left; } else { right-
>next = merge(left, right->next); return right; } } Node*
mergeSort(Node* node) { if (!node || !node->next) return node; Node*
middle = getMiddle(node); Node* nextOfMiddle = middle->next; middle-
>next = NULL; Node* left = mergeSort(node); Node* right =
mergeSort(nextOfMiddle); return merge(left, right); } Node*
getMiddle(Node* node) { if (!node) return node; Node* slow = node;
Node* fast = node->next; while (fast && fast->next) { slow =
slow->next; fast = fast->next->next; } return slow; }
void shellSort() { int size = getSize(); Node** array = new
Node*[size]; Node* current = head; for (int i = 0; i < size; ++i) {
array[i] = current; current = current->next; } for (int
gap = size / 2; gap > 0; gap /= 2) { for (int i = gap; i < size; ++i) {
Node* temp = array[i]; int j; for (j = i; j >= gap &&
array[j - gap]->data.NIM > temp->data.NIM; j -= gap) { array[j]
= array[j - gap]; } array[j] = temp; }
} for (int i = 0; i < size - 1; ++i) { array[i]->next = array[i +
1]; } array[size - 1]->next = NULL; head = array[0];
delete[] array; } int getSize() { int size = 0; Node* current =
head; while (current) { size++; current = current-
>next; } return size; }public: LinkedList() : head(NULL) {}
void insertData(const Mahasiswa& mhs) { Node* newNode = new Node{mhs, NULL};
if (!head) { head = newNode; } else { Node* temp =
head; while (temp->next) { temp = temp->next;
} temp->next = newNode; } } void displayData()
{ Node* temp = head; while (temp) { cout << "NIM: " <<
temp->data.NIM << ", Nama: " << temp->data.nama <<
", Nilai: " << temp->data.nilai << endl; temp = temp->next; }
} void sortData(int method) { if (method == 1) { head =
mergeSort(head); cout << "Data telah diurutkan menggunakan Merge Sort.\
n"; } else if (method == 2) { shellSort(); cout <<
"Data telah diurutkan menggunakan Shell Sort.\n"; } else { cout
<< "Metode pengurutan tidak valid.\n"; } }};int main() { cout <<
"Andhika Vallerian Ramadhani Santosa" << endl; cout << "231011401957" << endl <<
endl; LinkedList list; int choice; do { cout << "\nMenu:\n";
cout << "1. Insert Data Mahasiswa\n"; cout << "2. Tampilkan Data Mahasiswa\
n"; cout << "3. Urutkan Data Mahasiswa (1: Merge Sort, 2: Shell Sort)\n";
cout << "4. Keluar\n"; cout << "Pilih: "; cin >> choice; if
(cin.fail()) { cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "Input
tidak valid. Masukkan angka.\n"; continue; } switch
(choice) { case 1: { Mahasiswa mhs; cout
<< "Masukkan NIM: "; cin >> mhs.NIM; if (cin.fail()
|| mhs.NIM < 100000000000 || mhs.NIM > 999999999999) {
cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\
n'); cout << "NIM harus berupa 12 digit angka.\n";
break; } cin.ignore(); cout <<
"Masukkan Nama: "; getline(cin, mhs.nama); cout <<
"Masukkan Nilai: "; cin >> mhs.nilai; if (cin.fail())
{ cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout <<
"Nilai harus berupa angka.\n"; break; }
list.insertData(mhs); break; } case 2:
list.displayData(); break; case 3: { int
method; cout << "Pilih metode pengurutan (1: Merge Sort, 2: Shell
Sort): "; cin >> method; if (cin.fail()) {
cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\
n'); cout << "Input tidak valid. Masukkan angka.\n";
break; } list.sortData(method); break;
} case 4: cout << "Keluar program.\n";
break; default: cout << "Pilihan tidak valid.\n";
} } while (choice != 4); return 0;}

You might also like