1 | import { Collection } from "../Collection";
|
2 | import LinkList from "../linklist/LinkList";
|
3 | import { LinkNode } from "../linklist/LinkNode";
|
4 |
|
5 | export 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 |
|
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 |
|
33 |
|
34 |
|
35 | public enqueue(value: T){
|
36 | this.linkList.append(value);
|
37 | }
|
38 |
|
39 | |
40 |
|
41 |
|
42 |
|
43 | public dequeue(){
|
44 | const head = this.linkList.shift();
|
45 | return head ? head.Value : null;
|
46 | }
|
47 |
|
48 | |
49 |
|
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 | }
|