Queue.ts 3.6 KB

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