UNPKG

1.4 kBPlain TextView Raw
1import { Collection } from "../Collection";
2import LinkList from "../linklist/LinkList";
3import { LinkNode } from "../linklist/LinkNode";
4
5export class Queue<T> extends Collection<LinkNode<T>>{
6 private linkList: LinkList<T>;
7 constructor(){
8 super();
9 this.linkList = new LinkList();
10 }
11
12 /**
13 * 是否为空队列
14 * @returns boolean
15 */
16 public isEmpty(){
17 return !this.linkList.getTailNode();
18 }
19
20 /**
21 * 查看头部的值
22 */
23 public peek(){
24 if (!this.linkList.getHeadNode()){
25 return null;
26 }
27 return this.linkList.getHeadNode().Value;
28 }
29
30 /**
31 * 向队列中添加一个节点
32 * @param value
33 * @returns LinkNode
34 */
35 public enqueue(value: T){
36 this.linkList.append(value);
37 }
38
39 /**
40 * 推出一个队列节点
41 * @returns null | LinkNode
42 */
43 public dequeue(){
44 const head = this.linkList.shift();
45 return head ? head.Value : null;
46 }
47
48 /**
49 * @returns string
50 */
51 public toString(){
52 return this.linkList.toString();
53 }
54
55 protected __iterate(fn: (item: LinkNode<T>, index: number) => void): void {
56 let temp = this.linkList.getHeadNode(),
57 index = 0;
58 while (temp){
59 fn(temp, index);
60 index++;
61 temp = temp.Next;
62 }
63 }
64}