2017-04-24 161 views
0

我將隊列出隊,並且員工的薪水低於50,000。我不知道如何將它排入另一個隊列,因爲我的排隊函數有三個參數。我的任務說創建一個班,然後在主要兩個隊列。我把隊列作爲班級的對象,這是正確的嗎?如何排隊進入第二個隊列,只有一個入隊函數需要三個參數。感謝所有的幫助。將數據從一個隊列移動到另一個隊列

#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; 
} 
+1

讓我感到震驚的是你有全局變量'front'和'rear'。爲什麼全局變量?我傾向於認爲'front'和'rear'節點屬於隊列類的一個實例,而不是一個翻譯單元。 –

+0

我將它們作爲全局變量,因爲它們一直都是在課堂上設置的。我應該搜索每個出列的節點,以便查看他們的薪水是高於還是低於50,000? – hockey34

+0

您的出隊操作不必要地將結果拉入本地數據,然後丟棄它。如果你要從隊列中取出某些東西,可能首先將它存儲在某個地方。看起來你需要一個'front()'動作,以及一個'empty()'狀態檢查。 – WhozCraig

回答

0

您無法知道隊列中存在哪些員工。看你如何定義你的方法:

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

正如你所看到的,沒有方法將返回node或員工的數據。所以,就像你現在宣佈這個班級一樣,沒有辦法從你的隊列中獲得員工。而且,由於你甚至無法讓員工進入隊列,所以你不能將他們添加到另一個隊列中。

可能的解決方案:

修改您traverse()方法,使得它需要一個工資作爲參數,並返回包含所有員工的陣列(或甚至一個隊列)的量,薪水比薪水低。

一個更好,更靈活的解決方案是使用謂詞,但是(因爲你使用的是全局變量),看起來好像你不是在尋找完美的解決方案。

+0

我的教授希望排隊採取這三個參數。我應該在哪裏放置前後指針?對不起,這是我們教授教給我們的方式...... – hockey34

+0

哪裏?在'DynEmpQueue'類中,這是一個屬性。每個隊列應該有自己的'front'和'rear'。 –

+0

所以我使用謂詞,我會設置它來接受薪水,然後返回true,如果薪水低於50,000? – hockey34

相關問題