大家好能帮我看道数据结构题吗?(连上)
CirQueue.h:template<classT>classCirQueue{private:T*base;intfront;intrear;intqueuesize...
CirQueue.h:
template<class T>
class CirQueue
{private:
T* base;
int front;
int rear;
int queuesize;
public:
CirQueue(int m);
~CirQueue();
void EnQueue(T x);
T DeQueue(T x);
T GetHead();
T GetLast();
int QueueEmpty();
int QueueFull();
void ClearQueue();
void Pointer();
void QueueTranverse();
};
template<class T>
CirQueue<T>::CirQueue(int m)
{base=new T[m];
if(base==NULL)
{cout<<"队列创建失败,退出!"<<endl;
exit(1);
}
front=rear=0;
queuesize=0;
}
template<class T>
void CirQueue<T>::EnQueue(T x)
{
if((rear+1)%queuesize==front) throw"上溢,无法入队";
base[rear]=x;
rear=(rear+1)%queuesize;
}
template<class T>
T CirQueue<T>::GetHead()
{
T x;
if(front==rear)throw"队空,队尾无元素";
x=base[front];
return x;
}
template<class T>
T CirQueue<T>::GetLast()
{
T x;
if(front==rear) throw"队空,队尾无元素";
x=base[rear-1];
return x;
}
template<class T>
int CirQueue<T>::QueueFull()
{
if((rear+1)%queuesize==front)
return 1;
else
return 0;
}
template<class T>
void CirQueue<T>::ClearQueue()
{
front=rear=0;
}
template<class T>
void CirQueue<T>::Pointer()
{cout<<"队头front="<<front<<endl;
cout<<"队尾rear="<<rear<<endl;
}
template <class T>
void CirQueue<T>::QueueTranverse()
{
int i=front;
while(i!=rear)
{
cout<<base[i]<<'\t';
i=(i+1)%queuesize;
}
cout<<endl;
}
主程序如下:
#include <iostream>
#include "LinkList.h"
#include "CirQueue.h"
using namespace std;
int main(void)
{LinkList<int> L;
int count,busnumber,lorrynumber;
CirQueue<int> bus(50);//客车队列
CirQueue<int> lorry(50);//货车队列
char a;
int locate=0;
int shipnumber=0;
while(a=='N')
{
cout<<"请输入此批客车数量:";
cin>>busnumber;
cout<<"请输入此批货车数量:";
cin>>lorrynumber;
for(int i=0;i<busnumber;i++)
{
int e=0; //0表示客车
bus.EnQueue(e); //客车入队
}
for(int j=0;j<lorrynumber;j++)
{
int f=1;//1表示货车
lorry.EnQueue(f);//货车入队
}
Node ship[50]; //船只
for(int i=0;i<50;i++)//为50只船编号
{ship.data=i;
}
int h=0;
int loacte=1;
while(bus.QueueEmpty()&&lorry.QueueEmpty()==0)//当两个队列均不为空时,客车和货车上船
{
if(bus.QueueEmpty==0)//客车队列不空
{
for(int k=0;k<4;k++) //客车上船
{ship[shipnumber].x[h++]=bus.DeQueue();
count++;
if(count==10)//船满,换新船
{shipnumber++;
ship[shipnumber].x=new int[10];
count=0;
L.Insert(locate++,ship[shipnumber]);//将满船插入链表
}
if(bus.QueueEmpty==1) break;
}
}
if(lorry.QueueEmpty==0) //货车队列不空
{ship[shipnumber].x[h++]=lorry.DeQueue();
count++;
if(count==10)//船满,换新船
{shipnumber++;
ship[shipnumber].x=new int[10];
count=0;
L.Insert(locate++,ship[shipnumber]);//将满船插入链表
}
}
}
cout<<"此批车是否全上船(Y/N):";
cin>>a;
}
cout<<"渡轮上的情况为:";
L.ListDisplay();
return 0;
} 展开
template<class T>
class CirQueue
{private:
T* base;
int front;
int rear;
int queuesize;
public:
CirQueue(int m);
~CirQueue();
void EnQueue(T x);
T DeQueue(T x);
T GetHead();
T GetLast();
int QueueEmpty();
int QueueFull();
void ClearQueue();
void Pointer();
void QueueTranverse();
};
template<class T>
CirQueue<T>::CirQueue(int m)
{base=new T[m];
if(base==NULL)
{cout<<"队列创建失败,退出!"<<endl;
exit(1);
}
front=rear=0;
queuesize=0;
}
template<class T>
void CirQueue<T>::EnQueue(T x)
{
if((rear+1)%queuesize==front) throw"上溢,无法入队";
base[rear]=x;
rear=(rear+1)%queuesize;
}
template<class T>
T CirQueue<T>::GetHead()
{
T x;
if(front==rear)throw"队空,队尾无元素";
x=base[front];
return x;
}
template<class T>
T CirQueue<T>::GetLast()
{
T x;
if(front==rear) throw"队空,队尾无元素";
x=base[rear-1];
return x;
}
template<class T>
int CirQueue<T>::QueueFull()
{
if((rear+1)%queuesize==front)
return 1;
else
return 0;
}
template<class T>
void CirQueue<T>::ClearQueue()
{
front=rear=0;
}
template<class T>
void CirQueue<T>::Pointer()
{cout<<"队头front="<<front<<endl;
cout<<"队尾rear="<<rear<<endl;
}
template <class T>
void CirQueue<T>::QueueTranverse()
{
int i=front;
while(i!=rear)
{
cout<<base[i]<<'\t';
i=(i+1)%queuesize;
}
cout<<endl;
}
主程序如下:
#include <iostream>
#include "LinkList.h"
#include "CirQueue.h"
using namespace std;
int main(void)
{LinkList<int> L;
int count,busnumber,lorrynumber;
CirQueue<int> bus(50);//客车队列
CirQueue<int> lorry(50);//货车队列
char a;
int locate=0;
int shipnumber=0;
while(a=='N')
{
cout<<"请输入此批客车数量:";
cin>>busnumber;
cout<<"请输入此批货车数量:";
cin>>lorrynumber;
for(int i=0;i<busnumber;i++)
{
int e=0; //0表示客车
bus.EnQueue(e); //客车入队
}
for(int j=0;j<lorrynumber;j++)
{
int f=1;//1表示货车
lorry.EnQueue(f);//货车入队
}
Node ship[50]; //船只
for(int i=0;i<50;i++)//为50只船编号
{ship.data=i;
}
int h=0;
int loacte=1;
while(bus.QueueEmpty()&&lorry.QueueEmpty()==0)//当两个队列均不为空时,客车和货车上船
{
if(bus.QueueEmpty==0)//客车队列不空
{
for(int k=0;k<4;k++) //客车上船
{ship[shipnumber].x[h++]=bus.DeQueue();
count++;
if(count==10)//船满,换新船
{shipnumber++;
ship[shipnumber].x=new int[10];
count=0;
L.Insert(locate++,ship[shipnumber]);//将满船插入链表
}
if(bus.QueueEmpty==1) break;
}
}
if(lorry.QueueEmpty==0) //货车队列不空
{ship[shipnumber].x[h++]=lorry.DeQueue();
count++;
if(count==10)//船满,换新船
{shipnumber++;
ship[shipnumber].x=new int[10];
count=0;
L.Insert(locate++,ship[shipnumber]);//将满船插入链表
}
}
}
cout<<"此批车是否全上船(Y/N):";
cin>>a;
}
cout<<"渡轮上的情况为:";
L.ListDisplay();
return 0;
} 展开
2个回答
展开全部
CirQueue.h:
template<class T>
class CirQueue
{private:
T* base;
int front;
int rear;
int queuesize;
public:
CirQueue(int m);
//~CirQueue();
void EnQueue(T x);
T DeQueue();
T GetHead();
T GetLast();
int QueueEmpty();
int QueueFull();
void ClearQueue();
void Pointer();
void QueueTranverse();
};
template<class T>
CirQueue<T>::CirQueue(int m)
{base=new T[m];
if(base==NULL)
{cout<<"队列创建失败,退出!"<<endl;
exit(1);
}
front=rear=0;
queuesize=m;
}
template<class T>
void CirQueue<T>::EnQueue(T x)
{
if((rear+1)%queuesize==front) throw"上溢,无法入队";
base[rear]=x;
rear=(rear+1)%queuesize;
}
template<class T>
T CirQueue<T>::DeQueue()
{
T x;
if(front==rear) throw"队空,队尾无元素";
x=base[front++];
return x;
}
template<class T>
T CirQueue<T>::GetHead()
{
T x;
if(front==rear)throw"队空,队尾无元素";
x=base[front];
return x;
}
template<class T>
T CirQueue<T>::GetLast()
{
T x;
if(front==rear) throw"队空,队尾无元素";
x=base[rear-1];
return x;
}
template<class T>
int CirQueue<T>::QueueFull()
{
if((rear+1)%queuesize==front)
return 1;
else
return 0;
}
template<class T>
int CirQueue<T>::QueueEmpty()
{
if(front==rear)
return 1;
return 0;
}
template<class T>
void CirQueue<T>::ClearQueue()
{
front=rear=0;
}
template<class T>
void CirQueue<T>::Pointer()
{cout<<"队头front="<<front<<endl;
cout<<"队尾rear="<<rear<<endl;
}
template <class T>
void CirQueue<T>::QueueTranverse()
{
int i=front;
while(i!=rear)
{
cout<<base[i]<<'\t';
i=(i+1)%queuesize;
}
cout<<endl;
}
main.cpp
#include <iostream>
#include "LinkList.h"
#include "CirQueue.h"
using namespace std;
int main(void)
{
LinkList<int> L;
int count=0,busnumber,lorrynumber;
CirQueue<int> bus(50);//客车队列
CirQueue<int> lorry(50);//货车队列
Node<int> ship[50]; //船只
char a='N';
int locate=1;
int shipnumber=0;
while(a=='N')
{
cout<<"请输入此批客车数量:";
cin>>busnumber;
cout<<"请输入此批货车数量:";
cin>>lorrynumber;
for(int i=0;i<busnumber;i++)
{
int e=0; //0表示客车
bus.EnQueue(e); //客车入队
}
for(int j=0;j<lorrynumber;j++)
{
int f=1;//1表示货车
lorry.EnQueue(f);//货车入队
}
int h=0;
while(bus.QueueEmpty()==0||lorry.QueueEmpty()==0)//当两个队列有不为空时,客车和货车上船
{
if(bus.QueueEmpty()==0)//客车队列不空
{
int bf=1,lf=0,end=0;
for(int k=0;k<4;k++) //客车上船
{
if (shipnumber==50)
{
break;
}
if (bf)
{
bus.DeQueue();
ship[shipnumber].b++;
count++;
}
else
{
if(lf)
{
lorry.DeQueue();
ship[shipnumber].l++;
count++;
}
else
end=1;
}
if(count==10)//船满,换新船
{
shipnumber++;
count=0;
L.Insert(locate++,ship[shipnumber-1]);//将满船插入链表
if (bus.QueueEmpty()==0)
{
bf=1;
}
else
{
if(lorry.QueueEmpty()==0)
{
bf=0,lf=1;
}
else
bf=0,lf=0;
}
}
else
{
if (bus.QueueEmpty()==0)
{
bf=1;
}
else
{
if(lorry.QueueEmpty()==0)
{
bf=0,lf=1;
}
else
bf=0,lf=0;
}
}
if(end==1)
break;
}
}
if (lorry.QueueEmpty()==0)
{
lorry.DeQueue();
ship[shipnumber].l++;
count++;
if (count==10)
{
shipnumber++;
count=0;
L.Insert(locate++,ship[shipnumber-1]);
}
}
else
{
if(bus.QueueEmpty()==0)
{
bus.DeQueue();
ship[shipnumber].b++;
count++;
if (count==10)
{
shipnumber++;
count=0;
L.Insert(locate++,ship[shipnumber-1]);
}
}
}
if (shipnumber==50)
{
break;
}
}
cout<<"此批车是否全上船(Y/N):";
cin>>a;
}
if(count>0&&shipnumber<50)
L.Insert(locate,ship[shipnumber]);
cout<<"渡轮上的情况为:"<<endl;
L.ListDisplay();
return 0;
}
主要就是模拟和数据结构。
template<class T>
class CirQueue
{private:
T* base;
int front;
int rear;
int queuesize;
public:
CirQueue(int m);
//~CirQueue();
void EnQueue(T x);
T DeQueue();
T GetHead();
T GetLast();
int QueueEmpty();
int QueueFull();
void ClearQueue();
void Pointer();
void QueueTranverse();
};
template<class T>
CirQueue<T>::CirQueue(int m)
{base=new T[m];
if(base==NULL)
{cout<<"队列创建失败,退出!"<<endl;
exit(1);
}
front=rear=0;
queuesize=m;
}
template<class T>
void CirQueue<T>::EnQueue(T x)
{
if((rear+1)%queuesize==front) throw"上溢,无法入队";
base[rear]=x;
rear=(rear+1)%queuesize;
}
template<class T>
T CirQueue<T>::DeQueue()
{
T x;
if(front==rear) throw"队空,队尾无元素";
x=base[front++];
return x;
}
template<class T>
T CirQueue<T>::GetHead()
{
T x;
if(front==rear)throw"队空,队尾无元素";
x=base[front];
return x;
}
template<class T>
T CirQueue<T>::GetLast()
{
T x;
if(front==rear) throw"队空,队尾无元素";
x=base[rear-1];
return x;
}
template<class T>
int CirQueue<T>::QueueFull()
{
if((rear+1)%queuesize==front)
return 1;
else
return 0;
}
template<class T>
int CirQueue<T>::QueueEmpty()
{
if(front==rear)
return 1;
return 0;
}
template<class T>
void CirQueue<T>::ClearQueue()
{
front=rear=0;
}
template<class T>
void CirQueue<T>::Pointer()
{cout<<"队头front="<<front<<endl;
cout<<"队尾rear="<<rear<<endl;
}
template <class T>
void CirQueue<T>::QueueTranverse()
{
int i=front;
while(i!=rear)
{
cout<<base[i]<<'\t';
i=(i+1)%queuesize;
}
cout<<endl;
}
main.cpp
#include <iostream>
#include "LinkList.h"
#include "CirQueue.h"
using namespace std;
int main(void)
{
LinkList<int> L;
int count=0,busnumber,lorrynumber;
CirQueue<int> bus(50);//客车队列
CirQueue<int> lorry(50);//货车队列
Node<int> ship[50]; //船只
char a='N';
int locate=1;
int shipnumber=0;
while(a=='N')
{
cout<<"请输入此批客车数量:";
cin>>busnumber;
cout<<"请输入此批货车数量:";
cin>>lorrynumber;
for(int i=0;i<busnumber;i++)
{
int e=0; //0表示客车
bus.EnQueue(e); //客车入队
}
for(int j=0;j<lorrynumber;j++)
{
int f=1;//1表示货车
lorry.EnQueue(f);//货车入队
}
int h=0;
while(bus.QueueEmpty()==0||lorry.QueueEmpty()==0)//当两个队列有不为空时,客车和货车上船
{
if(bus.QueueEmpty()==0)//客车队列不空
{
int bf=1,lf=0,end=0;
for(int k=0;k<4;k++) //客车上船
{
if (shipnumber==50)
{
break;
}
if (bf)
{
bus.DeQueue();
ship[shipnumber].b++;
count++;
}
else
{
if(lf)
{
lorry.DeQueue();
ship[shipnumber].l++;
count++;
}
else
end=1;
}
if(count==10)//船满,换新船
{
shipnumber++;
count=0;
L.Insert(locate++,ship[shipnumber-1]);//将满船插入链表
if (bus.QueueEmpty()==0)
{
bf=1;
}
else
{
if(lorry.QueueEmpty()==0)
{
bf=0,lf=1;
}
else
bf=0,lf=0;
}
}
else
{
if (bus.QueueEmpty()==0)
{
bf=1;
}
else
{
if(lorry.QueueEmpty()==0)
{
bf=0,lf=1;
}
else
bf=0,lf=0;
}
}
if(end==1)
break;
}
}
if (lorry.QueueEmpty()==0)
{
lorry.DeQueue();
ship[shipnumber].l++;
count++;
if (count==10)
{
shipnumber++;
count=0;
L.Insert(locate++,ship[shipnumber-1]);
}
}
else
{
if(bus.QueueEmpty()==0)
{
bus.DeQueue();
ship[shipnumber].b++;
count++;
if (count==10)
{
shipnumber++;
count=0;
L.Insert(locate++,ship[shipnumber-1]);
}
}
}
if (shipnumber==50)
{
break;
}
}
cout<<"此批车是否全上船(Y/N):";
cin>>a;
}
if(count>0&&shipnumber<50)
L.Insert(locate,ship[shipnumber]);
cout<<"渡轮上的情况为:"<<endl;
L.ListDisplay();
return 0;
}
主要就是模拟和数据结构。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
蓝鲸智能科技
2024-11-21 广告
2024-11-21 广告
您好,我公司自主研发的“机动车驾驶人理论考试管理系统”,具有进/出考场考生身份确认应用场景,能够实现认证比对、约考信息查询和考试状态查询的功能;具有理论考试应用场景,具有考生签到和随机分配考台等功能,我司理论考试系统全面适配信创软硬件,并持...
点击进入详情页
本回答由蓝鲸智能科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询