30/09/2018, 19:22
hỏi về code class bị sai
#include<iostream>
using namespace std;
class node;
class queue;
//
class data
{
int a;
public:
int geta(){
return a;
}
void nhapdata(data *x);
friend class node;
friend void inserttail(queue *q, data x);
friend class queue;
friend void pop(queue *q);
};
//
void data::nhapdata(data *x)
{
cout<<"nhap data (o day la kieu int)";
cin>>x->a;
}
class node
{
data infor;
node *link;
public:
friend void inserttail(queue *q, data x);
friend class queue;
friend void pop(queue *q);
};
//
class queue
{
node *head;
node *tail;
int spt;// lỗi ở đây
public:
friend void inserttail(queue *q,data x);// them cuoi
void init(queue *q);
int empty(queue q);
void fifo(queue *q);
void trave(queue q);
friend void pop(queue *q);// xoa dau
};
//
void queue::init(queue *q)
{
q->head=NULL;
q->tail=NULL;
q->spt=0;
}
//
int empty(queue q)
{
return(q.spt==0);//spt = 0 thi rong
}
//
void pop(queue *q)// xoa dau
{
if (empty(*q)) cout<<"queue rong khong xoa dc";
else{
data x= q->head->infor;
if(q->spt==1) init(q);
else
{
q->head=q->head->link;
q->spt--;
cout<<"da xoa pt dau tien la "<<x.a;
}
}
}
void inserttail(queue *q, data x)
{
node *p;
p=(node*)malloc(sizeof(node));
p->infor=x;
p->link=NULL;
if(empty(*q))
{
q->head=p;
q->tail=p;
}
else{
q->tail->link=p;
q->tail=p;
}
++q->spt;
}
void queue::travel(queue q)
{
node *p=q.head;
while(p!=NULL){
cout<<p->infor.geta()<< " ";
p=p->link;
}}
void queue::fifo(queue *q)
{
data x;
init(q);
do{
x.nhapdata(&x);
if(x.geta()!=0)
inserttail(q,x);
}
while(x.geta!=0);
}
nó bị lỗi cái số phần tử… em chẳng hiểu nó là ntn luôn…
Bài liên quan