NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <iostream>

struct Mahasiswa {
int npm;
std::string nama;
Mahasiswa* next;

Mahasiswa(int _npm, const std::string& _nama) : npm(_npm), nama(_nama), next(nullptr) {}
};

class OrganisasiKampus {
private:
Mahasiswa* kepala;

public:
OrganisasiKampus() : kepala(nullptr) {}

// Fungsi untuk menambahkan mahasiswa ke dalam daftar
void tambahMahasiswa(int npm, const std::string& nama) {
Mahasiswa* mahasiswaBaru = new Mahasiswa(npm, nama);
if (!kepala) {
mahasiswaBaru->next = mahasiswaBaru; // Daftar kosong, membuatnya berputar sendiri
kepala = mahasiswaBaru;
} else {
Mahasiswa* tail = kepala;
while (tail->next != kepala) {
tail = tail->next;
}
tail->next = mahasiswaBaru;
mahasiswaBaru->next = kepala;
}
}

// Fungsi untuk menghapus mahasiswa dari daftar berdasarkan NPM
void hapusMahasiswa(int npm) {
if (!kepala) {
std::cout << "Daftar kosong." << std::endl;
return;
}

Mahasiswa* current = kepala;
Mahasiswa* prev = nullptr;

do {
if (current->npm == npm) {
if (prev) {
prev->next = current->next;
}
if (current == kepala) {
kepala = kepala->next;
}
delete current;
std::cout << "Mahasiswa dengan NPM " << npm << " telah dihapus." << std::endl;
return;
}

prev = current;
current = current->next;
} while (current != kepala);

std::cout << "Mahasiswa dengan NPM " << npm << " tidak ditemukan." << std::endl;
}

// Fungsi untuk menampilkan seluruh mahasiswa di daftar
void tampilMahasiswa() {
if (!kepala) {
std::cout << "Daftar kosong." << std::endl;
return;
}

Mahasiswa* current = kepala;

do {
std::cout << "NPM: " << current->npm << ", Nama: " << current->nama << std::endl;
current = current->next;
} while (current != kepala);
}

// Fungsi untuk mencari nilai minimum NPM dalam daftar
int cariMinNPM() {
if (!kepala) {
std::cout << "Daftar kosong." << std::endl;
return -1;
}

Mahasiswa* current = kepala;
int minNPM = current->npm;

do {
if (current->npm < minNPM) {
minNPM = current->npm;
}
current = current->next;
} while (current != kepala);

return minNPM;
}

// Fungsi untuk mencari nilai maksimum NPM dalam daftar
int cariMaxNPM() {
if (!kepala) {
std::cout << "Daftar kosong." << std::endl;
return -1;
}

Mahasiswa* current = kepala;
int maxNPM = current->npm;

do {
if (current->npm > maxNPM) {
maxNPM = current->npm;
}
current = current->next;
} while (current != kepala);

return maxNPM;
}
};

int main() {
OrganisasiKampus organisasi;

organisasi.tambahMahasiswa(12345, "John");
organisasi.tambahMahasiswa(67890, "Jane");
organisasi.tambahMahasiswa(54321, "Doe");

organisasi.tampilMahasiswa();

organisasi.hapusMahasiswa(67890);
organisasi.tampilMahasiswa();

int minNPM = organisasi.cariMinNPM();
int maxNPM = organisasi.cariMaxNPM();

std::cout << "NPM terkecil: " << minNPM << std::endl;
std::cout << "NPM terbesar: " << maxNPM << std::endl;

return 0;
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.