Untitled
unknown
c_cpp
6 months ago
5.8 kB
7
Indexable
Never
#include <iostream> using namespace std; struct Node { int age; string name; Node *next; }; Node *head; Node *tail; void init() { head = NULL; tail = NULL; } bool isEmpty() { if (head == NULL) return true; else return false; } void insertDepan(string name, int nilai) { // Buat Node baru Node *baru = new Node; baru->name = name; baru->age = nilai; baru->next = NULL; if (isEmpty() == true) { head = tail = baru; tail->next = NULL; } else { baru->next = head; head = baru; } } // Tambah Belakang void insertBelakang(string name, int nilai) { // Buat Node baru Node *baru = new Node; baru->name = name; baru->age = nilai; baru->next = NULL; if (isEmpty() == true) { head = tail = baru; tail->next = NULL; } else { tail->next = baru; tail = baru; } } // Hitung Jumlah List int hitungList() { Node *hitung; hitung = head; int jumlah = 0; while (hitung != NULL) { jumlah++; hitung = hitung->next; } return jumlah; } // Tambah Tengah void insertTengah(string name, int age, int posisi) { if (posisi < 1 || posisi > hitungList()) { cout << "Posisi diluar jangkauan" << endl; } else if (posisi == 1) { cout << "Posisi bukan posisi tengah" << endl; } else { Node *baru, *bantu; baru = new Node(); baru->name = name; baru->age = age; // tranversing bantu = head; int nomor = 1; while (nomor < posisi - 1) { bantu = bantu->next; nomor++; } baru->next = bantu->next; bantu->next = baru; } } // Hapus Depan void hapusDepan() { Node *hapus; if (isEmpty() == false) { if (head->next != NULL) { hapus = head; head = head->next; delete hapus; } else { head = tail = NULL; } } else { cout << "List kosong!" << endl; } } // Hapus Belakang void hapusBelakang() { Node *hapus; Node *bantu; if (isEmpty() == false) { if (head != tail) { hapus = tail; bantu = head; while (bantu->next != tail) { bantu = bantu->next; } tail = bantu; tail->next = NULL; delete hapus; } else { head = tail = NULL; } } else { cout << "List kosong!" << endl; } } // Hapus Tengah void hapusTengah(int posisi) { Node *hapus, *bantu, *bantu2; if (posisi < 1 || posisi > hitungList()) { cout << "Posisi di luar jangkauan" << endl; } else if (posisi == 1) { cout << "Posisi bukan posisi tengah" << endl; } else { int nomor = 1; bantu = head; while (nomor <= posisi) { if (nomor == posisi - 1) { bantu2 = bantu; } if (nomor == posisi) { hapus = bantu; } bantu = bantu->next; nomor++; } bantu2->next = bantu; delete hapus; } } // Ubah Depan void ubahDepan(string name, int age) { if (isEmpty() == false) { head->name = name; head->age = age; } else { cout << "List masih kosong!" << endl; } } // Ubah Tengah void ubahTengah(string name, int age, int posisi) { Node *bantu; if (isEmpty() == false) { if (posisi < 1 || posisi > hitungList()) { cout << "Posisi di luar jangkauan" << endl; } else if (posisi == 1) { cout << "Posisi bukan posisi tengah" << endl; } else { bantu = head; int nomor = 1; while (nomor < posisi) { bantu = bantu->next; nomor++; } bantu->name = name; bantu->age = age; } } else { cout << "List masih kosong!" << endl; } } // Ubah Belakang void ubahBelakang(string name, int age) { if (isEmpty() == false) { tail->name = name; tail->age = age; } else { cout << "List masih kosong!" << endl; } } // Hapus List void clearList() { Node *bantu, *hapus; bantu = head; while (bantu != NULL) { hapus = bantu; bantu = bantu->next; delete hapus; } head = tail = NULL; cout << "List berhasil terhapus!" << endl; } // Tampilkan List void tampil() { Node *bantu; bantu = head; if (isEmpty() == false) { while (bantu != NULL) { cout << bantu->name << "\t" << ends; cout << bantu->age << endl; bantu = bantu->next; } cout << endl; } else { cout << "List masih kosong!" << endl; } } int main() { init(); int arr_size = 7; string names[7] = {"John", "Jane", "Michael", "Yusuke", "Akechi", "Hoshino", "Karin"}; int ages[7] = {19, 20, 18, 19, 20, 18, 18}; int age; string name; for (int i = 0; i < arr_size; i++) { printf("Masukkan Nama ke-%s : ", i+1); cin >> name; printf("Masukkan Umur ke-%s: ", i+1); cin >> age; printf("\n"); insertDepan(name, age); } printf("\n"); // Hapus data akechi hapusTengah(5); insertTengah("Futaba", 18, 2); insertDepan("Igor", 20); ubahTengah("Reyn", 18, 5); tampil(); return 0; }
Leave a Comment