new、delete、以及queue类
本来以为很容易的,结果还是写了我两个小时。
用指针模拟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类的更多相关文章
- Java中Queue类实现
原先在java编程中,Queue的实现都是用LinkedList Queue queue = new LinkedList(); 但正如jdk中所说的那样: 注意,此实现不是同步的.如果多个线程同时访 ...
- C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类)
1.ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在ArrayList中主要使用Add.Remove.RemoveAt.Insert四个方法对栈进行操作.Add方法 ...
- 转:C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、Sort)
C#常用的集合类型(ArrayList类.Stack类.Queue类.Hashtable类.Sort) .ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在Array ...
- Java实现Queue类
Java实现Queue类 import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Sc ...
- C++中的queue类、QT中的QQueue类
C++中的queue 实现一种先进先出的数据结构,是一个模板类 头文件 #include<queue> 用法(以int型为例): queue<int> Q; //定义一个int ...
- 生产者、消费者模型---Queue类
Queue队列在几乎每种编程语言都会有,python的列表隐藏的一个特点就是一个后进先出(LIFO)队列.而本文所讨论的Queue是python标准库queue中的一个类.它的原理与列表相似,但是先进 ...
- Queue类
1.LinkedBlockingQueue:基于链接节点的可选限定的blocking queue . 这个队列排列元素FIFO(先进先出). 队列的头部是队列中最长的元素. 队列的尾部是队列中最短时间 ...
- queue 类
一:普通队列 1.队列特征:先进先出,它只允许在一端(队尾)进行插入元素操作,在另一端(队头)进行删除元素操作 2. 存取类函数 front():用来取出queue中的队头元素,对应于front()函 ...
- 数据结构——java Queue类
定义 队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作. LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用 图例 Que ...
随机推荐
- C/C++/动态链接库DLL中函数的调用约定与名称修饰
参见:http://blog.twofei.com/cc/impl/calling-convension.html 调用约定(Calling Convention)是指在程序设计语言中为了实现函数调用 ...
- [AaronYang原创] 敏捷开发-Jira 6.0.5环境搭建[2]
基本配置-关卡一(我研究了1.5个小时 AaronYang) JIRA的设置向导将只显示您安装后第一次JIRA. 一旦你完成了它,你不能再次运行它. 然而,每一个设置在设置向导配置可以通过管理控制 ...
- Oracle 12C -- purge dba_recyclebin
SQL> create user abce identified by abce; User created. SQL> grant resource,connect to abce; G ...
- app-framework学习--官网地址及demo下载地址
一起学习共同进步,加油..! 官网地址:http://app-framework-software.intel.com/ 下载地址:http://download.csdn.net/detail/ha ...
- selenium webdriver如何拿到页面的加载时间
这个问题与语言无关,对于现代浏览器来说,使用 window.performance.timing这个对象就好了. 用execute_script方法(java用executeScript)方法执行 w ...
- mongodb c++ driver 2.0编译使用
安装boost1.48.0 在boost的官网下载boost1.48.0,链接例如以下: http://sourceforge.net/projects/boost/files/boost/1.48. ...
- Mapped Statements collection does not contain value for com.xxxx.dao.impl.AreaDAOImpl.findByCode
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Persiste ...
- Java – Display all ZoneId and its UTC offset
package com.mkyong.date; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.Z ...
- JavaScript面向对象:类、方法、属性
JavaScript是一种基于对象的语言,与传统面向对象语言(C#.C++)相比,JavaScript中没有类的概念,其继承有两种基本形式:基于对象的继承和基于类型的继承(原型链继承).无论哪种形式的 ...
- unity, ios skin crash
https://issuetracker.unity3d.com/issues/ios-loading-models-with-tangents-set-to-calculate-legacy-fro ...