df48ec67964f126550b0d5d71bec2b4cfc027d50.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, Queue, Deque, _crd;
  4. _export({
  5. default: void 0,
  6. Deque: void 0
  7. });
  8. return {
  9. setters: [function (_cc) {
  10. _cclegacy = _cc.cclegacy;
  11. }],
  12. execute: function () {
  13. _crd = true;
  14. _cclegacy._RF.push({}, "16900mmPzlPkZiuO57PxvRs", "Queue", undefined);
  15. _export("default", Queue = class Queue {
  16. constructor() {
  17. this.count = void 0;
  18. this.lowestCount = void 0;
  19. this.items = void 0;
  20. this.count = 0;
  21. this.lowestCount = 0;
  22. this.items = {};
  23. }
  24. /**加入队列*/
  25. enqueue(item) {
  26. // 队列的末尾添加元素: 将队列的大小作为key
  27. this.items[this.count] = item;
  28. this.count++;
  29. }
  30. /**拿出队首*/
  31. dequeue() {
  32. if (this.isEmpty()) {
  33. return undefined;
  34. }
  35. var result = this.items[this.lowestCount]; // 删除队首元素
  36. delete this.items[this.lowestCount]; // 队首元素自增
  37. this.lowestCount++;
  38. return result;
  39. }
  40. /**是否为空队列*/
  41. isEmpty() {
  42. return this.count - this.lowestCount === 0;
  43. }
  44. /**查看下一个出队元素 */
  45. peek() {
  46. if (this.isEmpty()) {
  47. return undefined;
  48. }
  49. return this.items[this.lowestCount];
  50. }
  51. /**队列个数*/
  52. size() {
  53. return this.count - this.lowestCount;
  54. }
  55. /**清空队列*/
  56. clear() {
  57. this.count = 0;
  58. this.lowestCount = 0;
  59. this.items = {};
  60. }
  61. toString() {
  62. if (this.isEmpty()) {
  63. return "";
  64. }
  65. var objString = "" + this.items[this.lowestCount];
  66. for (var i = this.lowestCount + 1; i < this.count; i++) {
  67. objString = objString + "," + this.items[i];
  68. }
  69. return objString;
  70. }
  71. });
  72. /**
  73. * 双端队列
  74. */
  75. _export("Deque", Deque = class Deque {
  76. constructor() {
  77. this.items = void 0;
  78. this.lowestCount = void 0;
  79. this.count = void 0;
  80. this.items = {};
  81. this.lowestCount = 0;
  82. this.count = 0;
  83. }
  84. /**
  85. * 向队列的尾端添加元素
  86. * @param element
  87. * @returns size
  88. */
  89. addTail(element) {
  90. this.items[this.count++] = element;
  91. return this.size();
  92. }
  93. /**
  94. * 向队列头部添加元素
  95. * @param element
  96. * @returns size
  97. */
  98. addHead(element) {
  99. if (this.count === 0) {
  100. this.addTail(element);
  101. } else if (this.lowestCount > 0) {
  102. this.items[--this.lowestCount] = element;
  103. } else {
  104. for (var i = this.count; i > this.lowestCount; i--) {
  105. this.items[i] = this.items[i - 1];
  106. }
  107. this.count++;
  108. this.items[0] = element;
  109. }
  110. return this.size();
  111. }
  112. /**
  113. * 返回队列尾部的元素
  114. * @returns T
  115. */
  116. getTail() {
  117. if (this.isEmpty()) return undefined;
  118. this.count--;
  119. var res = this.items[this.count];
  120. delete this.items[this.count];
  121. return res;
  122. }
  123. /**
  124. * 返回头部元素
  125. * @returns T
  126. */
  127. getHead() {
  128. if (this.isEmpty()) return undefined;
  129. var res = this.items[this.lowestCount];
  130. delete this.items[this.lowestCount];
  131. this.lowestCount++;
  132. return res;
  133. }
  134. /**
  135. * 看一下队列首部的元素
  136. * @returns T
  137. */
  138. peekHead() {
  139. if (this.isEmpty()) return undefined;
  140. return this.items[this.lowestCount];
  141. }
  142. /**
  143. * 看一下队列尾部的元素
  144. * @return T
  145. */
  146. peekTail() {
  147. if (this.isEmpty()) return undefined;
  148. return this.items[this.count - 1];
  149. }
  150. /**
  151. * 返回元素的个数
  152. * @returns number
  153. */
  154. size() {
  155. return this.count - this.lowestCount;
  156. }
  157. /**
  158. * 判断队列是否为空
  159. */
  160. isEmpty() {
  161. return this.size() === 0;
  162. }
  163. /**
  164. * 清空队列
  165. */
  166. clear() {
  167. this.items = {};
  168. this.count = this.lowestCount = 0;
  169. }
  170. toString() {
  171. if (this.isEmpty()) {
  172. return '';
  173. }
  174. var res = this.items[this.lowestCount].toString();
  175. for (var i = this.lowestCount + 1; i < this.count; i++) {
  176. res = res + ", " + this.items[i].toString();
  177. }
  178. return res;
  179. }
  180. });
  181. _cclegacy._RF.pop();
  182. _crd = false;
  183. }
  184. };
  185. });
  186. //# sourceMappingURL=df48ec67964f126550b0d5d71bec2b4cfc027d50.js.map