Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
3.2 kB
3
Indexable
Never
#include <iostream>
#include <string>

using namespace std;

struct Order {
    string name;
    Order* next;
};

class CircularLinkedList {
private:
    Order* head;
    int maxOrders;
    int currentOrders;

public:
    CircularLinkedList(int m) : head(nullptr), maxOrders(m), currentOrders(0) {}

    void placeOrder(const string& customerName) {
        if (currentOrders >= maxOrders) {
            cout << "Order limit reached. Cannot place more orders." << endl;
            return;
        }

        Order* newOrder = new Order{customerName, nullptr};

        if (!head) {
            head = newOrder;
            newOrder->next = head; // Point to itself
        } else {
            Order* temp = head;
            while (temp->next != head) {
                temp = temp->next;
            }
            temp->next = newOrder;
            newOrder->next = head; // Make it circular
        }
        currentOrders++;
        cout << "Order placed: " << customerName << endl;
    }

    void serveOrder() {
        if (!head) {
            cout << "No orders to serve." << endl;
            return;
        }

        cout << "Serving order: " << head->name << endl;

        if (head->next == head) {
            delete head; // Only one order
            head = nullptr;
        } else {
            Order* temp = head;
            while (temp->next != head) {
                temp = temp->next;
            }
            Order* toDelete = head;
            head = head->next;
            temp->next = head; // Update the last order to point to new head
            delete toDelete;
        }
        currentOrders--;
    }

    void displayOrders() {
        if (!head) {
            cout << "No current orders." << endl;
            return;
        }

        Order* temp = head;
        do {
            cout << "Order: " << temp->name << endl;
            temp = temp->next;
        } while (temp != head);
    }

    ~CircularLinkedList() {
        while (head) {
            serveOrder();
        }
    }
};

int main() {
    int maxOrders;
    cout << "Enter the maximum number of orders: ";
    cin >> maxOrders;

    if (maxOrders <= 0) {
        cout << "Maximum orders must be greater than zero." << endl;
        return 1; // Exit with error code
    }

    CircularLinkedList orders(maxOrders);
    int choice;
    string customerName;

    do {
        cout << "\nMenu:\n";
        cout << "1. Place Order\n";
        cout << "2. Serve Order\n";
        cout << "3. Display Orders\n";
        cout << "4. Exit\n";
        cout << "Enter your choice: ";
        cin >> choice;

        switch (choice) {
            case 1:
                cout << "Enter customer name for the order: ";
                cin >> customerName;
                orders.placeOrder(customerName);
                break;
            case 2:
                orders.serveOrder();
                break;
            case 3:
                orders.displayOrders();
                break;
            case 4:
                cout << "Exiting program." << endl;
                break;
            default:
                cout << "Invalid choice. Please try again." << endl;
        }
    } while (choice != 4);

    return 0;
}
Leave a Comment