File tree Expand file tree Collapse file tree 5 files changed +132
-0
lines changed Expand file tree Collapse file tree 5 files changed +132
-0
lines changed Original file line number Diff line number Diff line change @@ -2,3 +2,5 @@ console.log('Typescript Data Structures & Algorithms');
22import './arrays/arrayClass' ;
33import './hash-table/HashTable' ;
44import './linked-list/LinkedList' ;
5+ import './doubly-linked-list/DoublyLinkedList' ;
6+ import './stack/StackClass' ;
Original file line number Diff line number Diff line change 1+ import { StackNode } from './StackNode' ;
2+
3+ export interface Stack < T > {
4+ size : number ;
5+ top : StackNode < T > | null ;
6+ bottom : StackNode < T > | null ;
7+ push ( value : T ) : number ;
8+ pop ( ) : StackNode < T > | null ;
9+ peek ( ) : StackNode < T > | null ;
10+ }
11+
12+ export class Stack < T > implements Stack < T > {
13+ public constructor ( ) {
14+ this . size = 0 ;
15+ this . top = null ;
16+ this . bottom = null ;
17+ }
18+
19+ public peek ( ) : StackNode < T > | null {
20+ return this . top ;
21+ }
22+
23+ public push ( value : T ) {
24+ const node = new StackNode ( value ) ;
25+ if ( this . size === 0 ) {
26+ this . top = node ;
27+ this . bottom = node ;
28+ } else {
29+ const currentTop = this . top ;
30+ this . top = node ;
31+ this . top . next = currentTop ;
32+ }
33+
34+ this . size += 1 ;
35+ return this . size ;
36+ }
37+
38+ public pop ( ) : StackNode < T > | null {
39+ if ( this . size > 0 ) {
40+ const nodeToBeRemoved = this . top ;
41+ this . top = nodeToBeRemoved . next ;
42+ this . size -= 1 ;
43+ nodeToBeRemoved . next = null ;
44+ return nodeToBeRemoved ;
45+ }
46+ return null ;
47+ }
48+ }
Original file line number Diff line number Diff line change 1+ export interface StackNode < T > {
2+ data : T | null ;
3+ next : StackNode < T > | null ;
4+ }
5+
6+ export class StackNode < T > implements StackNode < T > {
7+ constructor ( data ?: T ) {
8+ this . data = data ;
9+ this . next = null ;
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ import { Stack } from '../StackClass' ;
2+
3+ describe ( 'Stack' , ( ) => {
4+ test ( 'create an empty stack' , ( ) => {
5+ const stack = new Stack ( ) ;
6+
7+ expect ( stack ) . not . toBeNull ( ) ;
8+ } ) ;
9+
10+ test ( 'push data to stack' , ( ) => {
11+ const stack = new Stack ( ) ;
12+
13+ stack . push ( { value : 'test1' , key : 'key1' } ) ;
14+ stack . push ( { value : 'test2' , key : 'key2' } ) ;
15+
16+ expect ( stack . top . data [ 'value' ] ) . toBe ( 'test2' ) ;
17+ expect ( stack . top . next . data [ 'value' ] ) . toBe ( 'test1' ) ;
18+ expect ( stack . bottom . data [ 'value' ] ) . toBe ( 'test1' ) ;
19+ expect ( stack . bottom . next ) . toBeNull ( ) ;
20+ } ) ;
21+
22+ test ( 'remove data from stack' , ( ) => {
23+ const stack = new Stack < number > ( ) ;
24+
25+ stack . push ( 1 ) ;
26+ stack . push ( 2 ) ;
27+ stack . push ( 3 ) ;
28+
29+ expect ( stack . pop ( ) . data ) . toBe ( 3 ) ;
30+ expect ( stack . pop ( ) . data ) . toBe ( 2 ) ;
31+ expect ( stack . pop ( ) . data ) . toBe ( 1 ) ;
32+ expect ( stack . pop ( ) ) . toBeNull ( ) ;
33+ } ) ;
34+
35+ test ( 'peek node from stack' , ( ) => {
36+ const stack = new Stack < number > ( ) ;
37+
38+ stack . push ( 1 ) ;
39+ stack . push ( 2 ) ;
40+ stack . push ( 3 ) ;
41+
42+ expect ( stack . peek ( ) . data ) . toBe ( 3 ) ;
43+ } ) ;
44+ } ) ;
Original file line number Diff line number Diff line change 1+ import { StackNode } from '../StackNode' ;
2+
3+ describe ( 'Stack Node' , ( ) => {
4+ test ( 'create empty stack node' , ( ) => {
5+ const node = new StackNode ( ) ;
6+
7+ expect ( node . data ) . toBeUndefined ( ) ;
8+ expect ( node . next ) . toBeNull ( ) ;
9+ expect ( node . next ) . toBeNull ( ) ;
10+ } ) ;
11+
12+ test ( 'create stack node with value' , ( ) => {
13+ const node = new StackNode ( 10 ) ;
14+
15+ expect ( node . data ) . toBe ( 10 ) ;
16+ expect ( node . next ) . toBeNull ( ) ;
17+ } ) ;
18+
19+ test ( 'create stack node with object as a value' , ( ) => {
20+ const nodeValue = { key : 1 , value : 'test' } ;
21+ const node = new StackNode ( nodeValue ) ;
22+
23+ expect ( node . data . key ) . toBe ( 1 ) ;
24+ expect ( node . data . value ) . toBe ( 'test' ) ;
25+ expect ( node . next ) . toBeNull ( ) ;
26+ } ) ;
27+ } ) ;
You can’t perform that action at this time.
0 commit comments