NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

TUGAS 1
Nama : Ahmad Agung Setyawan (40622100065)
Nama : Aryandra Luthfir Rahman (40622100063)
Nama : Marshall Dzulidham (40622100072)
#include <iostream> #include <string> using namespace std;
struct Node {
string nama;
int nilai;
Node* next;
};
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = NULL;
}

void tambahKeDepan(string nama, int nilai) {

Node* newNode = new Node;
newNode->nama = nama;
newNode->nilai = nilai;
newNode->next = head;
head = newNode;
}
void tambahKeBelakang(string nama, int nilai) {
Node* newNode = new Node;
newNode->nama = nama;
newNode->nilai = nilai;
newNode->next = NULL;
if (!head) {
head = newNode;
return;
}
Node* current = head;
while (current->next) {
current = current->next;
}
current->next = newNode;
}
void tambahTerurut(string nama, int nilai) {

Node* newNode = new Node;
newNode->nama = nama;
newNode->nilai = nilai;
newNode->next = NULL;
if (!head || head->nilai >= nilai) {
newNode->next = head;
head = newNode;
} else {
Node* current = head;
while (current->next && current->next->nilai < nilai) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
bool cari(string nama) {
Node* current = head;
while (current) {
if (current->nama == nama) {
return true;
}
current = current->next;
}
return false;

}
void hapusDepan() {
if (head) {
Node* temp = head;
head = head->next;
delete temp;
}
}
void tampil() {
Node* current = head;
while (current) {
cout << "Nama: " << current->nama << ", Nilai: " << current->nilai << endl;
current = current->next;
}
cout << endl;
}
// Fungsi untuk mencari nilai minimum dan maksimum
void cariMinMax(int& minVal, int& maxVal, string& minNama, string& maxNama) {
if (!head) {
minVal = maxVal = 0;
return;
}
Node* current = head;

minVal = maxVal = current->nilai;
minNama = maxNama = current->nama;
while (current) {
if (current->nilai < minVal) {
minVal = current->nilai;
minNama = current->nama;
}
if (current->nilai > maxVal) {
maxVal = current->nilai;
maxNama = current->nama;
}
current = current->next;
}
}
};
int main() {
LinkedList myList;
int pilihan, nilai, minVal, maxVal;
string nama, minNama, maxNama;
do {
cout << "========== MENU ==========" << endl;
cout << "1. Tambah di Depan" << endl;
cout << "2. Tambah di Belakang" << endl;
cout << "3. Tambah secara Terurut" << endl;

cout << "4. Cari berdasarkan Nama" << endl;
cout << "5. Hapus dari Depan" << endl;
cout << "6. Cari Nilai Minimum dan Maksimum" << endl;
cout << "7. Tampilkan" << endl;
cout << "0. Keluar" << endl;
cout << "Masukkan pilihan anda: ";
cin >> pilihan;
switch (pilihan) {
case 1:
cout << "Masukkan nama: ";
cin.ignore();
getline(cin, nama);
cout << "Masukkan nilai: ";
cin >> nilai;
myList.tambahKeDepan(nama, nilai);
break;
case 2:
cout << "Masukkan nama: ";
cin.ignore();
getline(cin, nama);
cout << "Masukkan nilai: ";
cin >> nilai;
myList.tambahKeBelakang(nama, nilai);
break;

case 3:
cout << "Masukkan nama: ";
cin.ignore();
getline(cin, nama);
cout << "Masukkan nilai: ";
cin >> nilai;
myList.tambahTerurut(nama, nilai);
break;
case 4:
cout << "Masukkan nama yang ingin dicari: ";
cin.ignore();
getline(cin, nama);
if (myList.cari(nama)) {
cout << "Nama ditemukan dalam linked list." << endl;
} else {
cout << "Nama tidak ditemukan dalam linked list." << endl;
}
break;
case 5:
myList.hapusDepan();
break;
case 6:
myList.cariMinMax(minVal, maxVal, minNama, maxNama);
cout << "Nilai Minimum: " << minVal << " (Nama: " << minNama << ")" << endl;

case 7:
myList.tampil();
break;
cout << "Nilai Maksimum: " << maxVal << " (Nama: " << maxNama << ")" << endl;
break;
case 0:
cout << "Keluar dari program." << endl;
break;
default:
cout << "Pilihan tidak valid. Silakan coba lagi." << endl;
}
} while (pilihan != 0);
return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for 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 12 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.