本来以为很容易的,结果还是写了我两个小时。
用指针模拟queue类,再加上类,各种错误,总算是解决掉了--
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
class Item
{
private:
int time;
int cost;
public:
Item():time(0),cost(0){}
Item(int k):time(k)
{
cost=rand()%3;
}
Item (const Item &st)
{
time=st.time;
cost=st.cost;
}
Item &operator=(const Item &st)
{
time=st.time;
cost=st.cost;
return *this;
}
int dealt()
{
return time;
}
int dealc()
{
//cout<<"------"<<cost<<endl;
return cost; }
friend ostream &operator<<(ostream &os,Item &st)
{
os<<st.time<<endl<<st.cost;
return os;
}
};
struct ss
{
Item num;
struct ss *next;
};
class Queue
{
private:
struct ss *beg,*end;
int cnt;
int tolt;
int size;
int xx;
public:
Queue():beg(NULL),end(NULL),cnt(0),tolt(0),size(10),xx(0){};
Queue(const Queue &st)
{
cnt=st.cnt;
tolt=st.tolt;
size=st.size;
while(beg!=NULL)
{
ss *p=beg->next;
delete beg;
beg=p;
}
end=NULL;
ss *p=st.beg;
beg=new ss;
beg->next=NULL;
beg->num=p->num;
end=beg;
while(p->next!=NULL)
{
p=p->next;
ss *p1=new ss;
p1->num=p->num;
p1->next=NULL;
end->next=p1;
end=p1;
}
//return *this;
}
Queue &operator=(const Queue &st)
{
cnt=st.cnt;
tolt=st.tolt;
size=st.size;
while(beg!=NULL)
{
ss *p=beg->next;
delete beg;
beg=p;
}
end=NULL;
ss *p=st.beg;
beg=new ss;
beg->next=NULL;
beg->num=p->num;
end=beg;
while(p->next!=NULL)
{
p=p->next;
ss *p1=new ss;
p1->num=p->num;
p1->next=NULL;
end->next=p1;
end=p1;
}
return *this;
}
bool empty()
{
if(cnt==0) return true;
else return false;
}
bool full()
{
if(cnt==size) return true;
return false;
}
bool push(Item &st)
{
if(full()) return false;
cnt++;
if(beg==NULL)
{
ss *p=new ss;
p->num=st;
p->next=NULL;
beg=end=p;
}
else
{
ss *p=new ss;
p->num=st;
p->next=NULL;
beg->next=p;
end=p;
}
//cout<<beg->num<<endl;
return true;
}
bool pop()
{
if(empty()) return false;
cnt--;
xx++;
if(tolt<beg->num.dealt())
{
tolt=beg->num.dealt()+beg->num.dealc();
}
else
{
tolt+=beg->num.dealc();
}
ss *p=beg->next;
delete beg;
beg=p;
return true;
}
int top(int w)
{
int tmp=beg->num.dealt()+beg->num.dealc();
//cout<<"---"<<beg->num.dealt()<<" "<<beg->num.dealc()<<endl;
if(tmp<=w) pop();
}
void deal(int n)
{
tolt-=n;
}
~Queue()
{
while(beg!=NULL)
{
ss *p=beg->next;
delete beg;
beg=p;
}
}
friend ostream &operator<<(ostream &os,const Queue &st)
{
os<<"处理花费的总时间(分钟):"<<st.tolt<<endl<<"处理了多少人:"<<st.xx<<endl;
os<<"队列里面还有多少人:"<<st.cnt<<endl;
return os;
}
};
int main()
{
Queue q;
//int sum=1235;
int n,m;
cout<<"请输入n,m:";
cin>>n>>m;
if(n>m)
{
int tmp=n;
n=m;
m=n;
}
for(int i=n;i<=m;i++)
{
Item p(i);
q.push(p);
if(!q.empty())
{
q.top(i);
}
}
q.deal(n);
cout<<q;
return 0;
}

  

new、delete、以及queue类的更多相关文章

  1. Java中Queue类实现

    原先在java编程中,Queue的实现都是用LinkedList Queue queue = new LinkedList(); 但正如jdk中所说的那样: 注意,此实现不是同步的.如果多个线程同时访 ...

  2. C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类)

    1.ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在ArrayList中主要使用Add.Remove.RemoveAt.Insert四个方法对栈进行操作.Add方法 ...

  3. 转:C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、Sort)

    C#常用的集合类型(ArrayList类.Stack类.Queue类.Hashtable类.Sort) .ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在Array ...

  4. Java实现Queue类

    Java实现Queue类 import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Sc ...

  5. C++中的queue类、QT中的QQueue类

    C++中的queue 实现一种先进先出的数据结构,是一个模板类 头文件 #include<queue> 用法(以int型为例): queue<int> Q; //定义一个int ...

  6. 生产者、消费者模型---Queue类

    Queue队列在几乎每种编程语言都会有,python的列表隐藏的一个特点就是一个后进先出(LIFO)队列.而本文所讨论的Queue是python标准库queue中的一个类.它的原理与列表相似,但是先进 ...

  7. Queue类

    1.LinkedBlockingQueue:基于链接节点的可选限定的blocking queue . 这个队列排列元素FIFO(先进先出). 队列的头部是队列中最长的元素. 队列的尾部是队列中最短时间 ...

  8. queue 类

    一:普通队列 1.队列特征:先进先出,它只允许在一端(队尾)进行插入元素操作,在另一端(队头)进行删除元素操作 2. 存取类函数 front():用来取出queue中的队头元素,对应于front()函 ...

  9. 数据结构——java Queue类

    定义 队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作. LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用 图例 Que ...

随机推荐

  1. 业务、架构、技术,我们应该关注什么 Java和.Net的优势劣势简单看法 市场经济决定,商业之道即是软件之道,市场的需求决定着软件技术的发展 利益决定着选择应用新技术

    业务.架构.技术,我们应该关注什么 一个企业存在的必然和前提就是获取企业生成的利润,怎么样合法合理取得利润呢,企业怎么样生存下去呢,很简单,为客户提供等值的产品与服务,客户支付你相应的报酬. 我们是从 ...

  2. ceph mon更换ip地址

    一.概述: 数据中心的集群从A地迁移到B地, 更改的信息包括: 集群内所有节点的ip 集群内所有节点的主机名 由ceph文档可知:http://docs.ceph.com/docs/hammer/ra ...

  3. CentOS 7 systemd的坑

    一.概述 在从 CentOS 6 迁移到 CentOS 7 的过程中,可能有一些地方需要调整,最显著的地方莫过于 systemd 带来的改变,不同的管理服务的方式,不同的日志方式,设置时区,时间等等. ...

  4. Android事件总线还能怎么玩?

    作者简介:何红辉,Android工程师,现任职于友盟. 顾名思义,AndroidEventBus是一个Android平台的事件总线框架,它简化了Activity.Fragment.Service等组件 ...

  5. 对于“Newtonsoft.Json”已拥有为“NETStander.Library”定义的依赖项,解决办法

    问题描述: 在使用visual studio中的NuGet包管理下载程序时,有时会出现-对于“Newtonsoft.Json”已拥有为“NETStander.Library”定义的依赖项,这样的错误. ...

  6. 【Spring】Spring框架如何集成Hibernate框架

    下面个整理一下hibernate和Spring框架的结合. 首先是引入hibernate框架的包.Spring框架的包.数据库驱动包. User.java文件 package cn.shop.bean ...

  7. 关于tensorboard启动问题

    我在学习过程中遇到了tensorboard无法启动的问题. 按照网上的教程,我无法正常启动tensorboard,全过程没有报错,但是打开tensorboard显示 No dashboards are ...

  8. 开源项目mark

    1. Apache的开源软件列表 http://www.oschina.net/project/apache 2. Java开源Apache项目 http://www.open-open.com/56 ...

  9. OpenCV 学习笔记03 drawContours函数

    opencv-python   4.0.1 轮廓的绘制或填充. cv2.drawContours(image, contours, contourIdx, color[, thickness[, li ...

  10. php非阻塞服务器

    <?php class PHPSocketServer { const ERROR = 0; const LOG = 1; const DEBUG = 2; private $aConfig = ...