2012-02-19 61 views
1

我想做一個簡單的隊列類。現在我被卡住了。它幾乎完成。我發現只有一個「流行」功能導致了這個問題。任何人都可以告訴我該怎麼做。使隊列類錯誤

下面是代碼:
(queue.h)

#ifndef _QUEUE_ 
#define _QUEUE 

#include <iostream> 
#include <string> 
using namespace std; 


struct Stuff 
{ 
    string name; 
    int roll; 
}; 


class Queue 
{ 
private: 

struct Node 
{ 
    Stuff data ; 
    struct Node * next; 
}; 
Node *front; 
Node *back; 
int qsize; 
const int MAX; 
public: 

Queue(int size = 5); 
~Queue(); 

bool isfull() const; 
bool isempty() const; 
int queuesize() const; 

bool push(const Stuff & item); 
bool pop(); 
Stuff first(); 
Stuff last(); 


}; 




#endif 

queue.cpp飛出後

#include <iostream> 
using namespace std; 
#include "queue.h" 

Queue::Queue(int size) 
:MAX(size) 
{ 
front = back = 0; 
qsize = 0; 

} 
bool Queue::isempty() const 
{ 
if(qsize == 0) 
    return true; 
else return false; 
} 
bool Queue::isfull() const 
{ 
if(qsize == MAX) 
    return true; 
else 
    return false; 
} 
Queue::~Queue() 
{ 
Node * temp; 
while(front != NULL) 
{ 
    temp = front; 
    front = front->next; 
    delete temp; 
} 

} 
int Queue::queuesize() const 
{ 
return qsize; 
} 

bool Queue::push(const Stuff & swag) 
{ 
if(isfull()) 
    return false; 

Node *add = new Node; 

if(add == NULL) 
    return false; 
add->data = swag; 
add->next = NULL; 

if(front == NULL) 
    front = add; 
else 
    back->next = add->next; 
back = add; 
qsize++; 

return true; 
} 


bool Queue::pop() 
{ 
if(isempty()) 
    return false; 
if(front == NULL) 
    return false; 
Node *temp =front; 


// I think this part is doing something wrong. 
front = front->next; 



delete temp; 
qsize--; 
if(qsize == 0) 
    back = NULL; 
return true; 

} 
Stuff Queue::first() 
{ 
return front->data; 
} 
Stuff Queue::last() 
{ 
return back->data; 
} 

的main.cpp

#include <iostream> 
#include "queue.h" 
#include <string> 
using namespace std; 

int main() 
{ 
Queue a(5); 
Stuff data; 
data.name = "icp"; 
data.roll= 755; 
a.push(data); 
Stuff x = a.last(); 

cout << x.name << "\t" << x.roll << endl; 
data.name = "sms"; 
data.roll= 12544; 
a.push(data); 
x = a.last(); 
cout << x.name << "\t" << x.roll << endl; 

data.name = "jmc"; 
data.roll= 98740; 
a.push(data); 
x = a.last(); 
cout << x.name << "\t" << x.roll << endl; 

cout << a.queuesize() << endl; 

///////////// 
x = a.first(); 
cout << x.name << "\t" << x.roll << endl; 
a.pop(); 

x = a.first(); 
cout << x.name << "\t" << x.roll << endl; 
a.pop(); 

x = a.first(); 
cout << x.name << "\t" << x.roll << endl; 
a.pop(); 


//// 
cin.get(); 
cin.get(); 
return 0; 
} 

程序崩潰第一個元素。 我指出了我認爲會造成問題的部分。在此先感謝:)

+0

當你在調試器中運行你的代碼時,你學到了什麼? – 2012-02-19 12:43:59

回答

3

我認爲問題是由推功能引起的。 以下代碼

if(front == NULL) 
    front = add; 
else 
    back->next = add->next; 

背>下一個被設定爲下一個添加 - >,這是NULL。 add->next = NULL;

的添加是下一個要回來,所以它應該是back->next = add;

你覺得在流行功能的問題,是因爲前爲NULL,所以front->next是錯誤的。

希望這可以幫助你。

+0

謝謝!有效。是的,這是推動功能有問題。道具! – InspiredCoder 2012-02-19 14:03:51

0

當使用具有指向動態分配對象的指針的類時,應該提供複製構造函數。默認賦值運算符一點一點地複製。當您返回一個對象時,它將被複制並刪除。所以包含有用數據的指針被破壞,但我們仍然有一個指向它的指針。

我在調試器中運行了這段代碼,錯誤信息是字符串(它有指向動態分配空間的指針)。