C++队列问题
只设尾结点的循环队列完成基本功能出队和清空队有问题出队后再历遍的话会一下出去俩元素清空队则直接出错求解答和修改structNode{intdata;Node*next;}...
只设尾结点的循环队列完成基本功能 出队和清空队有问题 出队后再历遍的话 会一下出去俩元素 清空队则直接出错 求解答和修改
struct Node
{int data;
Node*next;
};
class LinkQueue
{
private:
Node*rear;
public:
LinkQueue()
{
rear=new Node;
rear->next=rear;
}
void ENQueue(int x)
{Node*s;
s=new Node;
s->data=x;
s->next=rear->next;
rear->next=s;
rear=s;
}
int DeQueue()
{int k;
Node*p;
if(rear->next==rear)
cout<<"队空,无法出队";
else
p=rear->next->next;
k=p->data;
rear->next=rear->next->next->next;
delete p;
return k;
}
void ClearQueue()
{
Node*p;
p=rear->next->next;
while(rear->next!=rear)
{rear->next->next=p->next;
delete p;
p=rear->next->next;
}
}
void QueueTranverse()
{
Node*p;
p=rear->next->next;
while(p!=rear->next)
{
cout<<p->data<<'\t';
p=p->next;
}
cout<<endl;
}
};
出队已经解决,现在就剩清队了,求解答 展开
struct Node
{int data;
Node*next;
};
class LinkQueue
{
private:
Node*rear;
public:
LinkQueue()
{
rear=new Node;
rear->next=rear;
}
void ENQueue(int x)
{Node*s;
s=new Node;
s->data=x;
s->next=rear->next;
rear->next=s;
rear=s;
}
int DeQueue()
{int k;
Node*p;
if(rear->next==rear)
cout<<"队空,无法出队";
else
p=rear->next->next;
k=p->data;
rear->next=rear->next->next->next;
delete p;
return k;
}
void ClearQueue()
{
Node*p;
p=rear->next->next;
while(rear->next!=rear)
{rear->next->next=p->next;
delete p;
p=rear->next->next;
}
}
void QueueTranverse()
{
Node*p;
p=rear->next->next;
while(p!=rear->next)
{
cout<<p->data<<'\t';
p=p->next;
}
cout<<endl;
}
};
出队已经解决,现在就剩清队了,求解答 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询