Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
6.9 kB
2
Indexable
Never
#include <iostream>
using namespace std;

class Node
{
public:
    string product;
    double price;
    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 tambah(string product, double price)
    {
        Node *newNode = new Node;
        newNode->product = product;
        newNode->price = price;
        newNode->prev = nullptr;
        newNode->next = head;
        if (head != nullptr)
        {
            head->prev = newNode;
        }
        else
        {
            tail = newNode;
        }
        head = newNode;
    }

    void tambahAt(string product, double price, 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->product = product;
        baru->price = price;

        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 removeAt(int position)
    {
        if (position <= 0)
        {
            cout << "Invalid position" << endl;
            return;
        }

        if (head == nullptr)
        {
            cout << "List is empty" << endl;
            return;
        }

        if (position == 1)
        {
            hapus();
            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 hapus()
    {
        if (head == nullptr)
        {
            return;
        }
        Node *temp = head;
        head = head->next;
        if (head != nullptr)
        {
            head->prev = nullptr;
        }
        else
        {
            tail = nullptr;
        }
        delete temp;
    }

    bool update(string oldProduct, string newProduct, double newPrice)
    {
        Node *current = head;
        while (current != nullptr)
        {
            if (current->product == oldProduct)
            {
                current->product = newProduct;
                current->price = newPrice;
                return true;
            }
            current = current->next;
        }
        return false;
    }

    void hapusAll()
    {
        Node *current = head;
        while (current != nullptr)
        {
            Node *temp = current;
            current = current->next;
            delete temp;
        }
        head = nullptr;
        tail = nullptr;
    }

    void muncul()
    {
        Node *current = head;
        while (current != nullptr)
        {
            cout << current->product << "\t";
            cout << current->price << endl;
            current = current->next;
        }
        cout << endl;
    }
};

int main()
{
    DoublyLinkedList list;

     int arr_size = 5;

    double harga[5];
    string produkk[5];

    for (int i = arr_size-1; i >= 0; i--)
    {  
        cout << "Masukkan nama produk : ";
        cin >> produkk[i];
        cout << "Masukkan harga : ";
        cin >> harga[i];
        printf("\n");
    }

    for (int i = 0; i < arr_size; i++)
    {
        list.tambah(produkk[i],harga[i]);
    }

    while (true)
    {
        cout << "TOKO SKINCARE PURWOKERTO" << endl;
        cout << "1. Tambah Data" << endl;
        cout << "2. Hapus data" << endl;
        cout << "3. Update data" << endl;
        cout << "4. Tambah Data Urutan Tertentu" << endl;
        cout << "5. Hapus Data Urutan Tertentu" << endl;
        cout << "6. Hapus Seluruh Data" << endl;
        cout << "7. Tampilkan Data" << endl;
        cout << "8. Exit" << endl;
        int choice;
        cout << "Masukkan Pilihan : ";
        cin >> choice;

        switch (choice)
        {
        case 1:
        {
            string product;
            double price;

            cout << "Masukkan nama produk : ";
            cin >> product;
            cout << "Masukkan harga produk : ";
            cin >> price;
            list.tambah(product, price);
            break;
        }
        case 2:
        {
            list.hapus();
            break;
        }
        case 3:
        {
            string oldProduct, newProduct;
            double newPrice;

            cout << "Masukkan produk lama       : ";
            cin >> oldProduct;
            cout << "Masukkan produk baru       : ";
            cin >> newProduct;
            cout << "Masukkan harga produk baru : ";
            cin >> newPrice;

            bool updated = list.update(oldProduct, newProduct, newPrice);
            if (!updated)
            {
                cout << "Data tidak ditemukan" << endl;
            }
            break;
        }
        case 4:
        {
            string product;
            double price;
            int position;

            cout << "Data ingin dimasukkan pada urutan ke        : ";
            cin >> position;
            cout << "Masukkan nama produk       : ";
            cin >> product;
            cout << "Masukkan harga produk       : ";
            cin >> price;

            list.tambahAt(product, price, position);
            break;
        }
        case 5:
        {
            int position;

            cout << "Masukkan data yang ingin dihapus :";
            cin >> position;

            list.removeAt(position);
            break;
        }
        case 6:
        {
            list.hapusAll();
            break;
        }
        case 7:
        {
            list.muncul();
            break;
        }
        case 8:
        {
            return 0;
        }
        default:
        {
            cout << "Invalid choice" << endl;
            break;
        }
        }
    }
    return 0;
}
Leave a Comment