Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
7.3 kB
5
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 push(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 insertAt(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)
        {
            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 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 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->product << "\t";
            cout << current->price << endl;
            current = current->next;
        }
        cout << endl;
    }
};

int main()
{
    DoublyLinkedList list;

     int arr_size = 5;

   double prices[5];
    string products[5];

    for (int i = arr_size-1; i >= 0; i--)
    {  
        printf("Masukkan nama produk ke%d : ", i-arr_size);
        cin >> products[i];
        printf("Masukkan harga produk ke%d: ", i-arr_size);
        cin >> prices[i];
        printf("\n");
    }

    for (int i = 0; i < arr_size; i++)
        list.push(products[i],prices[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.push(product, price);
            break;
        }
        case 2:
        {
            list.pop();
            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.insertAt(product, price, position);
            break;
        }
        case 5:
        {
            int position;

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

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