1+ // RUN: deno run Data-Structures/Arrays/My_Array_Implementation.ts
2+
3+ type NumIndexedObject = { [ index : number ] : any } ;
4+
5+ class MyArray < T > {
6+
7+ public length : number ;
8+ private data : NumIndexedObject ;
9+
10+ constructor ( ) {
11+ this . length = 0 ;
12+ this . data = Object . create ( { } ) ;
13+ }
14+
15+ public get ( index : number ) : T | null {
16+ if ( index > 0 && index < this . length ) {
17+ return this . data [ index ] ;
18+ }
19+
20+ return null ;
21+ }
22+
23+ public push ( item : T ) : number {
24+ this . data [ this . length ] = item ; // Add item to end of array
25+ ++ this . length ; // Add 1 to array length
26+
27+ return this . length ;
28+ }
29+
30+ public pop ( ) : T | null {
31+ if ( this . length > 0 ) {
32+ const lastItem = this . data [ this . length - 1 ] ; // Retrieve last item
33+ delete this . data [ this . length - 1 ] ; // Delete last item
34+ -- this . length ; // Decrement array length by 1
35+
36+ return lastItem ;
37+ }
38+
39+ return null ;
40+ }
41+
42+ public deleteIndex ( index : number ) : T | null {
43+ if ( index > 0 && index < this . length ) {
44+
45+ const requestedItem = this . data [ index ] ;
46+ this . shiftItemsLeftAfterIndex ( index ) ;
47+
48+ return requestedItem ;
49+ }
50+
51+ return null ;
52+ }
53+
54+ public insertItemAtIndex ( index : number , item : T ) : number | null {
55+ if ( index > 0 && index < this . length ) {
56+ this . shiftItemsRightAtIndex ( index ) ;
57+ this . data [ index ] = item ;
58+ return this . length ;
59+ }
60+
61+ return null ;
62+ }
63+
64+ private shiftItemsLeftAfterIndex ( index : number ) : void {
65+ for ( let i = index ; i < this . length - 1 ; ++ i ) {
66+ this . data [ i ] = this . data [ i + 1 ] ;
67+ }
68+
69+ -- this . length ;
70+ delete this . data [ this . length ] ;
71+ }
72+
73+ private shiftItemsRightAtIndex ( index : number ) : void {
74+ ++ this . length ;
75+
76+ for ( let i = this . length - 1 ; i > index ; -- i ) {
77+ this . data [ i ] = this . data [ i - 1 ] ;
78+ }
79+
80+ delete this . data [ index ] ;
81+ }
82+ }
83+
84+
85+ let helloArray = new MyArray < string > ( ) ;
86+
87+ helloArray . push ( 'Hello' ) ; // O(1)
88+ helloArray . push ( 'world' ) ;
89+ console . log ( helloArray ) ;
90+
91+ helloArray . pop ( ) ; // O(1)
92+ console . log ( helloArray ) ;
93+
94+ helloArray . push ( 'Deno' ) ;
95+ helloArray . push ( '!' ) ;
96+ console . log ( helloArray ) ;
97+
98+ console . log ( 'At index 2:' , helloArray . get ( 2 ) ) ;
99+
100+ // -------------------------------------------
101+
102+ let sokka = new MyArray < string > ( ) ;
103+
104+ sokka . push ( 's' ) ;
105+ sokka . push ( 'o' ) ;
106+ sokka . push ( 'c' ) ;
107+ sokka . push ( 'k' ) ;
108+ sokka . push ( 'a' ) ;
109+ console . log ( sokka ) ;
110+
111+ console . log ( 'Element deleted:' , sokka . deleteIndex ( 2 ) ) ; // O(n)
112+ console . log ( sokka ) ;
113+
114+ sokka . insertItemAtIndex ( 2 , 'k' ) ; // O(n)
115+ console . log ( sokka ) ;
116+
117+
118+
119+ // --------------------------- Terminal Output: ---------------------------
120+ // MyArray { length: 2, data: { 0: "Hello", 1: "world" } }
121+ // MyArray { length: 1, data: { 0: "Hello" } }
122+ // MyArray { length: 3, data: { 0: "Hello", 1: "Deno", 2: "!" } }
123+ // At index 2: !
124+ // MyArray { length: 5, data: { 0: "s", 1: "o", 2: "c", 3: "k", 4: "a" } }
125+ // Element deleted: c
126+ // MyArray { length: 4, data: { 0: "s", 1: "o", 2: "k", 3: "a" } }
127+ // MyArray { length: 5, data: { 0: "s", 1: "o", 2: "k", 3: "k", 4: "a" } }
0 commit comments