代码是链式队列的构建,编译过程没问题,但是运行时提示Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)。请问是什么原因造成的??这个问题已经困扰我很多次了
#include <iostream>
using namespace std;
class qnode{
public:
int data;
qnode *link;
};
class linkquene{
public:
void QInit(linkquene &Q);
void QInsert(linkquene &Q, int a);
void QDelete(linkquene &Q);
int isempty(linkquene &Q);
void Qshow(linkquene &Q);
private:
qnode *rear;
qnode *front;
};
void linkquene::QInit(linkquene &Q)
{
qnode *p = new qnode;
p->link = NULL;
Q.front = p;
Q.rear = p;
}
int linkquene::isempty(linkquene &Q)
{
if(Q.rear == Q.front){
return 1;
}
else return 0;
}
void linkquene::QInsert(linkquene &Q, int a)
{
qnode *n = new qnode;
n->link = NULL;
n->data = a;
Q.rear->link = n;
Q.rear = n;
}
void linkquene::QDelete(linkquene &Q)
{
qnode *n;
n = Q.front->link;
Q.front->link = Q.front->link->link;
if(Q.rear == n)
Q.rear = Q.front;
delete n;
}
void linkquene::Qshow(linkquene &Q)
{
qnode *p;
p = Q.front->link;
while(p != NULL)
{
cout<<p->data<<endl;
p = p->link;
}
}
int main(int argc, const char * argv[]) {
linkquene q;
linkquene *Q;
Q = new linkquene;
q.QInsert(*Q, 10);
q.QDelete(*Q);
q.QInsert(*Q, 7);
q.QInsert(*Q, 1);
q.QInsert(*Q, 8);
q.QDelete(*Q);
q.Qshow(*Q);
return 0;
}