1. 本来以为很容易的,结果还是写了我两个小时。
  2. 用指针模拟queue类,再加上类,各种错误,总算是解决掉了--
  3. #include<iostream>
  4. #include<cstdlib>
  5. #include<string>
  6. using namespace std;
  7. class Item
  8. {
  9. private:
  10. int time;
  11. int cost;
  12. public:
  13. Item():time(0),cost(0){}
  14. Item(int k):time(k)
  15. {
  16. cost=rand()%3;
  17. }
  18. Item (const Item &st)
  19. {
  20. time=st.time;
  21. cost=st.cost;
  22. }
  23. Item &operator=(const Item &st)
  24. {
  25. time=st.time;
  26. cost=st.cost;
  27. return *this;
  28. }
  29. int dealt()
  30. {
  31. return time;
  32. }
  33. int dealc()
  34. {
  35. //cout<<"------"<<cost<<endl;
  36. return cost;
  37.  
  38. }
  39. friend ostream &operator<<(ostream &os,Item &st)
  40. {
  41. os<<st.time<<endl<<st.cost;
  42. return os;
  43. }
  44. };
  45. struct ss
  46. {
  47. Item num;
  48. struct ss *next;
  49. };
  50. class Queue
  51. {
  52. private:
  53. struct ss *beg,*end;
  54. int cnt;
  55. int tolt;
  56. int size;
  57. int xx;
  58. public:
  59. Queue():beg(NULL),end(NULL),cnt(0),tolt(0),size(10),xx(0){};
  60. Queue(const Queue &st)
  61. {
  62. cnt=st.cnt;
  63. tolt=st.tolt;
  64. size=st.size;
  65. while(beg!=NULL)
  66. {
  67. ss *p=beg->next;
  68. delete beg;
  69. beg=p;
  70. }
  71. end=NULL;
  72. ss *p=st.beg;
  73. beg=new ss;
  74. beg->next=NULL;
  75. beg->num=p->num;
  76. end=beg;
  77. while(p->next!=NULL)
  78. {
  79. p=p->next;
  80. ss *p1=new ss;
  81. p1->num=p->num;
  82. p1->next=NULL;
  83. end->next=p1;
  84. end=p1;
  85. }
  86. //return *this;
  87. }
  88. Queue &operator=(const Queue &st)
  89. {
  90. cnt=st.cnt;
  91. tolt=st.tolt;
  92. size=st.size;
  93. while(beg!=NULL)
  94. {
  95. ss *p=beg->next;
  96. delete beg;
  97. beg=p;
  98. }
  99. end=NULL;
  100. ss *p=st.beg;
  101. beg=new ss;
  102. beg->next=NULL;
  103. beg->num=p->num;
  104. end=beg;
  105. while(p->next!=NULL)
  106. {
  107. p=p->next;
  108. ss *p1=new ss;
  109. p1->num=p->num;
  110. p1->next=NULL;
  111. end->next=p1;
  112. end=p1;
  113. }
  114. return *this;
  115. }
  116. bool empty()
  117. {
  118. if(cnt==0) return true;
  119. else return false;
  120. }
  121. bool full()
  122. {
  123. if(cnt==size) return true;
  124. return false;
  125. }
  126. bool push(Item &st)
  127. {
  128. if(full()) return false;
  129. cnt++;
  130. if(beg==NULL)
  131. {
  132. ss *p=new ss;
  133. p->num=st;
  134. p->next=NULL;
  135. beg=end=p;
  136. }
  137. else
  138. {
  139. ss *p=new ss;
  140. p->num=st;
  141. p->next=NULL;
  142. beg->next=p;
  143. end=p;
  144. }
  145. //cout<<beg->num<<endl;
  146. return true;
  147. }
  148. bool pop()
  149. {
  150. if(empty()) return false;
  151. cnt--;
  152. xx++;
  153. if(tolt<beg->num.dealt())
  154. {
  155. tolt=beg->num.dealt()+beg->num.dealc();
  156. }
  157. else
  158. {
  159. tolt+=beg->num.dealc();
  160. }
  161. ss *p=beg->next;
  162. delete beg;
  163. beg=p;
  164. return true;
  165. }
  166. int top(int w)
  167. {
  168. int tmp=beg->num.dealt()+beg->num.dealc();
  169. //cout<<"---"<<beg->num.dealt()<<" "<<beg->num.dealc()<<endl;
  170. if(tmp<=w) pop();
  171. }
  172. void deal(int n)
  173. {
  174. tolt-=n;
  175. }
  176. ~Queue()
  177. {
  178. while(beg!=NULL)
  179. {
  180. ss *p=beg->next;
  181. delete beg;
  182. beg=p;
  183. }
  184. }
  185. friend ostream &operator<<(ostream &os,const Queue &st)
  186. {
  187. os<<"处理花费的总时间(分钟):"<<st.tolt<<endl<<"处理了多少人:"<<st.xx<<endl;
  188. os<<"队列里面还有多少人:"<<st.cnt<<endl;
  189. return os;
  190. }
  191. };
  192. int main()
  193. {
  194. Queue q;
  195. //int sum=1235;
  196. int n,m;
  197. cout<<"请输入n,m:";
  198. cin>>n>>m;
  199. if(n>m)
  200. {
  201. int tmp=n;
  202. n=m;
  203. m=n;
  204. }
  205. for(int i=n;i<=m;i++)
  206. {
  207. Item p(i);
  208. q.push(p);
  209. if(!q.empty())
  210. {
  211. q.top(i);
  212. }
  213. }
  214. q.deal(n);
  215. cout<<q;
  216. return 0;
  217. }

  

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. activiti实战--第二章--搭建Activiti开发环境及简单示例

    (一)搭建开发环境 学习资料:<Activiti实战> 第一章 认识Activiti 2.1 下载Activiti 官网:http://activiti.org/download.html ...

  2. 指尖下的js —— 多触式web前端开发之三:处理复杂手势(转)

    这篇文章着重介绍多触式设备上特有的gesture event(android和iOS对这个事件的封装大同小异).这个事件是对touch event的更高层的封装,和touch一样,它同样包括gestu ...

  3. 【Linux】字符转换命令paste

    这个 paste 就要比join 简单多了!相对于 join 必须要比对两个文件的数据相关性, paste 就直接『将两行贴在一起,且中间以 [tab] 键隔开』而已!简单的使用方法: [root@w ...

  4. c# 中内部类的简单介绍 C#内部类

    最近在看java一方面的书籍,看到一个很奇怪的问题,java类中还可以再定义一个类,这种结构非常特殊!后来才发现我知识浅薄了,原来C#中也有内部类,之前都一直没有注意过这个语法结构! 使用内部类有这样 ...

  5. sublime Text 些许使用配置

    在安装numpy等库函数时,通过“命令提示符”操作显示库函数已经安装完毕,在pycharm中可是依然显示引用失败,尝试使用sublime,显示可用,遂好好使用sublime,现配置成想用的模式. 1 ...

  6. Oracle 12C -- plug unplugged PDB into CDB

    connetct to CDB as a common user and verify that pdb_test is closed SQL> select con_id,dbid,name, ...

  7. nginx根据http_user_agent防DDOS

    前端squid反向代理到nginx nginx根据http_user_agent防DDOS 首先查看访问日志,找出可疑访问 找到http_user_agent 的特征,然后再作过滤 "Moz ...

  8. jmeter maven自动移动jar包windows 批处理命令

    jmeter项目maven文件下面放这.bat 工具,可以把必要的jar包移动到jmeter响应的文件夹下面 rem 本文件放在jmeter 脚本maven项目根目录下面,和pom.xml在同一个文件 ...

  9. css遮罩层

    父元素:position:fixed; 让子元素居中对齐:position:absolute;top:0;bottom:0;left:0;right:0;margin:auto; <style& ...

  10. JDK1.5新特性,基础类库篇,线程类(Thread)增强了哪些

    java.lang.Thread类增强特性如下: 线程优先级已经更改.java.lang.Thread.MIN_PRIORITY = 1 java.lang.Thread.NORM_PRIORITY ...