File tree 8 files changed +32
-153
lines changed
8 files changed +32
-153
lines changed Original file line number Diff line number Diff line change @@ -73,25 +73,16 @@ class SinglyCircularLinkedList {
73
73
this . size --
74
74
}
75
75
76
- printData ( ) {
76
+ printData ( output = value => console . log ( value ) ) {
77
77
let count = 0
78
78
let current = this . head
79
79
80
- while ( current !== null && count !== this . size ) {
81
- console . log ( current . data + '\n' )
80
+ while ( current !== null && count < this . size ) {
81
+ output ( current . data )
82
82
current = current . next
83
83
count ++
84
84
}
85
85
}
86
86
}
87
87
88
- const ll = new SinglyCircularLinkedList ( )
89
-
90
- ll . insert ( 10 )
91
- ll . insert ( 20 )
92
- ll . insert ( 30 )
93
- ll . insert ( 40 )
94
- ll . insert ( 50 )
95
- ll . insertAt ( 5 , 60 )
96
- ll . remove ( 5 )
97
- ll . printData ( )
88
+ export { SinglyCircularLinkedList }
Original file line number Diff line number Diff line change @@ -180,12 +180,12 @@ const LinkedList = (function () {
180
180
}
181
181
182
182
// Function to view the LinkedList
183
- LinkedList . prototype . view = function ( ) {
183
+ LinkedList . prototype . view = function ( output = value => console . log ( value ) ) {
184
184
let currentNode = this . head
185
185
let count = 0
186
186
while ( count < this . length ) {
187
187
count ++
188
- console . log ( currentNode . element )
188
+ output ( currentNode . element )
189
189
currentNode = currentNode . next
190
190
}
191
191
}
@@ -194,16 +194,4 @@ const LinkedList = (function () {
194
194
return LinkedList
195
195
} ( ) )
196
196
197
- // Implementation of LinkedList
198
- const linklist = new LinkedList ( )
199
- linklist . add ( 2 )
200
- linklist . add ( 5 )
201
- linklist . add ( 8 )
202
- linklist . add ( 12 )
203
- linklist . add ( 17 )
204
- console . log ( linklist . size ( ) )
205
- console . log ( linklist . removeAt ( 4 ) )
206
- linklist . addAt ( 4 , 15 )
207
- console . log ( linklist . indexOf ( 8 ) )
208
- console . log ( linklist . size ( ) )
209
- linklist . view ( )
197
+ export { LinkedList }
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ class CircularQueue {
28
28
// REMOVES ELEMENTS
29
29
dequeue ( ) {
30
30
if ( this . checkEmpty ( ) ) {
31
- console . log ( ' UNDERFLOW' )
31
+ // UNDERFLOW
32
32
return
33
33
}
34
34
const y = this . queue [ this . front ]
@@ -62,15 +62,15 @@ class CircularQueue {
62
62
// Checks if max capacity of queue has been reached or not
63
63
checkOverflow ( ) {
64
64
if ( ( this . front === 1 && this . rear === this . maxLength ) || ( this . front === this . rear + 1 ) ) {
65
- console . log ( ' CIRCULAR QUEUE OVERFLOW' )
65
+ // CIRCULAR QUEUE OVERFLOW
66
66
return true
67
67
}
68
68
}
69
69
70
- // Prints the entire array
71
- display ( ) {
70
+ // Prints the entire array ('*' represents blank space)
71
+ display ( output = value => console . log ( value ) ) {
72
72
for ( let index = 1 ; index < this . queue . length ; index ++ ) {
73
- console . log ( this . queue [ index ] )
73
+ output ( this . queue [ index ] )
74
74
}
75
75
}
76
76
@@ -85,24 +85,4 @@ class CircularQueue {
85
85
}
86
86
}
87
87
88
- function main ( ) {
89
- // Star represents blank space
90
- const queue = new CircularQueue ( 6 ) // Enter Max Length
91
- queue . enqueue ( 1 )
92
- queue . enqueue ( 15 )
93
- queue . enqueue ( 176 )
94
- queue . enqueue ( 59 )
95
- queue . enqueue ( 3 )
96
- queue . enqueue ( 55 )
97
-
98
- queue . display ( )
99
-
100
- queue . dequeue ( )
101
- queue . dequeue ( )
102
- queue . dequeue ( )
103
- queue . display ( )
104
-
105
- console . log ( queue . peek ( ) )
106
- }
107
-
108
- main ( )
88
+ export { CircularQueue }
Original file line number Diff line number Diff line change @@ -43,38 +43,11 @@ const Queue = (function () {
43
43
}
44
44
45
45
// List all the items in the queue
46
- Queue . prototype . view = function ( ) {
47
- console . log ( this . queue )
46
+ Queue . prototype . view = function ( output = value => console . log ( value ) ) {
47
+ output ( this . queue )
48
48
}
49
49
50
50
return Queue
51
51
} ( ) )
52
52
53
- // Implementation
54
- const myQueue = new Queue ( )
55
-
56
- myQueue . enqueue ( 1 )
57
- myQueue . enqueue ( 5 )
58
- myQueue . enqueue ( 76 )
59
- myQueue . enqueue ( 69 )
60
- myQueue . enqueue ( 32 )
61
- myQueue . enqueue ( 54 )
62
-
63
- myQueue . view ( )
64
-
65
- console . log ( `Length: ${ myQueue . length ( ) } ` )
66
- console . log ( `Front item: ${ myQueue . peek ( ) } ` )
67
- console . log ( `Removed ${ myQueue . dequeue ( ) } from front.` )
68
- console . log ( `New front item: ${ myQueue . peek ( ) } ` )
69
- console . log ( `Removed ${ myQueue . dequeue ( ) } from front.` )
70
- console . log ( `New front item: ${ myQueue . peek ( ) } ` )
71
- myQueue . enqueue ( 55 )
72
- console . log ( 'Inserted 55' )
73
- console . log ( `New front item: ${ myQueue . peek ( ) } ` )
74
-
75
- for ( let i = 0 ; i < 5 ; i ++ ) {
76
- myQueue . dequeue ( )
77
- myQueue . view ( )
78
- }
79
-
80
- // console.log(myQueue.dequeue()); // throws exception!
53
+ export { Queue }
Original file line number Diff line number Diff line change @@ -45,28 +45,14 @@ const Stack = (function () {
45
45
}
46
46
47
47
// To see all the elements in the stack
48
- Stack . prototype . view = function ( ) {
49
- for ( let i = 0 ; i < this . top ; i ++ ) { console . log ( this . stack [ i ] ) }
48
+ Stack . prototype . view = function ( output = value => console . log ( value ) ) {
49
+ for ( let i = 0 ; i < this . top ; i ++ ) {
50
+ output ( this . stack [ i ] )
51
+ }
50
52
}
51
53
52
54
return Stack
53
55
} ( ) )
54
56
55
- // Implementation
56
- const myStack = new Stack ( )
57
57
58
- myStack . push ( 1 )
59
- myStack . push ( 5 )
60
- myStack . push ( 76 )
61
- myStack . push ( 69 )
62
- myStack . push ( 32 )
63
- myStack . push ( 54 )
64
- console . log ( myStack . size ( ) )
65
- console . log ( myStack . peek ( ) )
66
- console . log ( myStack . pop ( ) )
67
- console . log ( myStack . peek ( ) )
68
- console . log ( myStack . pop ( ) )
69
- console . log ( myStack . peek ( ) )
70
- myStack . push ( 55 )
71
- console . log ( myStack . peek ( ) )
72
- myStack . view ( )
58
+ export { Stack }
Original file line number Diff line number Diff line change @@ -53,16 +53,5 @@ class Stack {
53
53
return el instanceof Stack
54
54
}
55
55
}
56
- const newStack = new Stack ( )
57
- console . log ( 'Is it a Stack?,' , Stack . isStack ( newStack ) )
58
- console . log ( 'Is stack empty? ' , newStack . isEmpty )
59
- newStack . push ( 'Hello world' )
60
- newStack . push ( 42 )
61
- newStack . push ( { a : 6 , b : 7 } )
62
- console . log ( 'The length of stack is ' , newStack . length )
63
- console . log ( 'Is stack empty? ' , newStack . isEmpty )
64
- console . log ( 'Give me the last one ' , newStack . last )
65
- console . log ( 'Pop the latest ' , newStack . pop ( ) )
66
- console . log ( 'Pop the latest ' , newStack . pop ( ) )
67
- console . log ( 'Pop the latest ' , newStack . pop ( ) )
68
- console . log ( 'Is stack empty? ' , newStack . isEmpty )
56
+
57
+ export { Stack }
Original file line number Diff line number Diff line change @@ -32,13 +32,13 @@ const Node = (function Node () {
32
32
}
33
33
34
34
// Visit a node
35
- Node . prototype . visit = function ( ) {
35
+ Node . prototype . visit = function ( output = value => console . log ( value ) ) {
36
36
// Recursively go left
37
37
if ( this . left !== null ) {
38
38
this . left . visit ( )
39
39
}
40
40
// Print out value
41
- console . log ( this . value )
41
+ output ( this . value )
42
42
// Recursively go right
43
43
if ( this . right !== null ) {
44
44
this . right . visit ( )
@@ -115,7 +115,7 @@ const Tree = (function () {
115
115
// Inorder traversal
116
116
Tree . prototype . traverse = function ( ) {
117
117
if ( ! this . root ) {
118
- console . log ( ' No nodes are there in the tree till now' )
118
+ // No nodes are there in the tree till now
119
119
return
120
120
}
121
121
this . root . visit ( )
@@ -124,11 +124,11 @@ const Tree = (function () {
124
124
// Start by searching the root
125
125
Tree . prototype . search = function ( val ) {
126
126
const found = this . root . search ( val )
127
- if ( found === null ) {
128
- console . log ( val + ' not found' )
129
- } else {
130
- console . log ( 'Found:' + found . value )
127
+ if ( found !== null ) {
128
+ return found . value
131
129
}
130
+ // not found
131
+ return null
132
132
}
133
133
134
134
// Add a new value to the tree
@@ -151,16 +151,4 @@ const Tree = (function () {
151
151
return Tree
152
152
} ( ) )
153
153
154
- // Implementation of BST
155
- const bst = new Tree ( )
156
- bst . addValue ( 6 )
157
- bst . addValue ( 3 )
158
- bst . addValue ( 9 )
159
- bst . addValue ( 2 )
160
- bst . addValue ( 8 )
161
- bst . addValue ( 4 )
162
- bst . traverse ( )
163
- bst . search ( 8 )
164
- bst . removeValue ( 3 )
165
- bst . removeValue ( 8 )
166
- bst . traverse ( )
154
+ export { Tree }
Original file line number Diff line number Diff line change @@ -118,20 +118,4 @@ Trie.prototype.findOccurences = function (word) {
118
118
return node . count
119
119
} ;
120
120
121
- // To test
122
- ( function demo ( ) {
123
- const x = new Trie ( )
124
- x . insert ( 'sheldon' )
125
- x . insert ( 'hello' )
126
- x . insert ( 'anyword' )
127
- x . insert ( 'sheldoncooper' )
128
- console . log ( x . findOccurences ( 'sheldon' ) )
129
- x . remove ( 'anything' )
130
- x . insert ( 'sheldon' )
131
- console . log ( x . findOccurences ( 'sheldon' ) )
132
- console . log ( x . findAllWords ( 'sheldon' ) )
133
- x . insert ( 'anything' )
134
- x . remove ( 'sheldoncooper' )
135
- console . log ( x . contains ( 'sheldoncooper' ) )
136
- console . log ( x . findAllWords ( 'sheldon' ) )
137
- } ) ( )
121
+ export { Trie }
You can’t perform that action at this time.
0 commit comments