1+ import Node from '../Linked-Lists/NodeLL.ts' ;
2+
3+
4+ export default class Queue < T > {
5+ private last : Node < T > | null ;
6+ private first : Node < T > | null ;
7+ private length : number ;
8+
9+ constructor ( ) {
10+ this . last = null ;
11+ this . first = null ;
12+ this . length = 0 ;
13+ }
14+
15+ public getLength ( ) : number {
16+ return this . length ;
17+ }
18+
19+ public peek ( ) : T | null {
20+ return this . first ?. getValue ( ) || null ;
21+ }
22+
23+ public enqueue ( value : T ) : boolean {
24+ const newNode = new Node ( value ) ;
25+
26+ if ( ! this . last ) { // If empty queue, initialize
27+ this . first = newNode ;
28+ this . last = newNode ;
29+ }
30+ else {
31+ this . last . setNext ( newNode ) ;
32+ this . last = this . last . getNext ( ) ;
33+ }
34+
35+ ++ this . length ;
36+ return true ;
37+ }
38+
39+ public dequeue ( ) : T | null {
40+ if ( ! this . first ) return null ; // Edge case: Empty queue
41+
42+ if ( this . length === 1 ) { // Edge case: Queue has 1 element, so a dequeue should reset the queue's state
43+ this . last = null ; // Challenge: What is the state of each 'class field' after a dequeue when 1 element was remaining?
44+ }
45+
46+ const value = this . first . getValue ( ) ;
47+ this . first = this . first . getNext ( ) ;
48+
49+ -- this . length ;
50+ return value ;
51+ }
52+ }
53+
54+ function printQueue ( queue : Queue < any > ) {
55+ console . log ( JSON . stringify ( queue ) ) ;
56+ }
57+
58+ function printPeekQueue ( queue : Queue < any > ) {
59+ console . log ( 'Peeking... Found' , JSON . stringify ( queue . peek ( ) ) ) ;
60+ }
61+
62+ function printDequeue ( queue : Queue < any > ) {
63+ console . log ( 'Dequeued:' , JSON . stringify ( queue . dequeue ( ) ) ) ;
64+ }
65+
66+
67+ //---------------------------------------------------------------------
68+ // ---------- MAIN PROGRAM ----------
69+ //---------------------------------------------------------------------
70+ if ( import . meta. main ) {
71+
72+ const ATLA = new Queue < string > ( ) ;
73+
74+ printPeekQueue ( ATLA ) ;
75+ ATLA . enqueue ( 'Sokka' ) ;
76+ ATLA . enqueue ( 'Katara' ) ;
77+ printPeekQueue ( ATLA ) ;
78+ ATLA . enqueue ( 'Aang' ) ;
79+ ATLA . enqueue ( 'Appa' ) ;
80+
81+ printQueue ( ATLA ) ;
82+
83+ printDequeue ( ATLA ) ;
84+ printDequeue ( ATLA ) ;
85+ printDequeue ( ATLA ) ;
86+
87+ printQueue ( ATLA ) ;
88+
89+ printDequeue ( ATLA ) ;
90+
91+ printQueue ( ATLA ) ;
92+
93+ ATLA . enqueue ( 'Zuko' ) ;
94+ ATLA . enqueue ( 'Iroh' ) ;
95+
96+ printQueue ( ATLA ) ;
97+
98+ // RUN: deno run Data-Structures/Sequential/Queue.ts
99+ }
100+
101+
102+ // --------------------------- Terminal Output: ---------------------------
103+ // Peeking... Found null
104+ // Peeking... Found "Sokka"
105+ // {"last":{"value":"Appa","next":null},"first":{"value":"Sokka","next":{"value":"Katara","next":{"value":"Aang","next":{"value":"Appa","next":null}}}},"length":4 }
106+ // Dequeued: "Sokka"
107+ // Dequeued: "Katara"
108+ // Dequeued: "Aang"
109+ // {"last":{"value":"Appa","next":null},"first":{"value":"Appa","next":null},"length":1 }
110+ // Dequeued: "Appa"
111+ // {"last":null,"first":null,"length":0}
112+ // {"last":{"value":"Iroh","next":null},"first":{"value":"Zuko","next":{"value":"Iroh","next":null}},"length":2 }
0 commit comments