0% found this document useful (0 votes)
2 views7 pages

Searching

The document presents a C++ implementation of a Circular Linked List to manage student data, including operations to insert, delete, print, and search for student records. It defines a 'Mahasiswa' structure for student attributes and provides methods for manipulating the linked list. The main function allows user interaction to perform various operations on the student data stored in the circular linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Searching

The document presents a C++ implementation of a Circular Linked List to manage student data, including operations to insert, delete, print, and search for student records. It defines a 'Mahasiswa' structure for student attributes and provides methods for manipulating the linked list. The main function allows user interaction to perform various operations on the student data stored in the circular linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ALFITRA FANNY WIJAYA

221011450216

03TPLM003

SEARCHING PERTEMUAN KE-16

#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;
}

void insertAwal(string nama, string nim, char gender, float nilai)


{
Mahasiswa *newNode = new Mahasiswa;
newNode->nama = nama;
newNode->nim = nim;
newNode->gender = gender;
newNode->nilai = nilai;

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();
}

void insertAkhir(string nama, string nim, char gender, float nilai)


{
Mahasiswa *newNode = new Mahasiswa;
newNode->nama = nama;
newNode->nim = nim;
newNode->gender = gender;
newNode->nilai = nilai;

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);
}

void searchData(string nama)


{
if (head == NULL)
{
cout << "Linked list kosong" << endl;
return;
}
Mahasiswa *temp = head;
bool found = false;
do
{
if (temp->nama == nama)
{
cout << "\nData Ditemukan:" << endl;
cout << "Nama: " << temp->nama << "\nNIM: " << temp->nim <<
"\nGender: " << temp->gender << "\nNilai: " << temp->nilai << endl;
found = true;
break;
}
temp = temp->next;
} while (temp != head);
if (!found)
{
cout << "\nData dengan nama " << nama << " tidak ditemukan." <<
endl;
}
}

~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;
}

INSERT AWAL SEBANYAK 4 DATA LALU CARI DATA BERDASARKAN NAMA

You might also like