【发布时间】:2020-05-28 18:55:34
【问题描述】:
我在程序中将节点从一个队列移动到另一个队列时遇到问题。我在 C++ 中使用单链表实现队列。我正在制作电晕测试程序。队列有testingQueue、quarantineQueue和homeQueue三个对象。用户将人员一一添加到测试队列中,添加后,用户从测试队列中逐一检查每个人是否进行电晕测试(使用FIFO方法,因为它是一个队列)。测试结果是随机的,可以是阳性或阴性。如果结果为正,则将该人发送到隔离队列,如果结果为负,则将该人发送到 homeQueue。我的程序运行良好,没有错误,但是当一个人被发送到隔离队列之后,我为隔离队列运行 displayQueue() 函数,它没有显示任何人在那里。请帮忙。
完整代码:
#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");
}
【问题讨论】:
-
一个问题是
checkPerson的参数是副本。功能中的任何更改都是本地副本,而不是调用站点的原始副本。您应该通过引用 (checkPerson(Queue &qq, Queue &hq)) 传递参数。 -
@1201ProgramAlarm 非常感谢,现在我明白了按值传递和按引用传递的概念。谢谢。
标签: c++ linked-list queue