File tree Expand file tree Collapse file tree 2 files changed +104
-0
lines changed
contents/stacks_and_queues/code/typescript Expand file tree Collapse file tree 2 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
1
+ interface IQueue < T > {
2
+ /**
3
+ * `dequeue` removes first element from the queue and returns the same
4
+ */
5
+ dequeue ( ) : T ;
6
+ /**
7
+ * `enqueue` adds element to last of the queue and returns the size
8
+ */
9
+ enqueue ( data : T ) : number ;
10
+ /**
11
+ * `size` return size or length of the queue
12
+ */
13
+ size ( ) : number ;
14
+ /**
15
+ * `front` returns first element of the queue
16
+ */
17
+ front ( ) : T ;
18
+ }
19
+
20
+ class Queue < T > implements IQueue < T > {
21
+ private readonly list : Array < T > = [ ] ;
22
+
23
+ public enqueue ( data : T ) {
24
+ return this . list . push ( data ) ;
25
+ }
26
+
27
+ public dequeue ( ) {
28
+ return this . list . shift ( ) ;
29
+ }
30
+
31
+ public size ( ) {
32
+ return this . list . length ;
33
+ }
34
+
35
+ public front ( ) {
36
+ return this . list [ 0 ] ;
37
+ }
38
+ }
39
+
40
+ function exampleQueue ( ) {
41
+ const numberQueue = new Queue < number > ( ) ;
42
+
43
+ numberQueue . enqueue ( 4 ) ;
44
+ numberQueue . enqueue ( 5 ) ;
45
+ numberQueue . enqueue ( 9 ) ;
46
+
47
+ console . log ( numberQueue . dequeue ( ) ) ;
48
+ console . log ( numberQueue . size ( ) ) ;
49
+ console . log ( numberQueue . front ( ) ) ;
50
+ }
51
+
52
+ exampleQueue ( ) ;
Original file line number Diff line number Diff line change
1
+ interface IStack < T > {
2
+ /**
3
+ * `pop` removes last element from the stack and returns the same
4
+ */
5
+ pop ( ) : T ;
6
+ /**
7
+ * `push` adds element to last of the stack and returns the size
8
+ */
9
+ push ( data : T ) : number ;
10
+ /**
11
+ * `size` return size or length of the stack
12
+ */
13
+ size ( ) : number ;
14
+ /**
15
+ * `top` returns last element of the stack
16
+ */
17
+ top ( ) : T ;
18
+ }
19
+
20
+ class Stack < T > implements IStack < T > {
21
+ private readonly list : Array < T > = [ ] ;
22
+
23
+ public push ( data : T ) {
24
+ return this . list . push ( data ) ;
25
+ }
26
+
27
+ public pop ( ) {
28
+ return this . list . pop ( ) ;
29
+ }
30
+
31
+ public size ( ) {
32
+ return this . list . length ;
33
+ }
34
+
35
+ public top ( ) {
36
+ return this . list [ this . list . length - 1 ] ;
37
+ }
38
+ }
39
+
40
+ function exampleStack ( ) {
41
+ const numberStack = new Stack < number > ( ) ;
42
+
43
+ numberStack . push ( 4 ) ;
44
+ numberStack . push ( 5 ) ;
45
+ numberStack . push ( 9 ) ;
46
+
47
+ console . log ( numberStack . pop ( ) ) ;
48
+ console . log ( numberStack . size ( ) ) ;
49
+ console . log ( numberStack . top ( ) ) ;
50
+ }
51
+
52
+ exampleStack ( ) ;
You can’t perform that action at this time.
0 commit comments