c++ 求大神帮我改下队列让它能实现入队和出队操作并能输出它们
#include<iostream>usingnamespacestd;classCqueue{private:int*elem;intrear;intfront;int...
#include<iostream>
using namespace std;
class Cqueue
{
private:
int *elem;
int rear;
int front;
int maxsize;
public:
Cqueue()
{
maxsize=5;
rear=front=0;
elem=0;
}
int enQueue(int x)
{
Cqueue q;
if((q.rear+1)%q.maxsize==q.front)
return 0;
q.elem[q.rear]=x;
q.rear=(q.rear+1)%q.maxsize;
}
int deQueue(int e)
{
Cqueue q;
if(q.front=q.rear)
return 0;
e=q.elem[q.front];
q.front=(q.front+1)%q.maxsize;
return 1;
}
void dispQueue()
{
Cqueue q;
for (int i=q.front;i<q.rear;i++)
{
cout<<q.elem[i];
}
cout<<endl;
}
};
int main()
{
Cqueue q;
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.dispQueue();
int e;
q.deQueue(e);
q.dispQueue();
return 0;
} 展开
using namespace std;
class Cqueue
{
private:
int *elem;
int rear;
int front;
int maxsize;
public:
Cqueue()
{
maxsize=5;
rear=front=0;
elem=0;
}
int enQueue(int x)
{
Cqueue q;
if((q.rear+1)%q.maxsize==q.front)
return 0;
q.elem[q.rear]=x;
q.rear=(q.rear+1)%q.maxsize;
}
int deQueue(int e)
{
Cqueue q;
if(q.front=q.rear)
return 0;
e=q.elem[q.front];
q.front=(q.front+1)%q.maxsize;
return 1;
}
void dispQueue()
{
Cqueue q;
for (int i=q.front;i<q.rear;i++)
{
cout<<q.elem[i];
}
cout<<endl;
}
};
int main()
{
Cqueue q;
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.dispQueue();
int e;
q.deQueue(e);
q.dispQueue();
return 0;
} 展开
1个回答
展开全部
前两天刚帮别人改成java代码
#include<iostream>
using namespace std;
class Cqueue
{
private:
int *elem;
int rear;
int front;
int maxsize;
public:
Cqueue(int maxSize=5)
{
maxsize=maxSize;
rear=front=0;
elem=new int(maxSize * sizeof(int));
}
void enQueue(int x)
{
if((rear+1)%maxsize==front)
return;
elem[rear]=x;
rear=(rear+1)%maxsize;
}
int deQueue(int e)
{
if(front==rear)
return 0;
e=elem[front];
front=(front+1)%maxsize;
return e;
}
void dispQueue()
{
cout << "{";
for (int i=front;i<rear;i++) {
cout<<elem[i];
if (i!=(rear-1)) cout << ",";
}
cout<<"}" << endl;
}
~Cqueue() {
delete elem;
}
};
int main() {
Cqueue q(6);
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.dispQueue();
int e=0;
q.deQueue(e);
q.dispQueue();
return 0;
}
更多追问追答
追问
elem=new int(maxSize * sizeof(int));和elem=new int有什么区别呢?
追答
写错了,new int[maxSize * sizeof(int)];
还有 delete []elem;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询