Untitled
unknown
plain_text
a year ago
7.6 kB
4
Indexable
#include <iostream> using namespace std; // Struct untuk node struct Node { string nama; string nim; Node* next; }; // Fungsi untuk tambah depan Node* tambahDepan(Node* head, string nama, string nim) { Node* baru = new Node(); baru->nama = nama; baru->nim = nim; baru->next = head; head = baru; return head; } // Fungsi untuk tambah belakang Node* tambahBelakang(Node* head, string nama, string nim) { Node* baru = new Node(); Node* bantu = head; baru->nama = nama; baru->nim = nim; baru->next = NULL; if (head == NULL) { head = baru; } else { while (bantu->next != NULL) { bantu = bantu->next; } bantu->next = baru; } return head; } // Fungsi untuk tambah tengah Node* tambahTengah(Node* head, string nama, string nim, int posisi) { Node* baru = new Node(); Node* bantu = head; Node* sebelum = head; baru->nama = nama; baru->nim = nim; if (posisi == 0) { baru->next = head; head = baru; } else { for (int i = 0; i < posisi - 1; i++) { sebelum = bantu; bantu = bantu->next; } sebelum->next = baru; baru->next = bantu; } return head; } // Fungsi untuk ubah depan Node* ubahDepan(Node* head, string nama, string nim) { if (head != NULL) { head->nama = nama; head->nim = nim; } return head; } // Fungsi untuk ubah belakang Node* ubahBelakang(Node* head, string nama, string nim) { Node* bantu = head; if (bantu != NULL) { while (bantu->next != NULL) { bantu = bantu->next; } bantu->nama = nama; bantu->nim = nim; } return head; } // Fungsi untuk ubah tengah Node* ubahTengah(Node* head, string nama, string nim, int posisi) { Node* bantu = head; Node* sebelum = head; if (bantu != NULL) { for (int i = 0; i < posisi - 1; i++) { sebelum = bantu; bantu = bantu->next; } bantu->nama = nama; bantu->nim = nim; } return head; } // Fungsi untuk hapus depan Node* hapusDepan(Node* head) { Node* hapus = head; if (head != NULL) { head = head->next; delete hapus; } return head; } // Fungsi untuk hapus belakang Node* hapusBelakang(Node* head) { Node* bantu = head; Node* sebelum = head; if (bantu != NULL) { while (bantu->next != NULL) { sebelum = bantu; bantu = bantu->next; } delete bantu; sebelum->next = NULL; } return head;} // Fungsi untuk hapus tengah Node* hapusTengah(Node* head, int posisi) { Node* bantu = head; Node* sebelum = head; if (bantu != NULL) { for (int i = 0; i < posisi - 1; i++) { sebelum = bantu; bantu = bantu->next; } sebelum->next = bantu->next; delete bantu; } return head; } // Fungsi untuk hapus list Node* hapusList(Node* head) { Node* hapus = head; while (head != NULL) { head = hapus->next; delete hapus; hapus = head; } return head; } // Fungsi untuk tampilkan data void tampilData(Node* head) { Node* bantu = head; if (bantu == NULL) { cout << "Data kosong" << endl; } else { cout << "DATA MAHASISWA" << endl; cout << "NAMA\tNIM" << endl; while (bantu != NULL) { cout << bantu->nama << "\t" << bantu->nim << endl; bantu = bantu->next; } } } int main() { Node* head = NULL; int pilih, posisi; string nama, nim; do { system("cls"); cout << "PROGRAM SINGLE LINKED LIST NON-CIRCULAR" << endl; cout << "1. Tambah Depan" << endl; cout << "2. Tambah Belakang" << endl; cout << "3. Tambah Tengah" << endl; cout << "4. Ubah Depan" << endl; cout << "5. Ubah Belakang" << endl; cout << "6. Ubah Tengah" << endl; cout << "7. Hapus Depan" << endl; cout << "8. Hapus Belakang" << endl; cout << "9. Hapus Tengah" << endl; cout << "10. Hapus List" << endl; cout << "11. TAMPILKAN" << endl; cout << "0. KELUAR" << endl; cout << "Pilih Operasi : "; cin >> pilih; switch (pilih) { case 1: cout << "Tambah Depan" << endl; cout << "Masukkan Nama : "; cin.ignore(); getline(cin, nama); cout << "Masukkan NIM : "; getline(cin, nim); head = tambahDepan(head, nama, nim); break; case 2: cout << "Tambah Belakang" << endl; cout << "Masukkan Nama : "; cin.ignore(); getline(cin, nama); cout << "Masukkan NIM : "; getline(cin, nim); head = tambahBelakang(head, nama, nim); break; case 3: cout << "Tambah Tengah" << endl; cout << "Masukkan Nama : "; cin.ignore(); getline(cin, nama); cout << "Masukkan NIM : "; getline(cin, nim); cout << "Masukkan Posisi : "; cin >> posisi; head = tambahTengah(head, nama, nim, posisi); break; case 4: cout << "Ubah Depan" << endl; cout << "Masukkan Nama : "; cin.ignore(); getline(cin, nama); cout << "Masukkan NIM : "; getline(cin, nim); head = ubahDepan(head, nama, nim); break; case 5: cout << "Ubah Belakang" << endl; cout << "Masukkan Nama : "; cin.ignore(); getline(cin, nama); cout << "Masukkan NIM : "; getline(cin, nim); head = ubahBelakang(head, nama, nim); break; case 6: cout << "Ubah Tengah" << endl; cout << "Masukkan Nama : "; cin.ignore(); getline(cin, nama); cout << "Masukkan NIM : "; getline(cin, nim); cout << "Masukkan Posisi : "; cin >> posisi; head = ubahTengah(head, nama, nim, posisi); break; case 7: cout << "Hapus Depan" << endl; head = hapusDepan(head); break; case 8: cout << "Hapus Belakang" << endl; head = hapusBelakang(head); break; case 9: cout << "Hapus Tengah" << endl; cout << "Masukkan Posisi : "; cin >> posisi; head = hapusTengah(head, posisi); break; case 10: cout << "Hapus List" << endl; head = hapusList(head); break; case 11: cout << "TAMPILKAN" << endl; tampilData(head); system("pause"); break; } } while (pilih != 0); return 0; }
Editor is loading...
Leave a Comment