如何通过队列指针访问存储在节点中的类中的数据

How do I access data in a class stored in a node through queue pointers?

本文关键字:节点 数据 存储 何通过 队列 指针 访问      更新时间:2023-10-16

在我当前的任务中,我很难弄清楚如何访问这段特定的数据。

首先,分配要求从文件中提取数据,以模拟正常运行的商店。唯一的问题是,所提取的数据是关于客户的。具体来说,就是客户何时进入队列,以及收银员处理订单需要多长时间。

现在我如何将客户数据存储在一组类中。

for(int i = 0; i < entries; i++)
{  
/* !!!!!IMPORTANT!!!!!  
* The way this program extracts file data assumes that the file will  
follow a specific format  
* results will vary heavily based on how the file is set up  
*   
* before docking points please make sure that the file follows the format  
*   
*  "Number of entries"  
*  "Customer Number", "Arrival Time", "Service Time"  
*  "Customer Number", "Arrival Time", "Service Time"  
*  "Customer Number", "Arrival Time", "Service Time"   
*/
xfiles >> dataEntry;
fileData[i].setNumber(dataEntry);
//inserts a number from the file into the "number" variable in the customer class.
xfiles >> dataEntry;
fileData[i].setArrival(dataEntry);
//inserts a number from the file into the "arrival" variable in the customer class.
xfiles >> dataEntry;
fileData[i].setServTime(dataEntry);
//inserts a number from the file into the "servTime" variable in the customer class.
} 
xfiles.close();

它没有包含在代码中,但有一行考虑了程序早期的条目。

在我的下一个区块中,我必须在一段时间内同时排队和处理客户。我知道应该如何对它们进行排队,但我不太确定应该如何处理它们。据我目前所知,我相信我可能想做一个条件语句来检查阵列中的某个客户是否已经排队。

我目前试图访问的数据是存储在类中的到达时间。

所以类似于,
fileData[i].returnArrival();

但是由于这个类存储在一个队列中,我不确定如何访问它

现在我如何让所有东西排队是

for(int x = 0; x < 570; x++)
{
if(cusTime == x)
{
if(scully.isFull() = false)
scully.enqueue(fileData[cusTime]);
else if(mulder.isFull() = false)
mulder.enqueue(fileData[cusTime]);
else if(skinner.isFull() = false)
skinner.enqueue(fileData[cusTime]);
else
cout << "queues are full, disposing..n";
}
cusTime++;
}

一开始我以为它会像

scully.returnFront()->temp->returnClass()->fileData.returnArrival();  

但我不确定,因为temp只是在队列类中声明的指针。

我的一个朋友提出了另一个建议,他建议可能是这样的,但当我运行代码时,我最终出现了分段错误。

scully.returnFront()->returnClass().returnArrival();

我认为应该是以下内容:

scully.returnFront().returnArrival()

因为您将数组中的项目排入队列。因此,returnFront()检索一个项目,您的方法应该可以在该项目上使用。

与教授和TA讨论后,问题的原因是return front函数返回了一个指针,使访问节点内的数据变得更加困难。一个解决方案是让return front函数返回与数据关联的Class,并让return语句返回一个指针,该指针指向返回存储在节点中的类的类函数。

所以

Node *returnFront();

已更改为

Customer returnFront();

功能内的变化是

return front;

return front->returnClass();

这些更改使从主文件内部访问Customer类数据变得更加容易。因此,我能够为类实例化一个新的占位符变量。

Customer scullyTemp;

然后存储类内部的数据,这些数据通过赋值语句存储在节点中。

scullyTemp = scully.returnFront();
scullyTemp.returnArrival();

它可能比它需要的要复杂一点,但现在它做了我需要它做的