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");
}