【问题标题】:Moving data from one queue to another queue将数据从一个队列移动到另一个队列
【发布时间】:2017-04-24 22:13:30
【问题描述】:

如果员工的工资低于 50,000,我将队列出队。我不确定如何将它排入另一个队列,因为我的入队函数需要三个参数。我的任务说要创建一个类,然后在 main 中创建两个队列。我将队列作为类的对象,这是正确的吗?我如何排入第二个队列,类中只有一个入队函数,该函数需要三个参数。感谢所有的帮助。

#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::fixed;
using std::setprecision;

struct node{
    string name;
    int id;
    int salary;
    struct node *next;
};

node *rear;
node *front;

class DynEmpQueue{
private:
    int counter = 0;
public:
    void enqueue(string, int, int);
    void dequeue();
    void traverse()const;
    DynEmpQueue()
    {
        rear = nullptr;
        front = nullptr;
        counter = 0;
    }
};

void DynEmpQueue::enqueue(string localName, int localID, int localSalary)
{
    node *temp;
    temp = new (struct node);
    temp -> name = localName;
    temp -> id = localID;
    temp -> salary = localSalary;
    temp -> next = nullptr;
    if (front == nullptr)
        front = temp;
    else
        rear -> next = temp;
    rear = temp;
    counter++;
}

void DynEmpQueue::dequeue()
{
    string localName;
    int localID;
    int localSalary;
    node *temp;
    if (front == nullptr)
        cout << "The queue is empty.";
    else
    {
        temp = front;
        localName = temp -> name;
        localID = temp -> id;
        localSalary = temp -> salary;
        front = front -> next;
        delete temp;
        counter--;
    }
}

void DynEmpQueue::traverse()const
{
    node *temp;
    temp = front;
    if (front == nullptr)
        cout << "Queue is empty.";
    else
    {
        cout << "Queue contains " << counter << " elements." << endl;
        cout << "Queue elements:" << endl;
        while (temp != nullptr)
        {
            cout << temp -> name << "\t" << temp -> id << "\t" << temp -> salary << endl;
            temp = temp -> next;
        }
    }
}

int main()
{
    const int NumberEmployees = 5;
    DynEmpQueue originalQueue;

    originalQueue.enqueue("Justin Gray", 100, 104000);
    originalQueue.enqueue("Mike Smith", 200, 207000);
    originalQueue.enqueue("Jose Cans", 400, 47000);
    originalQueue.enqueue("Auston Matts", 300, 31000);
    originalQueue.enqueue("Liz Learnerd", 600, 89100);

    node object;
    DynEmpQueue demandSalaryIncrease;

    for (int i = 0; i < NumberEmployees; i++)
    {
        originalQueue.dequeue();
        if (object.salary <= 50000)
            demandSalaryIncrease.enqueue();
    }

    demandSalaryIncrease.traverse();

    return 0;
}

【问题讨论】:

  • 让我印象深刻的是你有全局变量frontrear。为什么是全局变量?我倾向于认为frontrear 节点属于队列类的一个实例,而不是一个翻译单元。
  • 我将它们作为全局变量,因为它们一直是在课堂上设置的。我应该搜索每个出队的节点,以便查看他们的薪水是高于还是低于 50,000?
  • 您的出队操作不必要地将结果拉取存储到本地数据中,然后将其丢弃。如果你要从队列中取出一些东西,也许先把它存储在某个地方。看来您需要 front() 操作以及 empty() 状态检查。
  • @hockey34 和 frontrear 作为全局变量考虑当你构造 demandSalaryIncreasefront = nullptr; 执行时 originalQueue 的列表会发生什么。

标签: c++ queue


【解决方案1】:

您无法知道队列中有哪些员工。看看你是如何定义你的方法的:

void enqueue(string, int, int);
void dequeue();
void traverse() const;

如您所见,没有任何方法返回 node 或员工的数据。因此,正如您当前声明的课程,没有办法从您的队列中获取员工。而且,由于您甚至无法让员工在队列中,因此您无法将他们添加到另一个队列中。

可能的解决方案:

修改您的traverse() 方法,使其接受薪水作为参数并返回一个数组(甚至是一个队列),其中包含薪水低于该薪水的所有员工。

更好、更灵活的解决方案是使用谓词,但是(因为您使用的是全局变量)似乎您并没有在寻找完美的解决方案。

【讨论】:

  • 我的教授希望 enqueue 采用这三个参数。我应该把前后指针放在哪里?抱歉,这是我们教授教给我们的方式……
  • 在哪里?在DynEmpQueue 类内部,使其成为一个属性。每个队列都应该有自己的frontrear
  • 所以我使用了一个谓词,我会设置它来获取薪水,然后如果薪水低于 50,000 则返回 true?
  • 结束chat的讨论。
  • @hockey34 我几乎可以肯定你误解了你的导师关于frontrear 指针的说明。没有他们在课堂上违反了指导 OO 原则之一:encapsulation。更不用说让多个队列使用相同的列表指针是一个非常糟糕的主意。如果你的老师是认真的,那他们就是疯了。
最近更新 更多