Untitled
unknown
c_cpp
2 years ago
7.4 kB
7
Indexable
#include <iostream>
using namespace std;
class Node
{
public:
string nama;
string Nim;
Node *prev;
Node *next;
};
class DoublyLinkedList
{
public:
Node *head;
Node *tail;
DoublyLinkedList()
{
head = nullptr;
tail = nullptr;
}
int countList()
{
Node *hitung;
hitung = head;
int jumlah = 0;
while (hitung != NULL)
{
jumlah++;
hitung = hitung->next;
}
return jumlah;
}
void push(string nama, string Nim)
{
Node *newNode = new Node;
newNode->nama = nama;
newNode->Nim = Nim;
newNode->prev = nullptr;
newNode->next = head;
if (head != nullptr)
{
head->prev = newNode;
}
else
{
tail = newNode;
}
head = newNode;
}
bool isEmpty()
{
return (head == NULL);
}
void insertAt(string nama, string Nim, int posisi)
{
if (posisi < 1 || posisi > countList())
{
cout << "Posisi diluar jangkauan" << endl;
return;
}
if (posisi == 1)
{
cout << "Posisi bukan posisi tengah" << endl;
return;
}
Node *baru, *bantu;
baru = new Node();
baru->nama = nama;
baru->Nim = Nim;
if (posisi > countList())
posisi = countList();
int nomor = 1;
bantu = head;
while (nomor < posisi - 1)
{
bantu = bantu->next;
nomor++;
}
baru->next = bantu->next;
bantu->next = baru;
}
void insertBelakang(string nama, string Nim)
{
Node *baru = new Node;
baru->nama = nama;
baru->Nim = Nim;
baru->next = NULL;
if (isEmpty())
{
head = tail = baru;
}
else
{
tail->next = baru;
tail = baru;
}
}
void removeAt(int position)
{
if (position <= 0)
{
cout << "Invalid position" << endl;
return;
}
if (head == nullptr)
{
cout << "List is empty" << endl;
return;
}
if (position == 1)
{
pop();
return;
}
Node *current = head;
int i = 1;
while (current != nullptr && i < position)
{
current = current->next;
i++;
}
if (current == nullptr)
{
cout << "Position out of range" << endl;
return;
}
if (current->prev != nullptr)
{
current->prev->next = current->next;
}
else
{
head = current->next;
}
if (current->next != nullptr)
{
current->next->prev = current->prev;
}
else
{
tail = current->prev;
}
delete current;
}
void pop()
{
if (head == nullptr)
{
return;
}
Node *temp = head;
head = head->next;
if (head != nullptr)
{
head->prev = nullptr;
}
else
{
tail = nullptr;
}
delete temp;
}
bool ubahBelakang(string oldnama, string newnama, string newNim)
{
Node *current = head;
while (current != nullptr)
{
if (current->nama == oldnama)
{
current->nama = newnama;
current->Nim = newNim;
return true;
}
current = current->next;
}
return false;
}
void ubahDepan(string nama, string Nim)
{
if (!isEmpty())
{
head->nama = nama;
head->Nim = Nim;
}
else
{
cout << "List masih kosong!" << endl;
}
}
void deleteAll()
{
Node *current = head;
while (current != nullptr)
{
Node *temp = current;
current = current->next;
delete temp;
}
head = nullptr;
tail = nullptr;
}
void display()
{
Node *current = head;
while (current != nullptr)
{
cout << current->nama << "\t";
cout << current->Nim << endl;
current = current->next;
}
cout << endl;
}
};
int main()
{
DoublyLinkedList list;
int arr_size = 5;
string namas[10] = {"Jawad", "Amir", "Farel", "Denis", "Anis", "Bowo", "Gahar", "Udin", "Ucok", "Budi"};
string Nims[10] = {"23300001", "2311102233", "23300003", "23300005", "23300008", "23300015", "23300040", "23300048", "23300050", "23300099"};
for (int i = 0; i < arr_size; i++)
list.push(namas[i], Nims[i]);
while (true)
{
string nama, Nim;
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 << "12. KELUAR" << endl;
int choice;
cout << "Masukkan Pilihan : ";
cin >> choice;
switch (choice)
{
case 1:
{
cout << "Masukkan NAMA : ";
cin >> nama;
cout << "Masukkan NIM : ";
cin >> Nim;
list.push(nama, Nim);
break;
}
case 2:
{
cout << "Masukan NAMA :";
cin >> nama;
cout << "Masukan NIM : ";
cin >> Nim;
list.insertBelakang(nama, Nim);
break;
}
case 3:
{
int position;
cout << "Data ingin dimasukkan pada urutan ke : ";
cin >> position;
cout << "Masukkan nama produk : ";
cin >> nama;
cout << "Masukkan harga produk : ";
cin >> Nim;
list.insertAt(nama, Nim, position);
break;
}
case 4:
{
cout << "Masukan NAMA :";
cin >> nama;
cout << "Masukan NIM : ";
cin >> Nim;
list.ubahDepan(nama, Nim);
break;
}
case 5:
{
string oldnama, newnama;
string newNim;
cout << "Masukkan produk lama : ";
cin >> oldnama;
cout << "Masukkan produk baru : ";
cin >> newnama;
cout << "Masukkan harga produk baru : ";
cin >> newNim;
bool ubahBelakangd = list.ubahBelakang(oldnama, newnama, newNim);
if (!ubahBelakangd)
{
cout << "Data tidak ditemukan" << endl;
}
break;
}
case 11:
{
list.display();
break;
}
}
}
return 0;
}Editor is loading...
Leave a Comment