2017-06-04 69 views
-4

我已經創建了一個使用數組的隊列類,它最多需要兩個整數。但是,我如何將兩個整數同時傳遞給數組?使用bool Queue :: enqueue(int,int)C++傳遞兩個整數到隊列

另外我需要一個dequeue(),它打印兩個整數並將它們從隊列中拋出。任何建議如何做到這一點?

class Queue { 
private: 
    int * table; 
    int front; 
    int length; 
    const int size=2; 
public: 
    Queue(int n); 
    ~Queue(); 
    bool isEmpty(); 
    bool enqueue(int, int); 
    bool dequeue(); 
    void print(); 
}; 

Queue::Queue(int n){ 
n=size; 
length=0; 
front=0; 
table=new int[n]; 
} 

Queue::~Queue(){ 
delete [] table; 
} 

bool Queue::isEmpty(){ 
if(length==size) 
    return false; 
else 
    return true; 
} 

bool Queue::enqueue(int , int){ 
if (length == size) 
    return -1; // Error, Queue is full 
now i need to pass two integers if it it empty 
+0

你將如何與做一個int? – 2017-06-04 17:29:35

+1

使您的隊列通用並使用'std :: pair '? –

回答

0

爲什麼你想玩一個隊列的定義?似乎你的設計存在缺陷。但是我一直在使用,這將解決你的目的的載體仍然寫的工作代碼(雖然我懷疑你希望通過添加/在排隊移除2項/隊列操作來實現的)

#include <iostream> 
#include <vector> 

using namespace std; 

class Queue { 

    vector<int> table; 
    const int CAPACITY = 2; 

public: 
    bool isEmpty(); 
    bool enqueue(int, int); 
    void dequeue(); 
    void print(); 
}; 

bool Queue::isEmpty(){ 
    if(table.size()) 
     return false; 
    else 
     return true; 
} 

bool Queue::enqueue(int a, int b){ 
    if (table.size()) 
     return false; // Error, Queue is full 
    table.push_back(a); 
    table.push_back(b); 
    return true; 
} 

void Queue::dequeue(){ 
    if(!table.size()) 
     return; //queue is empty 
    table.pop_back(); 
    table.pop_back(); 
} 

void Queue::print(){ 
    if(!table.size()){ 
     cout << "Queue is empty" << endl; 
     return; 
    } 
    cout << table[0] << "," << table[1] << endl; 
} 

int main() 
{ 
    //Make a variable of our container 
    Queue queue; 
    queue.enqueue(1,2); 
    queue.print(); 
    queue.dequeue(); 
    queue.print(); 
    return 0; 
} 
+0

假設這可能是一個家庭作業問題,我不認爲這個人的教師會喜歡矢量作爲創建隊列的內部數據結構。如果標準庫被允許使用,爲什麼不使用'std :: queue'或'std :: dequeue'? –

+0

那麼,在那種情況下,我認爲我幫助了不道德的活動。家庭作業應該由學生在沒有外部幫助的情況下自己完成;-) –

+0

對於這樣的情況,最好不要自己回答問題,而是鼓勵學生自己找出解決方案。不要只是發佈代碼,也可以提供一個人可以做什麼來解決問題的想法。 –