Untitled

mail@pastecode.io avatar
unknown
c_cpp
18 days ago
11 kB
9
Indexable
Never
#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 hapusTengah(int posisi) {
    if (posisi < 1 || posisi > hitungList()) {
        cout << "Posisi diluar jangkauan" << endl;
    } else if (posisi == 1) {
        cout << "Posisi bukan posisi tengah" << endl;
    } else {
        Node *bantu = head;
        for (int i = 1; i < posisi - 1; i++) {
            bantu = bantu->next;
        }
        Node *hapus = bantu->next;
        bantu->next = hapus->next;
        delete hapus;
    }
    }
    void pop()
    {
        if (head == nullptr)
        {
            return;
        }
        Node *temp = head;
        head = head->next;
        if (head != nullptr)
        {
            head->prev = nullptr;
        }
        else
        {
            tail = nullptr;
        }
        delete temp;
    }

    void ubahBelakang(string nama, string Nim) {
    if (!isEmpty()) {
        tail->nama = nama;
        tail->Nim = Nim;
    } else {
        cout << "List masih kosong!" << endl;
    }
    }

    void ubahDepan(string nama, string Nim)
    {
        if (!isEmpty())
        {
            head->nama = nama;
            head->Nim = Nim;
        }
        else
        {
            cout << "List masih kosong!" << endl;
        }
    }

    int hitungList() {
    Node *hitung = head;
    int jumlah = 0;
    while (hitung != NULL) {
        jumlah++;
        hitung = hitung->next;
    }
    return jumlah;
    }

    void ubahTengah(string nama, string Nim, int posisi) {
        if (!isEmpty()) {
            if (posisi < 1 || posisi > hitungList()) {
                cout << "Posisi di luar jangkauan" << endl;
            } else if (posisi == 1) {
                cout << "Posisi bukan posisi tengah" << endl;
            } else {
                Node *bantu = head;
                for (int i = 1; i < posisi; i++) {
                bantu = bantu->next;
                }
                bantu->nama = nama;
                bantu->Nim = Nim;
            }
        } else {
            cout << "List masih kosong!" << endl;
        }
    }

    void hapusDepan() {
    if (!isEmpty()) {
        Node *hapus = head;
        if (head->next != NULL) {
            head = head->next;
        } else {
            head = tail = NULL;
        }
        delete hapus;
        } else {
            cout << "List kosong!" << endl;
        }
    }

    void hapusBelakang() { 
    if (!isEmpty()) {
        Node *hapus = tail;
        if (head != tail) {
            Node *bantu = head;
            while (bantu->next != tail) {
                bantu = bantu->next;
            }
            tail = bantu;
            tail->next = NULL;
        } else {
            head = tail = NULL;
        }
        delete hapus;
    } else {
        cout << "List 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 = 10;
    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.insertBelakang(namas[i], Nims[i]);

    while (true)
    {
        string nama, Nim;
        cout << endl;
        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;
        cout << endl;
        switch (choice)
        {
            case 1:
            {
                cout << "Masukkan NAMA : ";
                cin >> nama;
                cout << "Masukkan NIM  : ";
                cin >> Nim;
                cout << "Data telah di tambahkan" << endl;
                list.push(nama, Nim);
                break;
            }
            case 2:
            {

                cout << "Masukan NAMA :";
                cin >> nama;
                cout << "Masukan NIM : ";
                cin >> Nim;
                cout << "Data telah di tambahkan" << endl;
                list.insertBelakang(nama, Nim);
                break;
            }
            case 3:
            {
                int position;

                cout << "Masukkan Nama  : ";
                cin >> nama;
                cout << "Masukkan Nim   : ";
                cin >> Nim;
                cout << "Posisi         : ";
                cin >> position;
                list.insertAt(nama, Nim, position);
                break;
            }
            case 4:
            {
                cout << "Masukan NAMA :";
                cin >> nama;
                cout << "Masukan NIM : ";
                cin >> Nim;
                cout << "Data berhasil depan di ubah" << endl ;
                list.ubahDepan(nama, Nim);
                break;
            }
            case 5:
            {
                cout << "Masukkan nama  : ";
                cin >> nama;
                cout << "Masukkan Nim   : ";
                cin >> Nim;
                cout << "Data belakang berhasil di ubah" << endl;
                list.ubahBelakang(nama, Nim);
                break;
            }
            case 6:
            {
                int position;

                cout << "Masukkan nama   : ";
                cin >> nama;
                cout << "Masukkan Nim    : ";
                cin >> Nim;
                cout << "Masukkan posisi : ";
                cin >> position;

                if (position < 1 || position > list.countList()) {
                    cout << "Posisi diluar jangkauan" << endl;
                } else {
                    string namaLama = "";
                    Node *current = list.head;
                    for (int i = 1; i < position; ++i) {
                        current = current->next;
                    }
                    if (current != nullptr) {
                        namaLama = current->nama;
                    }
                    list.ubahTengah(nama, Nim, position);
                    cout << "Data " << namaLama << " telah diganti dengan " << nama << endl;
                }
                break;
            }
            case 7:
            {   
                cout << "DATA BERHASIL DI HAPUS" << endl;
                list.hapusDepan();
                break;
            }
            case 8:
            {
                if (!list.isEmpty()) {
                    cout << "Data " << list.tail->nama << " berhasil dihapus" << endl;
                    list.hapusBelakang(); // Menghapus elemen terakhir dari linked list
                } else {
                    cout << "List kosong, tidak ada data untuk dihapus" << endl;
                }
            break;
            }
            case 9:
            {
                int posisi;
                cout << "Masukkan posisi data yang ingin dihapus: ";
                cin >> posisi;

                if (posisi < 1 || posisi > list.countList()) {
                    cout << "Posisi diluar jangkauan" << endl;
                } else {
                    Node *current = list.head;
                    for (int i = 1; i < posisi; ++i) {
                        current = current->next;
                    }
                    cout << "Data " << current->nama << " berhasil dihapus" << endl;
                    list.hapusTengah(posisi);
                }
                break;
            }
            case 10:
            {
                cout << "DATA BERHASIL DI HAPUS" << endl;

                list.deleteAll();
            break;
            }
            case 11:
            {
                cout << "DATA MAHASISWA" << endl;
                list.display();
                break;
            }
            case 12:
            {  
                return 0;
            }
        }
    }
    return 0;
}
Leave a Comment