【问题标题】:Using Queue class to queue carriages of a Train - C++使用 Queue 类对火车的车厢进行排队 - C++
【发布时间】:2011-10-07 11:58:20
【问题描述】:

我正在尝试制作一个模拟器类,该类将使用队列将车厢从火车添加到队列中,因此它将成为一个车厢队列。 Carriage 也是 train 类的 dataType。

我将使用一种方法在火车到达时将它们排入队列,并将它们添加到不同的队列中。一旦他们的货物被卸载,我也会有一种方法让火车出队。

我有一个 Train 类,它有一个私有变量:

LinkedList<Carriage> *list;

队列类有一个私有变量:

LinkedList<dataType> *list;

在我正在调用的演示文件中:

Train* train1; //declare a train.

train1 = new Train(arr); //instantiate train with an array of integers

Queue<Carriage> queue1;  //declare a queue.

queue1 = new Queue<Carriage>(train1); //instantiate queue with train data.

我的队列类有问题,我不太确定如何实现它。

我的队列类.h:

template <typename dataType> //dataType
class Queue
{

public:
           Queue();

       Queue(dataType arr[]);

           ~Queue();

           void pop();

           void push(dataType data);

private:

    LinkedList<dataType> *list;

};

#include "Queue.template"
//there is also a namespace and a macroguard, left them out of this.

编辑 - 没有添加足够的信息。

在上面的演示文件代码中,我希望能够调用 queue1 = new Queue(train1);

当我这样做时,我得到了错误,所以我知道我的构造函数做错了,因为它们都是链表,我是否需要使用循环将车厢分配到队列中?

我需要帮助的是让火车车厢进入队列。

谢谢:)

【问题讨论】:

  • 你需要让你的问题更具体。 “我不确定如何实施”不是很具体。你试过什么?你想要什么样的建议?
  • 元评论:你不认为队列是一列火车的错误抽象。想想您可以在队列上执行的操作与您可以在火车车厢上执行的操作。例如,如何在火车中间插入车厢?
  • 部分模拟使用了卸货设施,在该设施中处理火车并移除其负载,这就是火车将进入队列的原因。

标签: c++ queue linked-list simulator


【解决方案1】:

您将LinkedList 类型的参数传递给构造函数,该构造函数采用datatype 数组的参数,这会导致错误。

但是,您可以遍历每个 train1 元素,并将 push 循环到 Queue

请注意,pushpopstack 的函数名称。 Queueenqueuedequeue

【讨论】:

  • 我在某处读到,新的 C++ 库对堆栈和队列都使用 push 和 pop。那么你是说我需要更改构造函数以接受火车对象,然后在构造函数中循环遍历火车中的每个位置将其推入队列?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多