Untitled

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

class stackNode {
public:
    string workerName;
    stackNode* next;
};

class Stack {
private:
    stackNode* head;
public:

    int size;
    int capacity;

    Stack() {
        head = NULL; size = 0; capacity = 100;
    }

    void push(string addedWorkerName) {
        if (size == capacity - 1) {
            cout << "Product line is full, can't push" << endl;
        }
        else {
            stackNode* newNode = new stackNode;
            newNode->workerName = addedWorkerName;
            newNode->next = head;
            head = newNode;
            size++;
        }
    }

    string top() {
        if (head == nullptr) return "Product line is empty";
        else return head->workerName;
    }

    void pop() {
        if (head == nullptr) cout << "Product line is empty, nothing to pop" << endl;
        else
        {
            stackNode* newNode = head;
            cout << "\n" << newNode->workerName << " has been removed." << endl;
            head = head->next;
            delete newNode;
            size--;
        }
    }

    void display() {
        stackNode* newNode = head;
        if (newNode == nullptr) cout << "There is no workers in this MODEL." << endl;
        while (newNode != nullptr) {
            cout << newNode->workerName << " ";
            newNode = newNode->next;
        }
    }

    void deleteAll()
    {
        stackNode* firstNode = head;
        stackNode* secondNode = head;
        while (firstNode != nullptr)
        {
            secondNode = firstNode;
            firstNode = firstNode->next;
            delete secondNode;
        }
    }
};


class queueNode {
public:
    string partName;
    int piecesNeeded;
    queueNode* next;
};

class Queue {
private:
    queueNode* rear;
    queueNode* front;
public:

    Queue() {
        rear = nullptr; front = nullptr;
    }
    void enqueue(string addedPartName, int addedPiecesNeeded)
    {
        queueNode* newwNode = new queueNode;
        newwNode->partName = addedPartName;
        newwNode->piecesNeeded = addedPiecesNeeded;
        newwNode->next = nullptr;
        if (front == nullptr) { front = newwNode; rear = newwNode; }
        else { rear->next = newwNode; rear = newwNode; }
    }

    bool dequeue()
    {
        if (front == nullptr) return false;
        else
        {
            queueNode* newNode = front;
            front = front->next;
            delete newNode;
        }
        return true;
    }

    void display(queueNode* newNode)
    {
        if (front == nullptr) cout << "There is no parts." << endl;
        else if (newNode != nullptr)
        {
            cout << newNode->partName << " and the number of pieces is: " << newNode->piecesNeeded << endl;
            display(newNode->next);
        }
    }

    void display()
    {
        display(front);
    }

    void displayInReverse()
    {
        displayInReverse(front);
    }


    void displayInReverse(queueNode* newNode)
    {
        if (newNode == nullptr) 
            return;
        else {
            displayInReverse(newNode->next);
            cout << endl << newNode->partName << " and its number is " << newNode->piecesNeeded;
        }
    }

    void deleteAll()
    {
        queueNode* firstNode = front;
        queueNode* secondNode = front;
        while (firstNode != nullptr)
        {
            secondNode = firstNode;
            firstNode = firstNode->next;
            delete secondNode;
        }
    }
};

class listNode
{
public:
    string modelName, lineSupervisorName;
    Queue neededParts;
    Stack lineWorkers;
    listNode* next;
};

class list
{
private:
    listNode* head;
public:
    list() { head = nullptr; }

    bool insert(int index, string addedModelName, string addedLineSupervisorName, Queue addedNeededParts, Stack addedLineWorkers)
    {
        if (index < 0) return false;
        if (index > 0 && head == nullptr) return false;
        listNode* newNode = new listNode;
        newNode->modelName = addedModelName;
        newNode->lineSupervisorName = addedLineSupervisorName;
        newNode->neededParts = addedNeededParts;
        newNode->lineWorkers = addedLineWorkers;
        if (index == 0)
        {
            newNode->next = head;
            head = newNode;
            return true;
        }
        if (index >= 1)
        {
            int currIndex = 0;
            listNode* currNode = head;
            while (currNode->next != nullptr && currIndex < index)
            {
                currNode = currNode->next;
                currIndex++;
            }
            newNode->next = currNode->next;
            currNode->next = newNode;
        }
        return true;
    }
    void displayNode(string searchedModelName) {
        listNode* currNode = head;
        while (currNode != nullptr && currNode->modelName != searchedModelName) { currNode = currNode->next; }
        if (currNode != nullptr)
        {
            cout << currNode->modelName << " it's production line supervisor is " << currNode->lineSupervisorName;
            cout << " and it's parts: "; currNode->neededParts.display();
            cout << " and the workers whos assigned to this production line are "; currNode->lineWorkers.display();
            cout << endl;
        }
        else cout << "NO MODEL FOUND!" << endl;
    }
    void display()
    {
        listNode* currNode = head;
        while (currNode != nullptr)
        {
            cout << endl << currNode->modelName << " his supervisor is " << currNode->lineSupervisorName;
            cout << " and it's parts: "; currNode->neededParts.display();
            cout << "and the workers whos assigned to this production line are "; currNode->lineWorkers.display();
            cout << endl;
            currNode = currNode->next;
        }
    }

    void deleteNode(string deleteModelName)
    {
        if (head == nullptr)
        {
            cout << "There is no MODELS in this company." << endl;
            return;
        }

        listNode* firstNode = head;
        listNode* secondNode = nullptr;

        while (firstNode != nullptr && firstNode->modelName != deleteModelName)
        {
            secondNode = firstNode;
            firstNode = firstNode->next;
        }
        if (firstNode != nullptr)
        {
            if (secondNode != nullptr)
            {
                secondNode->next = firstNode->next;
            }
            else head = firstNode->next;

            firstNode->lineWorkers.deleteAll();
            firstNode->neededParts.deleteAll();
            cout << "Model removed.";
            delete firstNode;
        }
        else
        {
            cout << "\nModel not found.";
        }
    }

    void removeWorker(string deleteModelName)
    {
        if (head == nullptr)
        {
            cout << "\nThere is no Workers here.";
            return;
        }
        listNode* firstNode = head;
        while (firstNode->modelName != deleteModelName && firstNode != nullptr)
        {
            firstNode = firstNode->next;
        }
        if (firstNode != nullptr)
        {
            firstNode->lineWorkers.pop();
        }
        else cout << "\nModel not found!";
    }

    void addWorker(string addModelName, string newWorkerName)
    {
        if (head == nullptr)
        {
            cout << "\nThere is no Workers here.";
            return;
        }
        listNode* firstNode = head;
        while (firstNode->modelName != addModelName && firstNode != nullptr)
        {
            firstNode = firstNode->next;
        }
        if (firstNode != nullptr)
        {
            firstNode->lineWorkers.push(newWorkerName);
        }
        else cout << "\nModel not found!";
    }

};

class treeNode {
public:

    treeNode* left;
    treeNode* right;
    string modelName, lineSupervisorName;
    Queue neededParts;
    Stack lineWorkers;
    treeNode() {
        left = right = nullptr;
    }
    treeNode(string addedModelName, string addedLineSupervisorName, Queue addedNeededParts, Stack addedLineWorkers) {
        modelName = addedModelName;
        lineSupervisorName = addedLineSupervisorName;
        neededParts = addedNeededParts;
        lineWorkers = addedLineWorkers;
        left = right = nullptr;
    }
};

class bst {
private:
    treeNode* root;
public:

    bst() { root = nullptr; }

    bool insert(string addedModelName, string addedLineSupervisorName, Queue addedNeededParts, Stack addedLineWorkers) {
        if (root == nullptr) {
            root = new treeNode(addedModelName, addedLineSupervisorName, addedNeededParts, addedLineWorkers);
            return true;
        }
        else {
            treeNode* p = root; //search
            treeNode* q = root; //insert
            while (p != nullptr)
            {
                q = p;
                if (addedModelName < p->modelName)
                    p = p->left;
                else if (addedModelName > p->modelName)
                    p = p->right;
                else
                    return false;
            }
            if (addedModelName < q->modelName)
                q->left = new treeNode(addedModelName, addedLineSupervisorName, addedNeededParts, addedLineWorkers);
            else
                q->right = new treeNode(addedModelName, addedLineSupervisorName, addedNeededParts, addedLineWorkers);
            return true;
        }
    }

    bool searchAndDisplay(string searchingModelName)
    {
        if (root == nullptr) { return false; }
        else
        {
            //search
            treeNode* p = root;
            while (p != nullptr)
            {
                if (searchingModelName < p->modelName)
                    p = p->left;
                else if (searchingModelName > p->modelName)
                    p = p->right;
                else
                {
                    cout << p->modelName << " it's production line supervisor is " << p->lineSupervisorName;
                    cout << " and it's parts: "; p->neededParts.display();
                    cout << " and the workers whos assigned to this production line are "; p->lineWorkers.display();
                    cout << endl;

                    return true;
                }

            }
            cout << "NO MODEL FOUND!" << endl;
            return false;
        }
    }

    treeNode* deleteNode(string deletingModelName)
    {
        root = deleteNode(root, deletingModelName);
        return  root;
    }

    treeNode* deleteNode(treeNode* root, string deletingModelName)
    {

        if (root == nullptr)
            return root;

        if (deletingModelName < root->modelName)
            root->left = deleteNode(root->left, deletingModelName);

        else if (deletingModelName > root->modelName)
            root->right = deleteNode(root->right, deletingModelName);

        else
        {
            if (root->left == nullptr && root->right == nullptr)
                return 0;

            else if (root->left == nullptr) {
                treeNode* temp = root->right;
                delete(root);
                return temp;
            }
            else if (root->right == nullptr) {
                treeNode* temp = root->left;
                delete(root);
                return temp;
            }

            treeNode* temp = minValueNode(root->right);

            root->modelName = temp->modelName;

            root->right = deleteNode(root->right, temp->modelName);
        }
        return root;
    }

    treeNode* minValueNode(treeNode* root) {
        while (root->left != nullptr) {
            root = root->left;
        }
        return root;
    }

    void displayModelParts(string modelName)
    {
        treeNode* p = root;
        while (p != nullptr)
        {
            if (modelName < p->modelName)
                p = p->left;
            else if (modelName > p->modelName)
                p = p->right;
            else
            {
                p->neededParts.display();
            }

        }
    }

    void displayModelPartsInReverse(string modelName)
    {
        treeNode* p = root;
        while (p != nullptr && p->modelName!= modelName)
        {
            if (modelName < p->modelName)
                p = p->left;
            else if (modelName > p->modelName)
                p = p->right;
        }
        p->neededParts.displayInReverse();
    }

    void displayInOrder()
    {
        displayInOrder(root);
    }

    void displayInOrder(treeNode* Newroot)
    {
        if (Newroot != nullptr)
        {
            displayInOrder(Newroot->left);

            cout << Newroot->modelName << " it's production line supervisor is " << Newroot->lineSupervisorName;
            cout << " and it's parts: ";
            Newroot->neededParts.display();
            cout << " and the workers whos assigned to this production line are ";
            Newroot->lineWorkers.display();
            cout << endl;

            displayInOrder(Newroot->right);
        }
    }

    void removeWorker(string deleteModelName)
    {
        if (root == nullptr)
        {
            cout << "\nThere is no Workers here.";
            return;
        }
        else {
            //search
            treeNode* p = root;
            while (p != nullptr)
            {
                if (deleteModelName < p->modelName)
                    p = p->left;
                else if (deleteModelName > p->modelName)
                    p = p->right;
                else
                {
                    p->lineWorkers.pop();
                    return;
                }

            }
            cout << "NO MODEL FOUND!" << endl;
            return;
        }
    }

    void addWorker(string addModelName, string newWorkerName)
    {
        treeNode* firstNode = root;
        while (firstNode->modelName != addModelName && firstNode != nullptr)
        {
            if (addModelName < firstNode->modelName)
                firstNode = firstNode->left;
            else if (addModelName > firstNode->modelName)
                firstNode = firstNode->right;
        }
        if (firstNode != nullptr)
        {
            firstNode->lineWorkers.push(newWorkerName);
        }
        else cout << "\nModel not found!";
    }
};

int main()
{
    bst shamCars2;
    Queue firstModelParts; Stack firstModelWorkers;
    firstModelParts.enqueue("dalob", 4); firstModelParts.enqueue("Radio", 1); firstModelParts.enqueue("Krse", 5);
    firstModelWorkers.push("moaz"); firstModelWorkers.push("yazan"); firstModelWorkers.push("kareem");
    shamCars2.insert("SHAM1", "ZAKI", firstModelParts, firstModelWorkers);

    Queue secondModelParts; Stack secondModelWorkers;
    shamCars2.insert("SHAM2", "NEWADMIN", secondModelParts, secondModelWorkers);

    Queue thirdModelParts; Stack thirdModelWorkers;
    thirdModelParts.enqueue("Window", 4); thirdModelParts.enqueue("Drkson", 1); thirdModelParts.enqueue("Engine", 2);
    thirdModelWorkers.push("laith"); thirdModelWorkers.push("fadi"); thirdModelWorkers.push("rami");
    shamCars2.insert("SHAM3", "SARIA", thirdModelParts, thirdModelWorkers);

    string deleteForModel;
    cout << "Enter model name you want to delete it's data: "; cin >> deleteForModel;
    shamCars2.deleteNode(deleteForModel);

    string searchForModel;
    cout << "Enter model name you want to display it's data: "; cin >> searchForModel;
    shamCars2.searchAndDisplay(searchForModel);
    bool editQue;
    cout << "Do you want to edit any model WORKERS? (1 or 0)"; cin >> editQue;
    if (editQue)
    {
        string modelNameForEdit;
        cout << "enter the model name: "; cin >> modelNameForEdit;
        int selectAnEdit;
        cout << "1 to add a worker and 2 to remove: "; cin >> selectAnEdit;
        while (selectAnEdit != 1 && selectAnEdit != 2) { cout << "Wrong choice! enter another value:"; cin >> selectAnEdit; }
        if (selectAnEdit == 1) { string addingNewWorker; cout << "Enter the worker name: "; cin >> addingNewWorker; shamCars2.addWorker(modelNameForEdit, addingNewWorker); }
        else shamCars2.removeWorker(modelNameForEdit);
    }

    cout << endl << "The Model in  alphabetical order are: \n";
    shamCars2.displayInOrder();

    string inReverseParts;
    cout << "Enter the model name to see it's parts in reverse: "; cin >> inReverseParts;
    shamCars2.displayModelPartsInReverse(inReverseParts);

    /* ------------------------------------------------------------------------------------------------------------------- */

    list shamCars;
    shamCars.insert(1, "SHAM1", "ZAKI", firstModelParts, firstModelWorkers);
    shamCars.insert(2, "SHAM2", "NEWADMIN", secondModelParts, secondModelWorkers);
    shamCars.insert(3, "SHAM3", "SARIA", thirdModelParts, thirdModelWorkers);
    cout << "Enter model name you want to display it's data: "; cin >> searchForModel;
    shamCars.displayNode(searchForModel);
    cout << "Do you want to edit any model WORKERS? (1 or 0)"; cin >> editQue;
    if (editQue)
    {
        string modelNameForEdit;
        cout << "enter the model name: "; cin >> modelNameForEdit;
        int selectAnEdit;
        cout << "1 to add a worker and 2 to remove: "; cin >> selectAnEdit;
        while (selectAnEdit != 1 && selectAnEdit != 2) { cout << "Wrong choice! enter another value:"; cin >> selectAnEdit; }
        if (selectAnEdit == 1) { string addingNewWorker; cout << "Enter the worker name: "; cin >> addingNewWorker; shamCars.addWorker(modelNameForEdit, addingNewWorker); }
        else shamCars.removeWorker(modelNameForEdit);
    }
    return 0;
}
Leave a Comment