1 | import { Collection } from "../Collection";
|
2 | import LinkList from "../linklist/LinkList";
|
3 | export class Queue extends Collection {
|
4 | constructor() {
|
5 | super();
|
6 | this.linkList = new LinkList();
|
7 | }
|
8 | isEmpty() {
|
9 | return !this.linkList.getTailNode();
|
10 | }
|
11 | peek() {
|
12 | if (!this.linkList.getHeadNode()) {
|
13 | return null;
|
14 | }
|
15 | return this.linkList.getHeadNode().Value;
|
16 | }
|
17 | enqueue(value) {
|
18 | this.linkList.append(value);
|
19 | }
|
20 | dequeue() {
|
21 | const head = this.linkList.shift();
|
22 | return head ? head.Value : null;
|
23 | }
|
24 | toString() {
|
25 | return this.linkList.toString();
|
26 | }
|
27 | __iterate(fn) {
|
28 | let temp = this.linkList.getHeadNode(), index = 0;
|
29 | while (temp) {
|
30 | fn(temp, index);
|
31 | index++;
|
32 | temp = temp.Next;
|
33 | }
|
34 | }
|
35 | }
|