2012-02-19 7 views
1

간단한 대기열 클래스를 만들려고합니다. 이제 나는 붙어있다. 거의 완성되었습니다. 나는 단 하나의 함수 "pop"만이 문제를 일으킨다는 것을 알아 냈습니다. 누구든지 내가 무엇을해야하는지 말해 줄 수 있습니까? 여기 대기열 생성 중 오류가 발생했습니다.

코드입니다 :
(queue.h)

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

프로그램 충돌 밖으로 터지는 후

#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 첫 번째 요소. 나는 약간 문제가 있다고 생각하는 부분을 지적했다. 미리 감사드립니다 :)

+0

디버거에서 코드를 실행했을 때 무엇을 배웠습니까? –

답변

3

나는 푸시 기능에 의한 문제라고 생각합니다. NULL입니다

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

가 백> 다음은 추가 -> 다음으로 설정되어있는 다음 코드. 그래서 front->next 잘못, add->next = NULL;

추가 기능은 백업 옆에 하나, 그래서

back->next = add; 당신은 전면 NULL이기 때문에 팝업 기능에 문제가 생각해야한다.

희망이 도움이 될 수 있습니다.

+0

감사합니다! 그것은 효과가 있었다. 예, 문제가있는 기능이었습니다. 소품! – InspiredCoder

0

동적으로 할당 된 객체에 대한 포인터가있는 클래스를 사용할 때는 복사 생성자를 제공해야합니다. 기본 할당 연산자는 비트별로 복사합니다. 객체를 반환하면 객체가 복사 된 다음 삭제됩니다. 그래서 유용한 데이터를 포함하고 있던 포인터는 데이터가 파괴되었지만 여전히 포인터를 가지고 있습니다.

디버거에서이 코드를 실행했는데 오류가 문자열 (동적으로 할당 된 공간에 대한 포인터가 있음)에있었습니다.

관련 문제