【问题标题】:How to store multiple queue in array?如何在数组中存储多个队列?
【发布时间】:2020-01-22 20:21:48
【问题描述】:

我有一个队列类,我在其中实现队列结构。

#include "queue.h"

Queue::Queue()
{


}

Queue::Queue(int size){
    front = rear = -1;
    this->size = size;
    Q = new int[size];
}

void Queue::enqueue(int x){
    if (rear == size -1 ){
        cout << " its full" << endl;
    }else{
        rear ++;
        Q[rear] = x;
    }
}

int Queue::dequeue(){
    int x= -1;
    if (rear == front){
        cout << " its empty"<<endl;
    }else{
        front ++;
        x = Q[front];
    }

    return x;
}

void Queue::Display(){
    for(int i= front+1; i<=rear; i++){
        cout << Q[i] << "  ";
    }

    cout << endl;
}

bool Queue::isEmpty(){
    return (size==0);
}
int Queue::peek()
{
    if (isEmpty())
    {
        cout << "UnderFlow\nProgram Terminated\n";
        exit(EXIT_FAILURE);
    }
    return Q[front];
}

在 main.cpp 中,我创建了多个队列。我正在尝试实现一种调度算法,我需要按顺序处理每个队列。当我尝试遍历每个队列时,问题就开始了。我想只使用一个 for 循环来访问每个队列的元素,而不是每个队列的 for 循环。

示例:

queue[1..N] 其中 N 是队列数。在 for 循环中,我想检查 if queue[i].empty()

【问题讨论】:

    标签: c++ queue


    【解决方案1】:

    我找到了解决问题的方法。在 main.cpp 中,下面的代码解决了这个问题。

    Queue allQueues[4];
    
    allQueues[0] = queue1;
    allQueues[1] = queue2;
    allQueues[2] = queue3;
    allQueues[3] = queue4;
    

    访问:

    for(int i=0; i<4; i++){
    
        if allQueues[i].empty(){
            //do something
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果您需要生成特定数量的 Queue 类实例,这些实例在编译时是固定的且已知的,那么您的代码解决方案将起作用。但是,如果您有一个程序需要在程序运行时创建新的 Queue 实例,则需要在堆上使用动态内存分配。

      一种方法是在 main.cpp 中创建指向 Queue 类的数组或指针向量。 std::vector 更灵活,最好使用智能指针来创建队列的每个实例,尽管许多学术课程不允许使用标准模板库或智能指针,在这种情况下你只需要一个普通的指向 Queue 的指针数组,并适当地使用 new 和 delete。

      const int SIZE = 100  //max number of Queue instances
      Queue* allQueues[SIZE]; //array of uninitialized pointers to Queue
      for (int i = 0; i < SIZE; i++) {  //ensure all pointers are set to null
          allQueues[i] = nullptr;
      }
      
      //To make a new Queue instance and insert it into the array:
      allQueues[0] = new Queue();
      //And when done with that Queue instance, to avoid memory leaks and dangling pointers:
      delete allQueues[0];
      allQueues[0] = nullptr;
      

      (使用 std::array 或 std::vector 和智能指针会更好)。 还要注意内存使用情况,如果没有这种方法,您将拥有两个用于 queue1 的完整队列实例,而不是对象本身和指向该对象的指针。但是,也可以只使用自动堆栈分配来完成指针数组的事情,但在这种情况下,您不希望在运行时创建新对象。为此,很简单:

      Queue* allQueues[4];
      allQueues[0] = &queue1;
      //etc.
      

      附:您的解决方案的一个问题是,当您执行此作业时:

      allQueues[0] = queue1;
      

      您需要在类中使用复制构造函数或重载的“=”运算符,以确保将 queue1 的所有内部结构正确复制到 Queue 对象数组中,并避免所有“浅拷贝”问题。

      Queue::Queue(const Queue& copySource) {
          this->size = copysource.size;
          this->Q = new int[copysource.size];
          for (int i = 0; i < size; i++) {
              this->Q[i] = copysource.Q[i];
          }
      

      见: Why can I access private variables in the copy constructor?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-15
        • 2013-11-08
        • 2014-05-16
        • 2012-11-20
        • 1970-01-01
        • 2020-03-08
        • 1970-01-01
        相关资源
        最近更新 更多