0

I am having a problem in moving a node from one queue to another in my program. I am implementing Queue using singly linked list in c++. I am making a Corona Test Program. There are three objects of queues testingQueue, quarantineQueue and homeQueue. The user adds people in testing queue one by one and after adding, user checks each person from testing queue for corona test one by one (using FIFO method cuz its a queue). The test result is randomized, it can be positive or negative. If result is positive then that person is sent to quarantineQueue and if result is negative, the person is sent to homeQueue. My program is running fine, there are no errors but when a person is sent to quarantineQueue and after that i run displayQueue() function for quarantineQueue it does not show any person in there. Please Help.

Complete Code:

#include <string>
#include <stdlib.h>
#include <cstdlib>
using namespace std;

class Person {
public:
    string name;
    string gender;
    int age;
    string testType;
    string result;
    Person* next;
    Person() {
        testType = "Corona Test";
    }
};

class Queue {
public:
    Person* rear;
    Person* front;
public:
    Queue() {
        rear = NULL;
        front = NULL;
    }
    void addPerson() {
        Person* temp = new Person;
        cout << "Enter Name Of The Person: ";
        cin.ignore();
        getline(cin, temp->name);
        cout << "Enter Gender Of The Person: ";
        cin >> temp->gender;
        cout << "Enter Age Of The Person: ";
        cin >> temp->age;
        temp->next = NULL;
        if (front == NULL) {
            front = temp;
            rear = temp;
        }
        else {
            rear->next = temp;
            rear = temp;
        }
    }

    void checkPerson(Queue qq,Queue hq) {
        Person* temp = front;
        int a = rand() % 100 + 1;
        if (a < 50) {
            temp->result = "Positive";
            if (qq.front == NULL) {
                front = temp->next;
                qq.front = temp;
                qq.rear = temp;
                temp->next = NULL;
            }
            else {
                front = temp->next;
                qq.rear->next = temp;
                qq.rear = temp;
                temp->next = NULL;
            }
            cout << "Result Was Positive And Person Has Been Moved To Quarantine." << endl;
        }
        else {
            temp->result = "Negative";
            if (hq.front == NULL) {
                front = temp->next;
                hq.front = temp;
                hq.rear = temp;
                temp->next = NULL;
            }
            else {
                front = temp->next;
                hq.rear->next = temp;
                hq.rear = temp;
                temp->next = NULL;
            }
            cout << "Result Was Negative And Person Has Been Moved To Home." << endl;
        }
    }

    void displayQueue() {
        Person* temp = front;
        while (temp != NULL) {
            cout << endl;
            cout << "Name Of Person: " << temp->name << endl;
            cout << "Gender Of Person: " << temp->gender << endl;
            cout << "Age Of Person: " << temp->age << endl;
            cout << "Test Type Of Person: " << temp->testType << endl;
            cout << "Result Of Person: " << temp->result << endl << endl;
            temp = temp->next;
        }
    }
};

void menu() {
    cout << "\n\n\t\t\t\t   ----------------------------------------------------\n";
    cout << "\t\t\t\t   |        1.Add a Person To Testing Queue           |\n";
    cout << "\t\t\t\t   |       2.Check a Person From Testing Queue        |\n";
    cout << "\t\t\t\t   |     3.Recover a Person From Quarantine Queue     |\n";
    cout << "\t\t\t\t   |      4.Display All People In Testing Queue       |\n";
    cout << "\t\t\t\t   |     5.Display All People In Quarantine Queue     |\n";
    cout << "\t\t\t\t   |         6. Display All People At Home            |\n";
    cout << "\t\t\t\t   |               7. Exit Program                    |\n";
    cout << "\t\t\t\t   ----------------------------------------------------\n";
}

int main() {
    Queue testingQueue, quarantineQueue, homeQueue;
    int op;
Fahad:
    menu();
    cout << "Enter Option: ";
    cin >> op;
    switch (op) {
    case 1:
        testingQueue.addPerson();
        cout << "A Person Has Been Added To Testing Queue." << endl;
        system("pause");
        system("cls");
        goto Fahad;
    case 2:
        testingQueue.checkPerson(quarantineQueue, homeQueue);
        system("pause");
        system("cls");
        goto Fahad;
    case 3:
        system("pause");
        system("cls");
        goto Fahad;
    case 4:
        testingQueue.displayQueue();
        system("pause");
        system("cls");
        goto Fahad;
    case 5:
        quarantineQueue.displayQueue();
        system("pause");
        system("cls");
        goto Fahad;
    case 6:
        homeQueue.displayQueue();
        system("pause");
        system("cls");
        goto Fahad;
    case 7:
        return 0;
    default:
        cout << "Invalid Option." << endl;
        system("pause");
        system("cls");
        goto Fahad;
    }
    system("pause");
}
Fahad
  • 37
  • 7
  • 1
    One problem is that the parameters to `checkPerson` are copies. Any changes in the function are to the local copies, and not the original back at the call site. You should pass the parameters by reference (`checkPerson(Queue &qq, Queue &hq)`). – 1201ProgramAlarm May 29 '20 at 01:16
  • @1201ProgramAlarm Thank you so much, now i understand the concept of pass by value and pass by reference. Thank you. – Fahad May 29 '20 at 09:57

1 Answers1

1

Consider duplicating the interface for std::queue:

back    Returns a reference to the last element of the queue.
front   Returns a reference to the first element of the queue.
pop     Removes an element from the front of the queue.
push    Adds an element to the back of the queue.
empty   Tests if the queue is empty.
size    Returns the number of elements in the queue.
~Person deletes all nodes in the queue, then deletes the queue.

Rather than constantly allocating and freeing nodes, you could consider changing the interface to work with pointers to nodes, and add two more functions:

new     creates a node, returns a pointer to the node.
delete  deletes a node (input parameter is pointer to node).
back    Returns a pointer to the last  node of the queue.
front   Returns a pointer to the first node of the queue.
pop     Returns a pointer to what was the first node of the queue, which is now removed.
push    Appends an element via pointer to the back of the queue.
empty   Tests if the queue is empty.
size    Returns the number of elements in the queue.
~Person deletes all nodes in the queue, then deletes the queue.
rcgldr
  • 27,407
  • 3
  • 36
  • 61