Searching
Searching
221011450216
03TPLM003
#include <iostream>
#include <string>
using namespace std;
struct Mahasiswa
{
string nama;
string nim;
char gender;
float nilai;
Mahasiswa *next;
};
class CircularLinkedList
{
private:
Mahasiswa *head;
public:
CircularLinkedList()
{
head = NULL;
}
if (head == NULL)
{
head = newNode;
newNode->next = head;
}
else
{
Mahasiswa *temp = head;
while (temp->next != head)
{
temp = temp->next;
}
newNode->next = head;
temp->next = newNode;
head = newNode;
}
sortData();
}
if (head == NULL)
{
head = newNode;
newNode->next = head;
}
else
{
Mahasiswa *temp = head;
while (temp->next != head)
{
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
sortData();
}
void deleteAkhir()
{
if (head == NULL)
{
cout << "Linked list kosong" << endl;
return;
}
if (head->next == head)
{
delete head;
head = NULL;
cout << "Data terakhir dalam antrian telah dihapus" << endl;
return;
}
Mahasiswa *temp = head;
Mahasiswa *prev = NULL;
while (temp->next != head)
{
prev = temp;
temp = temp->next;
}
prev->next = head;
delete temp;
cout << "Data terakhir dalam antrian telah dihapus" << endl;
}
void deleteAwal()
{
if (head == NULL)
{
cout << "Linked list kosong" << endl;
return;
}
if (head->next == head)
{
delete head;
head = NULL;
cout << "Data pertama dalam antrian telah dihapus" << endl;
return;
}
Mahasiswa *temp = head;
while (temp->next != head)
{
temp = temp->next;
}
Mahasiswa *hapus = head;
temp->next = head->next;
head = head->next;
delete hapus;
cout << "Data pertama dalam antrian telah dihapus" << endl;
}
void printData()
{
if (head == NULL)
{
cout << "Linked list kosong" << endl;
return;
}
Mahasiswa *temp = head;
do
{
cout << "\nNama: " << temp->nama << "\nNIM: " << temp->nim <<
"\nGender: " << temp->gender << "\nNilai: " << temp->nilai << endl;
temp = temp->next;
} while (temp != head);
}
void sortData()
{
if (head == NULL || head->next == head)
{
return;
}
Mahasiswa *i, *j;
bool swapped;
do
{
swapped = false;
i = head;
while (i->next != head)
{
j = i->next;
if (i->nama > j->nama)
{
swap(i->nama, j->nama);
swap(i->nim, j->nim);
swap(i->gender, j->gender);
swap(i->nilai, j->nilai);
swapped = true;
}
i = i->next;
}
} while (swapped);
}
~CircularLinkedList()
{
if (head != NULL)
{
Mahasiswa *current = head;
Mahasiswa *next;
do
{
next = current->next;
delete current;
current = next;
} while (current != head);
}
}
};
int main()
{
CircularLinkedList mahasiswaList;
int choice;
string nama, nim;
char gender;
float nilai;
do
{
cout << "\nCIRCULAR LINKED LIST" << endl;
cout << "======================" << endl;
cout << "1. INSERT DATA AWAL" << endl;
cout << "2. INSERT DATA AKHIR" << endl;
cout << "3. HAPUS DATA AWAL" << endl;
cout << "4. HAPUS DATA AKHIR" << endl;
cout << "5. CETAK DATA" << endl;
cout << "6. CARI DATA BERDASARKAN NAMA" << endl;
cout << "7. EXIT" << endl;
cout << "Pilihan (1 - 7): ";
cin >> choice;
cin.ignore(); // Menangani newline setelah input angka
switch (choice)
{
case 1:
cout << "\nNama: ";
getline(cin, nama);
cout << "NIM: ";
getline(cin, nim);
cout << "Gender (L/P): ";
cin >> gender;
cout << "Nilai: ";
cin >> nilai;
mahasiswaList.insertAwal(nama, nim, gender, nilai);
break;
case 2:
cout << "\nNama: ";
getline(cin, nama);
cout << "NIM: ";
getline(cin, nim);
cout << "Gender (L/P): ";
cin >> gender;
cout << "Nilai: ";
cin >> nilai;
mahasiswaList.insertAkhir(nama, nim, gender, nilai);
break;
case 3:
mahasiswaList.deleteAwal();
break;
case 4:
mahasiswaList.deleteAkhir();
break;
case 5:
cout << "\nData Mahasiswa " << endl;
mahasiswaList.printData();
break;
case 6:
cout << "\nMasukkan nama yang dicari: ";
getline(cin, nama);
mahasiswaList.searchData(nama);
break;
case 7:
cout << "Program selesai." << endl;
break;
default:
cout << "Pilihan tidak valid. Silakan pilih antara 1 sampai 7." <<
endl;
}
} while (choice != 7);
return 0;
}